lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * This file based on printf.c from 'Dlibs' on the atari ST (RdeBath) |
| 3 | * |
| 4 | * |
| 5 | * Dale Schumacher 399 Beacon Ave. |
| 6 | * (alias: Dalnefre') St. Paul, MN 55104 |
| 7 | * dal@syntel.UUCP United States of America |
| 8 | * "It's not reality that's important, but how you perceive things." |
| 9 | */ |
| 10 | |
| 11 | /* Altered to use stdarg, made the core function vfnprintf. |
| 12 | * Hooked into the stdio package using 'inside information' |
| 13 | * Altered sizeof() assumptions, now assumes all integers except chars |
| 14 | * will be either |
| 15 | * sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short) |
| 16 | * |
| 17 | * -RDB |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Manuel Novoa III Dec 2000 |
| 22 | * |
| 23 | * The previous vfnprintf routine was almost completely rewritten with the |
| 24 | * goal of fixing some shortcomings and reducing object size. |
| 25 | * |
| 26 | * The summary of changes: |
| 27 | * |
| 28 | * Converted print conversion specification parsing from one big switch |
| 29 | * to a method using string tables. This new method verifies that the |
| 30 | * conversion flags, field width, precision, qualifier, and specifier |
| 31 | * appear in the correct order. Many questionable specifications were |
| 32 | * accepted by the previous code. This new method also resulted in a |
| 33 | * substantial reduction in object size of about 330 bytes (20%) from |
| 34 | * the old version (1627 bytes) on i386, even with the following |
| 35 | * improvements. |
| 36 | * |
| 37 | * Implemented %n specifier as required by the standards. |
| 38 | * Implemented proper handling of precision for int types. |
| 39 | * Implemented # for hex and pointer, fixed error for octal rep of 0. |
| 40 | * Implemented return of -1 on stream error. |
| 41 | * |
| 42 | * Added optional support for the GNU extension %m which prints the string |
| 43 | * corresponding the errno. |
| 44 | * |
| 45 | * Added optional support for long long ints and unsigned long long ints |
| 46 | * using the conversion qualifiers "ll", "L", or "q" (like glibc). |
| 47 | * |
| 48 | * Added optional support for doubles in a very limited form. None of |
| 49 | * the formating options are obeyed. The string returned by __dtostr |
| 50 | * is printed directly. |
| 51 | * |
| 52 | * Converted to use my (un)signed long (long) to string routines, which are |
| 53 | * smaller than the previous functions and don't require static buffers. |
| 54 | * |
| 55 | * Other Modifications: |
| 56 | * Modified sprintf, snprintf, vsprintf, vsnprintf to share on fake-file. |
| 57 | */ |
| 58 | |
| 59 | /* |
| 60 | * Manuel Novoa III Jan 2001 |
| 61 | * |
| 62 | * Removed fake file from *s*printf functions because of possible problems |
| 63 | * if called recursively. Instead, have sprintf, snprintf, and vsprintf |
| 64 | * call vsnprintf which allocates a fake file on the stack. |
| 65 | * Removed WANT_FPUTC option. Always use standard putc macro to avoid |
| 66 | * problems with the fake file used by the *s*printf functions. |
| 67 | * Fixed bug parsing flags -- did not restart scan. |
| 68 | * Added function asprintf. |
| 69 | * Fixed 0-pad prefixing bug. |
| 70 | * Converted sizeof(int) == sizeof(long) tests to compile time vs run time. |
| 71 | * This saves 112 bytes of code on i386. |
| 72 | * Fixed precision bug -- when negative set to default. |
| 73 | * Added function fnprintf to support __dtostr. |
| 74 | * Added floating point support for doubles. Yeah! |
| 75 | * |
| 76 | * |
| 77 | * May 2001 Fixes from Johan Adolfsson (johan.adolfsson@axis.com) |
| 78 | * 1) printf("%c",0) returned 0 instead of 1. |
| 79 | * 2) unrolled loop in asprintf to reduce size and remove compile warning. |
| 80 | * |
| 81 | * |
| 82 | * June 2001 |
| 83 | * 1) fix %p so that "0x" is prepended to outputed hex val |
| 84 | * 2) fix %p so that "(nil)" is output for (void *)0 to match glibc |
| 85 | * |
| 86 | * Sep 5, 2003 |
| 87 | * Convert to new floating point conversion routine. |
| 88 | * Fix qualifier handling on integer and %n conversions. |
| 89 | * Add support for vsnprintf when in non-buffered/no-wchar configuration. |
| 90 | * |
| 91 | */ |
| 92 | |
| 93 | /*****************************************************************************/ |
| 94 | /* OPTIONS */ |
| 95 | /*****************************************************************************/ |
| 96 | /* The optional support for long longs and doubles comes in two forms. |
| 97 | * |
| 98 | * 1) Normal (or partial for doubles) output support. Set to 1 to turn on. |
| 99 | * Adds about 130 bytes for doubles, about 220 bytes for long longs, |
| 100 | * and about 275 for both to the base code size of 1163 on i386. |
| 101 | */ |
| 102 | |
| 103 | /* These are now set in uClibc_config.h based on Config. */ |
| 104 | /* |
| 105 | #define __UCLIBC_HAS_FLOATS__ 1 |
| 106 | */ |
| 107 | |
| 108 | /* 2) An error message is inserted into the stream, an arg of the |
| 109 | * appropriate size is removed from the arglist, and processing |
| 110 | * continues. This is adds less code and may be useful in some |
| 111 | * cases. Set to 1 to turn on. Adds about 50 bytes for doubles, |
| 112 | * about 140 bytes for long longs, and about 175 bytes for both |
| 113 | * to the base code size of 1163 on i386. |
| 114 | */ |
| 115 | |
| 116 | #define WANT_FLOAT_ERROR 0 |
| 117 | |
| 118 | /* |
| 119 | * Set to support GNU extension of %m to print string corresponding to errno. |
| 120 | * |
| 121 | * Warning: This adds about 50 bytes (i386) to the code but it also pulls in |
| 122 | * strerror and the corresponding string table which together are about 3.8k. |
| 123 | */ |
| 124 | |
| 125 | /* Now controlled by uClibc_stdio.h and set below. */ |
| 126 | /* #define WANT_GNU_ERRNO 0 */ |
| 127 | |
| 128 | /**************************************************************************/ |
| 129 | |
| 130 | #define _ISOC99_SOURCE /* for ULLONG primarily... */ |
| 131 | #include "_stdio.h" |
| 132 | /* #include <stdio.h> */ |
| 133 | #include <stdarg.h> |
| 134 | #include <limits.h> |
| 135 | #include <stdint.h> |
| 136 | #include <string.h> |
| 137 | #include <errno.h> |
| 138 | #include <ctype.h> |
| 139 | #include <bits/uClibc_uintmaxtostr.h> |
| 140 | #include <printf.h> |
| 141 | |
| 142 | #ifdef __UCLIBC_HAS_THREADS__ |
| 143 | #include <pthread.h> |
| 144 | #endif /* __UCLIBC_HAS_THREADS__ */ |
| 145 | |
| 146 | |
| 147 | /* #undef __UCLIBC_HAS_FLOATS__ */ |
| 148 | /* #undef WANT_FLOAT_ERROR */ |
| 149 | /* #define WANT_FLOAT_ERROR 1 */ |
| 150 | |
| 151 | /* #define __isdigit(c) (((unsigned int)(c - '0')) < 10) */ |
| 152 | |
| 153 | #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__ |
| 154 | #define WANT_GNU_ERRNO 1 |
| 155 | #else |
| 156 | #define WANT_GNU_ERRNO 0 |
| 157 | #endif |
| 158 | |
| 159 | #undef PUTC |
| 160 | #undef OUTNSTR |
| 161 | #undef _outnstr |
| 162 | |
| 163 | #ifdef __STDIO_BUFFERS |
| 164 | |
| 165 | #define PUTC(C,F) putc_unlocked((C),(F)) |
| 166 | #define OUTNSTR _outnstr |
| 167 | #define _outnstr(stream, string, len) __stdio_fwrite(string, len, stream) |
| 168 | |
| 169 | #else /* __STDIO_BUFFERS */ |
| 170 | |
| 171 | typedef struct { |
| 172 | FILE f; |
| 173 | unsigned char *bufend; /* pointer to 1 past end of buffer */ |
| 174 | unsigned char *bufpos; |
| 175 | } __FILE_vsnprintf; |
| 176 | |
| 177 | #ifdef __UCLIBC_HAS_FLOATS__ |
| 178 | static void _outnstr(FILE *stream, const unsigned char *s, size_t n) |
| 179 | { |
| 180 | __FILE_vsnprintf *f = (__FILE_vsnprintf *) stream; |
| 181 | |
| 182 | if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) { |
| 183 | __stdio_fwrite(s, n, &f->f); |
| 184 | } else if (f->bufend > f->bufpos) { |
| 185 | size_t r = f->bufend - f->bufpos; |
| 186 | if (r > n) { |
| 187 | r = n; |
| 188 | } |
| 189 | memcpy(f->bufpos, s, r); |
| 190 | f->bufpos += r; |
| 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | static void putc_unlocked_sprintf(int c, __FILE_vsnprintf *f) |
| 196 | { |
| 197 | if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) { |
| 198 | putc_unlocked(c, &f->f); |
| 199 | } else if (f->bufpos < f->bufend) { |
| 200 | *f->bufpos++ = c; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | |
| 205 | #define PUTC(C,F) putc_unlocked_sprintf((C),(__FILE_vsnprintf *)(F)) |
| 206 | #define OUTNSTR _outnstr |
| 207 | |
| 208 | #endif /* __STDIO_BUFFERS */ |
| 209 | |
| 210 | #ifdef __UCLIBC_HAS_FLOATS__ |
| 211 | #include <float.h> |
| 212 | #include <bits/uClibc_fpmax.h> |
| 213 | |
| 214 | typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len, |
| 215 | intptr_t buf); |
| 216 | |
| 217 | extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info, |
| 218 | __fp_outfunc_t fp_outfunc) attribute_hidden; |
| 219 | |
| 220 | static void _charpad(FILE * __restrict stream, int padchar, size_t numpad) |
| 221 | { |
| 222 | /* TODO -- Use a buffer to cut down on function calls... */ |
| 223 | char pad[1]; |
| 224 | |
| 225 | *pad = padchar; |
| 226 | while (numpad) { |
| 227 | OUTNSTR(stream, pad, 1); |
| 228 | --numpad; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf) |
| 233 | { |
| 234 | if (type & 0x80) { /* Some type of padding needed. */ |
| 235 | int buflen = strlen((const char *) buf); |
| 236 | if ((len -= buflen) > 0) { |
| 237 | _charpad(fp, (type & 0x7f), len); |
| 238 | } |
| 239 | len = buflen; |
| 240 | } |
| 241 | if (len) { |
| 242 | OUTNSTR(fp, (const char *) buf, len); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | #endif |
| 247 | |
| 248 | |
| 249 | enum { |
| 250 | FLAG_PLUS = 0, |
| 251 | FLAG_MINUS_LJUSTIFY, |
| 252 | FLAG_HASH, |
| 253 | FLAG_0_PAD, |
| 254 | FLAG_SPACE, |
| 255 | }; |
| 256 | |
| 257 | /* layout 01234 */ |
| 258 | static const char spec[] = "+-#0 "; |
| 259 | |
| 260 | /**********************************************************************/ |
| 261 | |
| 262 | extern void _store_inttype(void *dest, int desttype, uintmax_t val) attribute_hidden; |
| 263 | extern uintmax_t _load_inttype(int desttype, const void *src, int uflag) attribute_hidden; |
| 264 | |
| 265 | /* |
| 266 | * In order to ease translation to what arginfo and _print_info._flags expect, |
| 267 | * we map: 0:int 1:char 2:longlong 4:long 8:short |
| 268 | * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701) |
| 269 | */ |
| 270 | |
| 271 | #ifdef PDS |
| 272 | #error PDS already defined! |
| 273 | #endif |
| 274 | #ifdef SS |
| 275 | #error SS already defined! |
| 276 | #endif |
| 277 | #ifdef IMS |
| 278 | #error IMS already defined! |
| 279 | #endif |
| 280 | |
| 281 | #if PTRDIFF_MAX == INT_MAX |
| 282 | #define PDS 0 |
| 283 | #elif PTRDIFF_MAX == LONG_MAX |
| 284 | #define PDS 4 |
| 285 | #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX) |
| 286 | #define PDS 8 |
| 287 | #else |
| 288 | #error fix QUAL_CHARS ptrdiff_t entry 't'! |
| 289 | #endif |
| 290 | |
| 291 | #if SIZE_MAX == UINT_MAX |
| 292 | #define SS 0 |
| 293 | #elif SIZE_MAX == ULONG_MAX |
| 294 | #define SS 4 |
| 295 | #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX) |
| 296 | #define SS 8 |
| 297 | #else |
| 298 | #error fix QUAL_CHARS size_t entries 'z', 'Z'! |
| 299 | #endif |
| 300 | |
| 301 | #if INTMAX_MAX == INT_MAX |
| 302 | #define IMS 0 |
| 303 | #elif INTMAX_MAX == LONG_MAX |
| 304 | #define IMS 4 |
| 305 | #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX) |
| 306 | #define IMS 8 |
| 307 | #else |
| 308 | #error fix QUAL_CHARS intmax_t entry 'j'! |
| 309 | #endif |
| 310 | |
| 311 | #define QUAL_CHARS { \ |
| 312 | /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \ |
| 313 | /* q:long_long Z:(s)size_t */ \ |
| 314 | 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \ |
| 315 | 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\ |
| 316 | 1, 8 \ |
| 317 | } |
| 318 | |
| 319 | static const char qual_chars[] = QUAL_CHARS; |
| 320 | |
| 321 | /* static const char qual[] = "hlLq"; */ |
| 322 | /**********************************************************************/ |
| 323 | |
| 324 | #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR |
| 325 | static const char dbl_err[] = "<DOUBLE>"; |
| 326 | #endif |
| 327 | |
| 328 | #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR |
| 329 | /* layout 012345678901234567 */ |
| 330 | static const char u_spec[] = "%nbopxXudicsfgGeEaA"; |
| 331 | #else |
| 332 | /* layout 0123456789012 */ |
| 333 | static const char u_spec[] = "%nbopxXudics"; |
| 334 | #endif |
| 335 | |
| 336 | /* WARNING: u_spec and u_radix need to stay in agreement!!! */ |
| 337 | /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */ |
| 338 | static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a"; |
| 339 | |
| 340 | int vfprintf(FILE * __restrict op, register const char * __restrict fmt, |
| 341 | va_list ap) |
| 342 | { |
| 343 | union { |
| 344 | #ifdef LLONG_MAX |
| 345 | long long ll; |
| 346 | #endif |
| 347 | #if LONG_MAX != INT_MAX |
| 348 | long l; |
| 349 | #endif |
| 350 | int i; |
| 351 | } intarg; |
| 352 | int i, cnt, dataargtype, len; |
| 353 | const void *argptr = argptr; /* ok to be initialized. */ |
| 354 | register char *p; |
| 355 | const char *fmt0; |
| 356 | int preci, width; |
| 357 | #define upcase i |
| 358 | int radix, dpoint /*, upcase*/; |
| 359 | char tmp[65]; /* TODO - determine needed size from headers */ |
| 360 | char flag[sizeof(spec)]; |
| 361 | __STDIO_AUTO_THREADLOCK_VAR; |
| 362 | |
| 363 | __STDIO_AUTO_THREADLOCK(op); |
| 364 | |
| 365 | __STDIO_STREAM_VALIDATE(op); |
| 366 | |
| 367 | cnt = 0; |
| 368 | |
| 369 | if (__STDIO_STREAM_IS_NARROW_WRITING(op) |
| 370 | || !__STDIO_STREAM_TRANS_TO_WRITE(op, __FLAG_NARROW) |
| 371 | ) { |
| 372 | |
| 373 | while (*fmt) { |
| 374 | if (*fmt == '%') { |
| 375 | fmt0 = fmt; /* save our position in case of bad format */ |
| 376 | ++fmt; |
| 377 | width = -1; /* min field width */ |
| 378 | preci = -5; /* max string width or mininum digits */ |
| 379 | radix = 10; /* number base */ |
| 380 | dpoint = 0; /* found decimal point */ |
| 381 | |
| 382 | /* init flags */ |
| 383 | for (p =(char *) spec ; *p ; p++) { |
| 384 | flag[p-spec] = '\0'; |
| 385 | } |
| 386 | flag[FLAG_0_PAD] = ' '; |
| 387 | |
| 388 | /* process optional flags */ |
| 389 | for (p = (char *)spec ; *p ; ) { |
| 390 | if (*fmt == *p) { |
| 391 | flag[p-spec] = *fmt++; |
| 392 | p = (char *)spec; /* restart scan */ |
| 393 | } else { |
| 394 | p++; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | if (!flag[FLAG_PLUS]) { |
| 399 | flag[FLAG_PLUS] = flag[FLAG_SPACE]; |
| 400 | } |
| 401 | |
| 402 | /* process optional width and precision */ |
| 403 | do { |
| 404 | if (*fmt == '.') { |
| 405 | ++fmt; |
| 406 | dpoint = 1; |
| 407 | } |
| 408 | if (*fmt == '*') { /* parameter width value */ |
| 409 | ++fmt; |
| 410 | i = va_arg(ap, int); |
| 411 | } else { |
| 412 | for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) { |
| 413 | i = (i * 10) + (*fmt - '0'); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (dpoint) { |
| 418 | preci = i; |
| 419 | if (i<0) { |
| 420 | preci = -5; |
| 421 | } |
| 422 | } else { |
| 423 | width = i; |
| 424 | if (i<0) { |
| 425 | width = -i; |
| 426 | flag[FLAG_MINUS_LJUSTIFY] = 1; |
| 427 | } |
| 428 | } |
| 429 | } while ((*fmt == '.') && !dpoint ); |
| 430 | |
| 431 | /* process optional qualifier */ |
| 432 | p = (char *) qual_chars; |
| 433 | do { |
| 434 | if (*fmt == *p) { |
| 435 | ++fmt; |
| 436 | break; |
| 437 | } |
| 438 | } while (*++p); |
| 439 | if ((p - qual_chars < 2) && (*fmt == *p)) { |
| 440 | p += ((sizeof(qual_chars)-2) / 2); |
| 441 | ++fmt; |
| 442 | } |
| 443 | dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8; |
| 444 | |
| 445 | #if WANT_GNU_ERRNO |
| 446 | if (*fmt == 'm') { |
| 447 | flag[FLAG_PLUS] = '\0'; |
| 448 | flag[FLAG_0_PAD] = ' '; |
| 449 | p = __glibc_strerror_r(errno, tmp, sizeof(tmp)); |
| 450 | goto print; |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | /* process format specifier */ |
| 455 | for (p = (char *) u_spec ; *p ; p++) { |
| 456 | if (*fmt != *p) continue; |
| 457 | if (p-u_spec < 1) { /* print a % */ |
| 458 | goto charout; |
| 459 | } |
| 460 | if (p-u_spec < 2) { /* store output count in int ptr */ |
| 461 | _store_inttype(va_arg(ap, void *), |
| 462 | dataargtype, |
| 463 | (intmax_t) (cnt)); |
| 464 | goto nextfmt; |
| 465 | } |
| 466 | |
| 467 | if (p-u_spec < 10) { |
| 468 | if (*p == 'p') { |
| 469 | #if INTPTR_MAX == INT_MAX |
| 470 | dataargtype = 0; |
| 471 | #else |
| 472 | #error Fix dataargtype for pointers! |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | switch(dataargtype) { |
| 477 | case (PA_INT|PA_FLAG_LONG_LONG): |
| 478 | #ifdef LLONG_MAX |
| 479 | intarg.ll = va_arg(ap, long long); |
| 480 | argptr = &intarg.ll; |
| 481 | break; |
| 482 | #endif |
| 483 | case (PA_INT|PA_FLAG_LONG): |
| 484 | #if LONG_MAX != INT_MAX |
| 485 | intarg.l = va_arg(ap, long); |
| 486 | argptr = &intarg.l; |
| 487 | break; |
| 488 | #endif |
| 489 | default: |
| 490 | intarg.i = va_arg(ap, int); |
| 491 | argptr = &intarg.i; |
| 492 | break; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (p-u_spec < 8) { /* unsigned conversion */ |
| 497 | radix = u_radix[p-u_spec-2]; |
| 498 | upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER); |
| 499 | if (*p == 'p') { |
| 500 | upcase = __UIM_LOWER; |
| 501 | flag[FLAG_HASH] = 'p'; |
| 502 | } |
| 503 | p = _uintmaxtostr(tmp + sizeof(tmp) - 1, |
| 504 | (uintmax_t) |
| 505 | _load_inttype(dataargtype, argptr, radix), |
| 506 | radix, upcase); |
| 507 | |
| 508 | flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */ |
| 509 | if (*p != '0') { /* non-zero */ |
| 510 | if (flag[FLAG_HASH]) { |
| 511 | if (radix == 8) { |
| 512 | *--p = '0'; /* add leadding zero */ |
| 513 | } else if (radix != 10) { /* either 2 or 16 */ |
| 514 | flag[FLAG_PLUS] = '0'; |
| 515 | *--p = 'b'; |
| 516 | if (radix == 16) { |
| 517 | *p = 'x'; |
| 518 | if (*fmt == 'X') { |
| 519 | *p = 'X'; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | } else if (flag[FLAG_HASH] == 'p') { /* null pointer */ |
| 525 | p = "(nil)"; |
| 526 | } |
| 527 | } else if (p-u_spec < 10) { /* signed conversion */ |
| 528 | p = _uintmaxtostr(tmp + sizeof(tmp) - 1, |
| 529 | (uintmax_t) |
| 530 | _load_inttype(dataargtype, argptr, -radix), |
| 531 | -radix, upcase); |
| 532 | |
| 533 | } else if (p-u_spec < 12) { /* character or string */ |
| 534 | flag[FLAG_PLUS] = '\0'; |
| 535 | flag[FLAG_0_PAD] = ' '; |
| 536 | if (*p == 'c') { /* character */ |
| 537 | p = tmp; |
| 538 | *p = va_arg(ap, int); |
| 539 | /* This takes care of the "%c",0 case */ |
| 540 | len = 1; |
| 541 | goto print_len_set; |
| 542 | } else { /* string */ |
| 543 | p = va_arg(ap, char *); |
| 544 | if (!p) { |
| 545 | p = "(null)"; |
| 546 | preci = 6; |
| 547 | } else { |
| 548 | if (preci < 0) { |
| 549 | preci = INT_MAX; |
| 550 | } |
| 551 | } |
| 552 | len = strnlen(p, preci); |
| 553 | goto print_len_set; |
| 554 | } |
| 555 | #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR |
| 556 | } else if (p-u_spec < 27) { /* floating point */ |
| 557 | #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */ |
| 558 | #if defined(__UCLIBC_HAS_FLOATS__) |
| 559 | struct printf_info info; |
| 560 | if (preci < 0) { |
| 561 | preci = 6; |
| 562 | } |
| 563 | info.width = width; |
| 564 | info.prec = preci; |
| 565 | info.spec = *fmt; |
| 566 | info.pad = flag[FLAG_0_PAD]; |
| 567 | info._flags = 0; |
| 568 | if (flag[FLAG_PLUS] == '+') { |
| 569 | PRINT_INFO_SET_FLAG(&info,showsign); |
| 570 | } else if (flag[FLAG_PLUS] == ' ') { |
| 571 | PRINT_INFO_SET_FLAG(&info,space); |
| 572 | } |
| 573 | if (flag[FLAG_HASH]) { |
| 574 | PRINT_INFO_SET_FLAG(&info,alt); |
| 575 | } |
| 576 | if (flag[FLAG_MINUS_LJUSTIFY]) { |
| 577 | PRINT_INFO_SET_FLAG(&info,left); |
| 578 | } |
| 579 | #if 1 |
| 580 | cnt += _fpmaxtostr(op, |
| 581 | (__fpmax_t) |
| 582 | ((dataargtype == (8 << 8)) |
| 583 | ? va_arg(ap, long double) |
| 584 | : (long double) va_arg(ap, double)), |
| 585 | &info, _fp_out_narrow); |
| 586 | #else |
| 587 | cnt += _fpmaxtostr(op, |
| 588 | (__fpmax_t) |
| 589 | ((lval > 1) |
| 590 | ? va_arg(ap, long double) |
| 591 | : (long double) va_arg(ap, double)), |
| 592 | &info, _fp_out_narrow); |
| 593 | #endif |
| 594 | goto nextfmt; |
| 595 | #elif WANT_FLOAT_ERROR |
| 596 | (void) ((lval > 1) ? va_arg(ap, long double) |
| 597 | : va_arg(ap, double)); /* carry on */ |
| 598 | p = (char *) dbl_err; |
| 599 | #endif /* defined(__UCLIBC_HAS_FLOATS__) */ |
| 600 | } |
| 601 | |
| 602 | #if WANT_GNU_ERRNO |
| 603 | print: |
| 604 | #endif |
| 605 | { /* this used to be printfield */ |
| 606 | /* cheaper than strlen call */ |
| 607 | /* for ( len = 0 ; p[len] ; len++ ) { } */ |
| 608 | len = strnlen(p, SIZE_MAX); |
| 609 | print_len_set: |
| 610 | if ((*p == '-') |
| 611 | #if WANT_GNU_ERRNO |
| 612 | && (*fmt != 'm') |
| 613 | #endif |
| 614 | && (*fmt != 's')) { |
| 615 | flag[FLAG_PLUS] = *p++; |
| 616 | --len; |
| 617 | } |
| 618 | if (flag[FLAG_PLUS]) { |
| 619 | ++len; |
| 620 | ++preci; |
| 621 | if (flag[FLAG_PLUS] == '0') { /* base 16 */ |
| 622 | ++preci; /* account for x or X */ |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (preci >= 0) { |
| 627 | if ((*fmt == 's') |
| 628 | #if WANT_GNU_ERRNO |
| 629 | || (*fmt == 'm') |
| 630 | #endif |
| 631 | ) { |
| 632 | if (len > preci) { |
| 633 | len = preci; |
| 634 | } else { |
| 635 | preci = len; |
| 636 | } |
| 637 | } |
| 638 | preci -= len; |
| 639 | if (preci < 0) { |
| 640 | preci = 0; |
| 641 | } |
| 642 | width -= preci; |
| 643 | } |
| 644 | |
| 645 | width -= len; |
| 646 | if (width < 0) { |
| 647 | width = 0; |
| 648 | } |
| 649 | |
| 650 | if (preci < 0) { |
| 651 | preci = 0; |
| 652 | if (!flag[FLAG_MINUS_LJUSTIFY] |
| 653 | /* && flag[FLAG_PLUS] */ |
| 654 | && (flag[FLAG_0_PAD] == '0')) { |
| 655 | preci = width; |
| 656 | width = 0; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | while (width + len + preci) { |
| 661 | unsigned char ch; |
| 662 | /* right padding || left padding */ |
| 663 | if ((!len && !preci) |
| 664 | || (width && !flag[FLAG_MINUS_LJUSTIFY])) { |
| 665 | ch = ' '; |
| 666 | --width; |
| 667 | } else if (flag[FLAG_PLUS]) { |
| 668 | ch = flag[FLAG_PLUS]; /* sign */ |
| 669 | if (flag[FLAG_PLUS]=='0') { /* base 16 case */ |
| 670 | flag[FLAG_PLUS] = *p++; /* get the x|X */ |
| 671 | } else { |
| 672 | flag[FLAG_PLUS] = '\0'; |
| 673 | } |
| 674 | --len; |
| 675 | } else if (preci) { |
| 676 | ch = '0'; |
| 677 | --preci; |
| 678 | } else { |
| 679 | ch = *p++; /* main field */ |
| 680 | --len; |
| 681 | } |
| 682 | ++cnt; |
| 683 | PUTC(ch, op); |
| 684 | } |
| 685 | } |
| 686 | goto nextfmt; |
| 687 | } |
| 688 | |
| 689 | fmt = fmt0; /* this was an illegal format */ |
| 690 | } |
| 691 | |
| 692 | charout: |
| 693 | ++cnt; |
| 694 | PUTC(*fmt, op); /* normal char out */ |
| 695 | |
| 696 | nextfmt: |
| 697 | ++fmt; |
| 698 | } |
| 699 | |
| 700 | } |
| 701 | |
| 702 | i = (__FERROR_UNLOCKED(op)) ? -1 : cnt; |
| 703 | |
| 704 | __STDIO_STREAM_VALIDATE(op); |
| 705 | |
| 706 | __STDIO_AUTO_THREADUNLOCK(op); |
| 707 | |
| 708 | return i; |
| 709 | } |
| 710 | libc_hidden_def(vfprintf) |