old_vfprintf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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 _uintmaxtostr __libc__uintmaxtostr
  120. #define strnlen __strnlen
  121. #define _ISOC99_SOURCE /* for ULLONG primarily... */
  122. #define _GNU_SOURCE /* for strnlen */
  123. #include "_stdio.h"
  124. /* #include <stdio.h> */
  125. #include <stdarg.h>
  126. #include <limits.h>
  127. #include <stdint.h>
  128. #include <string.h>
  129. #include <errno.h>
  130. #include <ctype.h>
  131. #include <bits/uClibc_uintmaxtostr.h>
  132. #define __PRINTF_INFO_NO_BITFIELD
  133. #include <printf.h>
  134. #ifdef __UCLIBC_HAS_THREADS__
  135. #include <pthread.h>
  136. #endif /* __UCLIBC_HAS_THREADS__ */
  137. /* #undef __UCLIBC_HAS_FLOATS__ */
  138. /* #undef WANT_FLOAT_ERROR */
  139. /* #define WANT_FLOAT_ERROR 1 */
  140. /* #define __isdigit(c) (((unsigned int)(c - '0')) < 10) */
  141. #ifdef __UCLIBC_HAS_PRINTF_M_SPEC__
  142. #define WANT_GNU_ERRNO 1
  143. #else
  144. #define WANT_GNU_ERRNO 0
  145. #endif
  146. #undef PUTC
  147. #undef OUTNSTR
  148. #undef _outnstr
  149. #ifdef __STDIO_BUFFERS
  150. #define PUTC(C,F) putc_unlocked((C),(F))
  151. #define OUTNSTR _outnstr
  152. #define _outnstr(stream, string, len) __stdio_fwrite(string, len, stream)
  153. #else /* __STDIO_BUFFERS */
  154. typedef struct {
  155. FILE f;
  156. unsigned char *bufend; /* pointer to 1 past end of buffer */
  157. unsigned char *bufpos;
  158. } __FILE_vsnprintf;
  159. #ifdef __UCLIBC_HAS_FLOATS__
  160. static void _outnstr(FILE *stream, const unsigned char *s, size_t n)
  161. {
  162. __FILE_vsnprintf *f = (__FILE_vsnprintf *) stream;
  163. if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) {
  164. __stdio_fwrite(s, n, &f->f);
  165. } else if (f->bufend > f->bufpos) {
  166. size_t r = f->bufend - f->bufpos;
  167. if (r > n) {
  168. r = n;
  169. }
  170. memcpy(f->bufpos, s, r);
  171. f->bufpos += r;
  172. }
  173. }
  174. #endif
  175. static void putc_unlocked_sprintf(int c, __FILE_vsnprintf *f)
  176. {
  177. if (!__STDIO_STREAM_IS_FAKE_VSNPRINTF_NB(&f->f)) {
  178. putc_unlocked(c, &f->f);
  179. } else if (f->bufpos < f->bufend) {
  180. *f->bufpos++ = c;
  181. }
  182. }
  183. #define PUTC(C,F) putc_unlocked_sprintf((C),(__FILE_vsnprintf *)(F))
  184. #define OUTNSTR _outnstr
  185. #endif /* __STDIO_BUFFERS */
  186. #ifdef __UCLIBC_HAS_FLOATS__
  187. #include <float.h>
  188. #include <bits/uClibc_fpmax.h>
  189. typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  190. intptr_t buf);
  191. extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  192. __fp_outfunc_t fp_outfunc) attribute_hidden;
  193. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  194. {
  195. /* TODO -- Use a buffer to cut down on function calls... */
  196. char pad[1];
  197. *pad = padchar;
  198. while (numpad) {
  199. OUTNSTR(stream, pad, 1);
  200. --numpad;
  201. }
  202. }
  203. static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  204. {
  205. if (type & 0x80) { /* Some type of padding needed. */
  206. int buflen = strlen((const char *) buf);
  207. if ((len -= buflen) > 0) {
  208. _charpad(fp, (type & 0x7f), len);
  209. }
  210. len = buflen;
  211. }
  212. if (len) {
  213. OUTNSTR(fp, (const char *) buf, len);
  214. }
  215. }
  216. #endif
  217. enum {
  218. FLAG_PLUS = 0,
  219. FLAG_MINUS_LJUSTIFY,
  220. FLAG_HASH,
  221. FLAG_0_PAD,
  222. FLAG_SPACE,
  223. };
  224. /* layout 01234 */
  225. static const char spec[] = "+-#0 ";
  226. /**********************************************************************/
  227. extern void _store_inttype(void *dest, int desttype, uintmax_t val) attribute_hidden;
  228. extern uintmax_t _load_inttype(int desttype, const void *src, int uflag) attribute_hidden;
  229. /*
  230. * In order to ease translation to what arginfo and _print_info._flags expect,
  231. * we map: 0:int 1:char 2:longlong 4:long 8:short
  232. * and then _flags |= (((q << 7) + q) & 0x701) and argtype |= (_flags & 0x701)
  233. */
  234. #ifdef PDS
  235. #error PDS already defined!
  236. #endif
  237. #ifdef SS
  238. #error SS already defined!
  239. #endif
  240. #ifdef IMS
  241. #error IMS already defined!
  242. #endif
  243. #if PTRDIFF_MAX == INT_MAX
  244. #define PDS 0
  245. #elif PTRDIFF_MAX == LONG_MAX
  246. #define PDS 4
  247. #elif defined(LLONG_MAX) && (PTRDIFF_MAX == LLONG_MAX)
  248. #define PDS 8
  249. #else
  250. #error fix QUAL_CHARS ptrdiff_t entry 't'!
  251. #endif
  252. #if SIZE_MAX == UINT_MAX
  253. #define SS 0
  254. #elif SIZE_MAX == ULONG_MAX
  255. #define SS 4
  256. #elif defined(LLONG_MAX) && (SIZE_MAX == ULLONG_MAX)
  257. #define SS 8
  258. #else
  259. #error fix QUAL_CHARS size_t entries 'z', 'Z'!
  260. #endif
  261. #if INTMAX_MAX == INT_MAX
  262. #define IMS 0
  263. #elif INTMAX_MAX == LONG_MAX
  264. #define IMS 4
  265. #elif defined(LLONG_MAX) && (INTMAX_MAX == LLONG_MAX)
  266. #define IMS 8
  267. #else
  268. #error fix QUAL_CHARS intmax_t entry 'j'!
  269. #endif
  270. #define QUAL_CHARS { \
  271. /* j:(u)intmax_t z:(s)size_t t:ptrdiff_t \0:int */ \
  272. /* q:long_long Z:(s)size_t */ \
  273. 'h', 'l', 'L', 'j', 'z', 't', 'q', 'Z', 0, \
  274. 2, 4, 8, IMS, SS, PDS, 8, SS, 0, /* TODO -- fix!!! */\
  275. 1, 8 \
  276. }
  277. static const char qual_chars[] = QUAL_CHARS;
  278. /* static const char qual[] = "hlLq"; */
  279. /**********************************************************************/
  280. #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
  281. static const char dbl_err[] = "<DOUBLE>";
  282. #endif
  283. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  284. /* layout 012345678901234567 */
  285. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  286. #else
  287. /* layout 0123456789012 */
  288. static const char u_spec[] = "%nbopxXudics";
  289. #endif
  290. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  291. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  292. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  293. int vfprintf(FILE * __restrict op, register const char * __restrict fmt,
  294. va_list ap)
  295. {
  296. union {
  297. #ifdef LLONG_MAX
  298. long long ll;
  299. #endif
  300. #if LONG_MAX != INT_MAX
  301. long l;
  302. #endif
  303. int i;
  304. } intarg;
  305. int i, cnt, dataargtype, len;
  306. const void *argptr; /* This does not need to be initialized. */
  307. register char *p;
  308. const char *fmt0;
  309. int preci, width;
  310. #define upcase i
  311. int radix, dpoint /*, upcase*/;
  312. char tmp[65]; /* TODO - determing needed size from headers */
  313. char flag[sizeof(spec)];
  314. __STDIO_AUTO_THREADLOCK_VAR;
  315. __STDIO_AUTO_THREADLOCK(op);
  316. __STDIO_STREAM_VALIDATE(op);
  317. cnt = 0;
  318. if (__STDIO_STREAM_IS_NARROW_WRITING(op)
  319. || !__STDIO_STREAM_TRANS_TO_WRITE(op, __FLAG_NARROW)
  320. ) {
  321. while (*fmt) {
  322. if (*fmt == '%') {
  323. fmt0 = fmt; /* save our position in case of bad format */
  324. ++fmt;
  325. width = -1; /* min field width */
  326. preci = -5; /* max string width or mininum digits */
  327. radix = 10; /* number base */
  328. dpoint = 0; /* found decimal point */
  329. /* init flags */
  330. for (p =(char *) spec ; *p ; p++) {
  331. flag[p-spec] = '\0';
  332. }
  333. flag[FLAG_0_PAD] = ' ';
  334. /* process optional flags */
  335. for (p = (char *)spec ; *p ; ) {
  336. if (*fmt == *p) {
  337. flag[p-spec] = *fmt++;
  338. p = (char *)spec; /* restart scan */
  339. } else {
  340. p++;
  341. }
  342. }
  343. if (!flag[FLAG_PLUS]) {
  344. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  345. }
  346. /* process optional width and precision */
  347. do {
  348. if (*fmt == '.') {
  349. ++fmt;
  350. dpoint = 1;
  351. }
  352. if (*fmt == '*') { /* parameter width value */
  353. ++fmt;
  354. i = va_arg(ap, int);
  355. } else {
  356. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  357. i = (i * 10) + (*fmt - '0');
  358. }
  359. }
  360. if (dpoint) {
  361. preci = i;
  362. if (i<0) {
  363. preci = -5;
  364. }
  365. } else {
  366. width = i;
  367. if (i<0) {
  368. width = -i;
  369. flag[FLAG_MINUS_LJUSTIFY] = 1;
  370. }
  371. }
  372. } while ((*fmt == '.') && !dpoint );
  373. /* process optional qualifier */
  374. p = (char *) qual_chars;
  375. do {
  376. if (*fmt == *p) {
  377. ++fmt;
  378. break;
  379. }
  380. } while (*++p);
  381. if ((p - qual_chars < 2) && (*fmt == *p)) {
  382. p += ((sizeof(qual_chars)-2) / 2);
  383. ++fmt;
  384. }
  385. dataargtype = ((int)(p[(sizeof(qual_chars)-2) / 2])) << 8;
  386. #if WANT_GNU_ERRNO
  387. if (*fmt == 'm') {
  388. flag[FLAG_PLUS] = '\0';
  389. flag[FLAG_0_PAD] = ' ';
  390. p = __glibc_strerror_r(errno, tmp, sizeof(tmp));
  391. goto print;
  392. }
  393. #endif
  394. /* process format specifier */
  395. for (p = (char *) u_spec ; *p ; p++) {
  396. if (*fmt != *p) continue;
  397. if (p-u_spec < 1) { /* print a % */
  398. goto charout;
  399. }
  400. if (p-u_spec < 2) { /* store output count in int ptr */
  401. _store_inttype(va_arg(ap, void *),
  402. dataargtype,
  403. (intmax_t) (cnt));
  404. goto nextfmt;
  405. }
  406. if (p-u_spec < 10) {
  407. if (*p == 'p') {
  408. #if INTPTR_MAX == INT_MAX
  409. dataargtype = 0;
  410. #else
  411. #error Fix dataargtype for pointers!
  412. #endif
  413. }
  414. switch(dataargtype) {
  415. case (PA_INT|PA_FLAG_LONG_LONG):
  416. #ifdef LLONG_MAX
  417. intarg.ll = va_arg(ap, long long);
  418. argptr = &intarg.ll;
  419. break;
  420. #endif
  421. case (PA_INT|PA_FLAG_LONG):
  422. #if LONG_MAX != INT_MAX
  423. intarg.l = va_arg(ap, long);
  424. argptr = &intarg.l;
  425. break;
  426. #endif
  427. default:
  428. intarg.i = va_arg(ap, int);
  429. argptr = &intarg.i;
  430. break;
  431. }
  432. }
  433. if (p-u_spec < 8) { /* unsigned conversion */
  434. radix = u_radix[p-u_spec-2];
  435. upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER);
  436. if (*p == 'p') {
  437. upcase = __UIM_LOWER;
  438. flag[FLAG_HASH] = 'p';
  439. }
  440. p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
  441. (uintmax_t)
  442. _load_inttype(dataargtype, argptr, radix),
  443. radix, upcase);
  444. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  445. if (*p != '0') { /* non-zero */
  446. if (flag[FLAG_HASH]) {
  447. if (radix == 8) {
  448. *--p = '0'; /* add leadding zero */
  449. } else if (radix != 10) { /* either 2 or 16 */
  450. flag[FLAG_PLUS] = '0';
  451. *--p = 'b';
  452. if (radix == 16) {
  453. *p = 'x';
  454. if (*fmt == 'X') {
  455. *p = 'X';
  456. }
  457. }
  458. }
  459. }
  460. } else if (flag[FLAG_HASH] == 'p') { /* null pointer */
  461. p = "(nil)";
  462. }
  463. } else if (p-u_spec < 10) { /* signed conversion */
  464. p = _uintmaxtostr(tmp + sizeof(tmp) - 1,
  465. (uintmax_t)
  466. _load_inttype(dataargtype, argptr, -radix),
  467. -radix, upcase);
  468. } else if (p-u_spec < 12) { /* character or string */
  469. flag[FLAG_PLUS] = '\0';
  470. flag[FLAG_0_PAD] = ' ';
  471. if (*p == 'c') { /* character */
  472. p = tmp;
  473. *p = va_arg(ap, int);
  474. /* This takes care of the "%c",0 case */
  475. len = 1;
  476. goto print_len_set;
  477. } else { /* string */
  478. p = va_arg(ap, char *);
  479. if (!p) {
  480. p = "(null)";
  481. preci = 6;
  482. } else {
  483. if (preci < 0) {
  484. preci = INT_MAX;
  485. }
  486. }
  487. len = strnlen(p, preci);
  488. goto print_len_set;
  489. }
  490. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  491. } else if (p-u_spec < 27) { /* floating point */
  492. #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
  493. #if defined(__UCLIBC_HAS_FLOATS__)
  494. struct printf_info info;
  495. if (preci < 0) {
  496. preci = 6;
  497. }
  498. info.width = width;
  499. info.prec = preci;
  500. info.spec = *fmt;
  501. info.pad = flag[FLAG_0_PAD];
  502. info._flags = 0;
  503. if (flag[FLAG_PLUS] == '+') {
  504. PRINT_INFO_SET_FLAG(&info,showsign);
  505. } else if (flag[FLAG_PLUS] == ' ') {
  506. PRINT_INFO_SET_FLAG(&info,space);
  507. }
  508. if (flag[FLAG_HASH]) {
  509. PRINT_INFO_SET_FLAG(&info,alt);
  510. }
  511. if (flag[FLAG_MINUS_LJUSTIFY]) {
  512. PRINT_INFO_SET_FLAG(&info,left);
  513. }
  514. #if 1
  515. cnt += _fpmaxtostr(op,
  516. (__fpmax_t)
  517. ((dataargtype == (8 << 8))
  518. ? va_arg(ap, long double)
  519. : (long double) va_arg(ap, double)),
  520. &info, _fp_out_narrow);
  521. #else
  522. cnt += _fpmaxtostr(op,
  523. (__fpmax_t)
  524. ((lval > 1)
  525. ? va_arg(ap, long double)
  526. : (long double) va_arg(ap, double)),
  527. &info, _fp_out_narrow);
  528. #endif
  529. goto nextfmt;
  530. #elif WANT_FLOAT_ERROR
  531. (void) ((lval > 1) ? va_arg(ap, long double)
  532. : va_arg(ap, double)); /* carry on */
  533. p = (char *) dbl_err;
  534. #endif /* defined(__UCLIBC_HAS_FLOATS__) */
  535. }
  536. #if WANT_GNU_ERRNO
  537. print:
  538. #endif
  539. { /* this used to be printfield */
  540. /* cheaper than strlen call */
  541. /* for ( len = 0 ; p[len] ; len++ ) { } */
  542. len = strnlen(p, SIZE_MAX);
  543. print_len_set:
  544. if ((*p == '-')
  545. #if WANT_GNU_ERRNO
  546. && (*fmt != 'm')
  547. #endif
  548. && (*fmt != 's')) {
  549. flag[FLAG_PLUS] = *p++;
  550. --len;
  551. }
  552. if (flag[FLAG_PLUS]) {
  553. ++len;
  554. ++preci;
  555. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  556. ++preci; /* account for x or X */
  557. }
  558. }
  559. if (preci >= 0) {
  560. if ((*fmt == 's')
  561. #if WANT_GNU_ERRNO
  562. || (*fmt == 'm')
  563. #endif
  564. ) {
  565. if (len > preci) {
  566. len = preci;
  567. } else {
  568. preci = len;
  569. }
  570. }
  571. preci -= len;
  572. if (preci < 0) {
  573. preci = 0;
  574. }
  575. width -= preci;
  576. }
  577. width -= len;
  578. if (width < 0) {
  579. width = 0;
  580. }
  581. if (preci < 0) {
  582. preci = 0;
  583. if (!flag[FLAG_MINUS_LJUSTIFY]
  584. /* && flag[FLAG_PLUS] */
  585. && (flag[FLAG_0_PAD] == '0')) {
  586. preci = width;
  587. width = 0;
  588. }
  589. }
  590. while (width + len + preci) {
  591. unsigned char ch;
  592. /* right padding || left padding */
  593. if ((!len && !preci)
  594. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  595. ch = ' ';
  596. --width;
  597. } else if (flag[FLAG_PLUS]) {
  598. ch = flag[FLAG_PLUS]; /* sign */
  599. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  600. flag[FLAG_PLUS] = *p++; /* get the x|X */
  601. } else {
  602. flag[FLAG_PLUS] = '\0';
  603. }
  604. --len;
  605. } else if (preci) {
  606. ch = '0';
  607. --preci;
  608. } else {
  609. ch = *p++; /* main field */
  610. --len;
  611. }
  612. ++cnt;
  613. PUTC(ch, op);
  614. }
  615. }
  616. goto nextfmt;
  617. }
  618. fmt = fmt0; /* this was an illegal format */
  619. }
  620. charout:
  621. ++cnt;
  622. PUTC(*fmt, op); /* normal char out */
  623. nextfmt:
  624. ++fmt;
  625. }
  626. }
  627. i = (__FERROR_UNLOCKED(op)) ? -1 : cnt;
  628. __STDIO_STREAM_VALIDATE(op);
  629. __STDIO_AUTO_THREADUNLOCK(op);
  630. return i;
  631. }