00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 #ifdef __GNUC__
00074 #include <sys/reent.h>
00075 #endif
00076
00077 #include <stdarg.h>
00078 #include <string.h>
00079
00080 #include "compiler.h"
00081
00082
00083 #define putchar(c) vtracedump_Putchar_Block(c)
00084
00085 static void printchar(char **str, int c)
00086 {
00087 extern int putchar(int c);
00088
00089 if (str) {
00090 **str = c;
00091 ++(*str);
00092 }
00093 else (void)putchar(c);
00094 }
00095
00096 #define PAD_RIGHT 1
00097 #define PAD_ZERO 2
00098
00099 static int prints(char **out, const char *string, int width, int pad, int printlimit, Bool IsNumber)
00100 {
00101 register int pc = 0, padchar = ' ';
00102 int i,len;
00103
00104 if (width > 0) {
00105 register int len = 0;
00106 register const char *ptr;
00107 for (ptr = string; *ptr; ++ptr) ++len;
00108 if (len >= width) width = 0;
00109 else width -= len;
00110 if (pad & PAD_ZERO) padchar = '0';
00111 }
00112 if (!(pad & PAD_RIGHT)) {
00113 for ( ; width > 0; --width) {
00114 printchar (out, padchar);
00115 ++pc;
00116 }
00117 }
00118 if( FALSE == IsNumber )
00119 {
00120
00121 for ( ; printlimit && *string ; ++string, --printlimit) {
00122 printchar (out, *string);
00123 ++pc;
00124 }
00125 }
00126 if( TRUE == IsNumber )
00127 {
00128
00129
00130
00131
00132 len = strlen(string);
00133 if( len < printlimit )
00134 {
00135 i = printlimit - len;
00136 for(; i; i--) {
00137 printchar (out, '0');
00138 ++pc;
00139 }
00140 }
00141 }
00142
00143
00144
00145
00146 for ( ; printlimit && *string ; ++string, --printlimit) {
00147 printchar (out, *string);
00148 ++pc;
00149 }
00150
00151 for ( ; width > 0; --width) {
00152 printchar (out, padchar);
00153 ++pc;
00154 }
00155
00156 return pc;
00157 }
00158
00159
00160 #define PRINT_BUF_LEN 12
00161
00162 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase, int printlimit)
00163 {
00164 char print_buf[PRINT_BUF_LEN];
00165 register char *s;
00166 register int t, neg = 0, pc = 0;
00167 register unsigned int u = i;
00168
00169 if (i == 0) {
00170 print_buf[0] = '0';
00171 print_buf[1] = '\0';
00172 return prints (out, print_buf, width, pad, printlimit, TRUE);
00173 }
00174
00175 if (sg && b == 10 && i < 0) {
00176 neg = 1;
00177 u = -i;
00178 }
00179
00180 s = print_buf + PRINT_BUF_LEN-1;
00181 *s = '\0';
00182
00183 while (u) {
00184 t = u % b;
00185 if( t >= 10 )
00186 t += letbase - '0' - 10;
00187 *--s = t + '0';
00188 u /= b;
00189 }
00190
00191 if (neg) {
00192 if( width && (pad & PAD_ZERO) ) {
00193 printchar (out, '-');
00194 ++pc;
00195 --width;
00196 }
00197 else {
00198 *--s = '-';
00199 }
00200 }
00201
00202 return pc + prints (out, s, width, pad, printlimit, TRUE);
00203 }
00204
00205 #ifdef __GNUC__
00206 int fprintf(__FILE *stream, const char *format, ...)
00207 {
00208 return 0;
00209 }
00210 #endif
00211
00212 static int print(char **out, const char *format, va_list args )
00213 {
00214 register int width, pad, printlimit;
00215 register int pc = 0;
00216 char scr[2];
00217
00218 for (; *format != 0; ++format) {
00219 if (*format == '%') {
00220 ++format;
00221 width = pad = printlimit = 0;
00222 if (*format == '\0') break;
00223 if (*format == '%') goto out;
00224 if (*format == '-') {
00225 ++format;
00226 pad = PAD_RIGHT;
00227 }
00228 while (*format == '0') {
00229 ++format;
00230 pad |= PAD_ZERO;
00231 }
00232 for ( ; *format >= '0' && *format <= '9'; ++format) {
00233 width *= 10;
00234 width += *format - '0';
00235 }
00236 if (*format == '.') {
00237 ++format;
00238 for ( ; *format >= '0' && *format <= '9'; ++format) {
00239 printlimit *= 10;
00240 printlimit += *format - '0';
00241 }
00242 }
00243 if( 0 == printlimit )
00244 printlimit--;
00245 if( *format == 's' ) {
00246 register char *s = (char *)va_arg( args, int );
00247 pc += prints (out, s?s:"(null)", width, pad, printlimit, FALSE);
00248 continue;
00249 }
00250 if( *format == 'd' ) {
00251 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a', printlimit);
00252 continue;
00253 }
00254 if( *format == 'x' ) {
00255 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a', printlimit);
00256 continue;
00257 }
00258 if( *format == 'X' ) {
00259 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A', printlimit);
00260 continue;
00261 }
00262 if( *format == 'u' ) {
00263 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a', printlimit);
00264 continue;
00265 }
00266 if( *format == 'c' ) {
00267
00268 scr[0] = (char)va_arg( args, int );
00269 scr[1] = '\0';
00270 pc += prints (out, scr, width, pad, printlimit, FALSE);
00271 continue;
00272 }
00273 }
00274 else {
00275 out:
00276 printchar (out, *format);
00277 ++pc;
00278 }
00279 }
00280 if (out) **out = '\0';
00281 va_end( args );
00282 return pc;
00283 }
00284
00285 int printk(const char *format, ...)
00286 {
00287 va_list args;
00288
00289 va_start( args, format );
00290 return print( 0, format, args );
00291 }
00292
00293 int sprintf(char *out, const char *format, ...)
00294 {
00295 va_list args;
00296
00297 va_start( args, format );
00298 return print( &out, format, args );
00299 }
00300
00301 #ifdef TEST_PRINTF
00302 int main(void)
00303 {
00304 char *ptr = "Hello world!";
00305 char *np = 0;
00306 int i = 5;
00307 unsigned int bs = sizeof(int)*8;
00308 int mi;
00309 char buf[80];
00310
00311 mi = (1 << (bs-1)) + 1;
00312 printf("%s\n", ptr);
00313 printf("printf test\n");
00314 printf("%s is null pointer\n", np);
00315 printf("%d = 5\n", i);
00316 printf("%d = - max int\n", mi);
00317 printf("char %c = 'a'\n", 'a');
00318 printf("hex %x = ff\n", 0xff);
00319 printf("hex %02x = 00\n", 0);
00320 printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
00321 printf("%d %s(s)%", 0, "message");
00322 printf("\n");
00323 printf("%d %s(s) with %%\n", 0, "message");
00324 sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
00325 sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
00326 sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
00327 sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
00328 sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
00329 sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
00330 sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
00331 sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
00332
00333 return 0;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366 #endif