old_vfprintf.c 19 KB

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