old_vfprintf.c 18 KB

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