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 #include <sys/reent.h>
00071 #include <stdarg.h>
00072
00073 #define putchar(c) print_dbg_char(c)
00074
00075 static void printchar(char **str, int c)
00076 {
00077 extern int putchar(int c);
00078
00079 if (str) {
00080 **str = c;
00081 ++(*str);
00082 }
00083 else (void)putchar(c);
00084 }
00085
00086 #define PAD_RIGHT 1
00087 #define PAD_ZERO 2
00088
00089 static int prints(char **out, const char *string, int width, int pad)
00090 {
00091 register int pc = 0, padchar = ' ';
00092
00093 if (width > 0) {
00094 register int len = 0;
00095 register const char *ptr;
00096 for (ptr = string; *ptr; ++ptr) ++len;
00097 if (len >= width) width = 0;
00098 else width -= len;
00099 if (pad & PAD_ZERO) padchar = '0';
00100 }
00101 if (!(pad & PAD_RIGHT)) {
00102 for ( ; width > 0; --width) {
00103 printchar (out, padchar);
00104 ++pc;
00105 }
00106 }
00107 for ( ; *string ; ++string) {
00108 printchar (out, *string);
00109 ++pc;
00110 }
00111 for ( ; width > 0; --width) {
00112 printchar (out, padchar);
00113 ++pc;
00114 }
00115
00116 return pc;
00117 }
00118
00119
00120 #define PRINT_BUF_LEN 12
00121
00122 static int printi(char **out, int i, int b, int sg, int width, int pad, int letbase)
00123 {
00124 char print_buf[PRINT_BUF_LEN];
00125 register char *s;
00126 register int t, neg = 0, pc = 0;
00127 register unsigned int u = i;
00128
00129 if (i == 0) {
00130 print_buf[0] = '0';
00131 print_buf[1] = '\0';
00132 return prints (out, print_buf, width, pad);
00133 }
00134
00135 if (sg && b == 10 && i < 0) {
00136 neg = 1;
00137 u = -i;
00138 }
00139
00140 s = print_buf + PRINT_BUF_LEN-1;
00141 *s = '\0';
00142
00143 while (u) {
00144 t = u % b;
00145 if( t >= 10 )
00146 t += letbase - '0' - 10;
00147 *--s = t + '0';
00148 u /= b;
00149 }
00150
00151 if (neg) {
00152 if( width && (pad & PAD_ZERO) ) {
00153 printchar (out, '-');
00154 ++pc;
00155 --width;
00156 }
00157 else {
00158 *--s = '-';
00159 }
00160 }
00161
00162 return pc + prints (out, s, width, pad);
00163 }
00164
00165 int fprintf(__FILE *stream, const char *format, ...)
00166 {
00167 return 0;
00168 }
00169 int printk_va(char **out, const char *format, va_list args )
00170 {
00171 register int width, pad;
00172 register int pc = 0;
00173 char scr[2];
00174
00175 for (; *format != 0; ++format) {
00176 if (*format == '%') {
00177 ++format;
00178 width = pad = 0;
00179 if (*format == '\0') break;
00180 if (*format == '%') goto out;
00181 if (*format == '-') {
00182 ++format;
00183 pad = PAD_RIGHT;
00184 }
00185 while (*format == '0') {
00186 ++format;
00187 pad |= PAD_ZERO;
00188 }
00189 for ( ; *format >= '0' && *format <= '9'; ++format) {
00190 width *= 10;
00191 width += *format - '0';
00192 }
00193 if( *format == 's' ) {
00194 register char *s = (char *)va_arg( args, int );
00195 pc += prints (out, s?s:"(null)", width, pad);
00196 continue;
00197 }
00198 if( *format == 'd' ) {
00199 pc += printi (out, va_arg( args, int ), 10, 1, width, pad, 'a');
00200 continue;
00201 }
00202 if( *format == 'p' ) {
00203 pad = 8;
00204 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
00205 continue;
00206 }
00207 if( *format == 'x' ) {
00208 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'a');
00209 continue;
00210 }
00211 if( *format == 'X' ) {
00212 pc += printi (out, va_arg( args, int ), 16, 0, width, pad, 'A');
00213 continue;
00214 }
00215 if( *format == 'u' ) {
00216 pc += printi (out, va_arg( args, int ), 10, 0, width, pad, 'a');
00217 continue;
00218 }
00219 if( *format == 'c' ) {
00220
00221 scr[0] = (char)va_arg( args, int );
00222 scr[1] = '\0';
00223 pc += prints (out, scr, width, pad);
00224 continue;
00225 }
00226 }
00227 else {
00228 out:
00229 printchar (out, *format);
00230 ++pc;
00231 }
00232 }
00233 if (out) **out = '\0';
00234 va_end( args );
00235 return pc;
00236 }
00237
00238 int printk(const char *format, ...)
00239 {
00240 va_list args;
00241
00242 va_start( args, format );
00243 return printk_va( 0, format, args );
00244 }
00245
00246 int sprintf(char *out, const char *format, ...)
00247 {
00248 va_list args;
00249
00250 va_start( args, format );
00251 return printk_va( &out, format, args );
00252 }
00253
00254 #ifdef TEST_PRINTF
00255 int main(void)
00256 {
00257 char *ptr = "Hello world!";
00258 char *np = 0;
00259 int i = 5;
00260 unsigned int bs = sizeof(int)*8;
00261 int mi;
00262 char buf[80];
00263
00264 mi = (1 << (bs-1)) + 1;
00265 printf("%s\n", ptr);
00266 printf("printf test\n");
00267 printf("%s is null pointer\n", np);
00268 printf("%d = 5\n", i);
00269 printf("%d = - max int\n", mi);
00270 printf("char %c = 'a'\n", 'a');
00271 printf("hex %x = ff\n", 0xff);
00272 printf("hex %02x = 00\n", 0);
00273 printf("signed %d = unsigned %u = hex %x\n", -3, -3, -3);
00274 printf("%d %s(s)%", 0, "message");
00275 printf("\n");
00276 printf("%d %s(s) with %%\n", 0, "message");
00277 sprintf(buf, "justif: \"%-10s\"\n", "left"); printf("%s", buf);
00278 sprintf(buf, "justif: \"%10s\"\n", "right"); printf("%s", buf);
00279 sprintf(buf, " 3: %04d zero padded\n", 3); printf("%s", buf);
00280 sprintf(buf, " 3: %-4d left justif.\n", 3); printf("%s", buf);
00281 sprintf(buf, " 3: %4d right justif.\n", 3); printf("%s", buf);
00282 sprintf(buf, "-3: %04d zero padded\n", -3); printf("%s", buf);
00283 sprintf(buf, "-3: %-4d left justif.\n", -3); printf("%s", buf);
00284 sprintf(buf, "-3: %4d right justif.\n", -3); printf("%s", buf);
00285
00286 return 0;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 #endif