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