old_vfprintf.c 18 KB

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