printf.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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. */
  84. /*****************************************************************************/
  85. /* OPTIONS */
  86. /*****************************************************************************/
  87. /* The optional support for long longs and doubles comes in two forms.
  88. *
  89. * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
  90. * Adds about 130 bytes for doubles, about 220 bytes for long longs,
  91. * and about 275 for both to the base code size of 1163 on i386.
  92. */
  93. /* These are now set in uClibc_config.h based on Config. */
  94. /*
  95. #define __UCLIBC_HAS_LONG_LONG__ 1
  96. #define __UCLIBC_HAS_FLOATS__ 1
  97. */
  98. /* 2) An error message is inserted into the stream, an arg of the
  99. * appropriate size is removed from the arglist, and processing
  100. * continues. This is adds less code and may be useful in some
  101. * cases. Set to 1 to turn on. Adds about 50 bytes for doubles,
  102. * about 140 bytes for long longs, and about 175 bytes for both
  103. * to the base code size of 1163 on i386.
  104. */
  105. #define WANT_LONG_LONG_ERROR 0
  106. #define WANT_FLOAT_ERROR 0
  107. /*
  108. * Set to support GNU extension of %m to print string corresponding to errno.
  109. *
  110. * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
  111. * strerror and the corresponding string table which together are about 3.8k.
  112. */
  113. #define WANT_GNU_ERRNO 0
  114. /**************************************************************************/
  115. #include <sys/types.h>
  116. #include <fcntl.h>
  117. #include <string.h>
  118. #include <stdlib.h>
  119. #include <limits.h>
  120. #include <assert.h>
  121. #if WANT_GNU_ERRNO
  122. #include <errno.h>
  123. #endif
  124. #ifdef __STDC__
  125. #include <stdarg.h>
  126. #define va_strt va_start
  127. #else
  128. #include <varargs.h>
  129. #define va_strt(p,i) va_start(p)
  130. #endif
  131. #include "stdio.h"
  132. extern int vfnprintf(FILE * op, size_t max_size,
  133. register __const char *fmt, register va_list ap);
  134. #ifdef L_printf
  135. int printf(const char *fmt, ...)
  136. {
  137. va_list ptr;
  138. int rv;
  139. va_strt(ptr, fmt);
  140. rv = vfnprintf(stdout, -1, fmt, ptr);
  141. va_end(ptr);
  142. return rv;
  143. }
  144. #endif
  145. #ifdef L_asprintf
  146. int asprintf(char **app, const char *fmt, ...)
  147. {
  148. va_list ptr;
  149. int rv;
  150. char *p;
  151. /*
  152. * First iteration - find out size of buffer required and allocate it.
  153. */
  154. va_strt(ptr, fmt);
  155. rv = vsnprintf(NULL, 0, fmt, ptr);
  156. va_end(ptr);
  157. p = malloc(++rv); /* allocate the buffer */
  158. *app = p;
  159. if (!p) {
  160. return -1;
  161. }
  162. /*
  163. * Second iteration - actually produce output.
  164. */
  165. va_strt(ptr, fmt);
  166. rv = vsnprintf(p, rv, fmt, ptr);
  167. va_end(ptr);
  168. return rv;
  169. }
  170. #endif
  171. #ifdef L_sprintf
  172. int sprintf(char *sp, const char *fmt, ...)
  173. {
  174. va_list ptr;
  175. int rv;
  176. va_strt(ptr, fmt);
  177. rv = vsnprintf(sp, -1, fmt, ptr);
  178. va_end(ptr);
  179. return rv;
  180. }
  181. #endif
  182. #ifdef L_snprintf
  183. int snprintf(char *sp, size_t size, const char *fmt, ...)
  184. {
  185. va_list ptr;
  186. int rv;
  187. va_strt(ptr, fmt);
  188. rv = vsnprintf(sp, size, fmt, ptr);
  189. va_end(ptr);
  190. return rv;
  191. }
  192. #endif
  193. #ifdef L_fprintf
  194. int fprintf(FILE * fp, const char *fmt, ...)
  195. {
  196. va_list ptr;
  197. int rv;
  198. va_strt(ptr, fmt);
  199. rv = vfnprintf(fp, -1, fmt, ptr);
  200. va_end(ptr);
  201. return rv;
  202. }
  203. #endif
  204. #ifdef L_fnprintf
  205. int fnprintf(FILE * fp, size_t size, const char *fmt, ...)
  206. {
  207. va_list ptr;
  208. int rv;
  209. va_strt(ptr, fmt);
  210. rv = vfnprintf(fp, size, fmt, ptr);
  211. va_end(ptr);
  212. return rv;
  213. }
  214. #endif
  215. #ifdef L_vprintf
  216. int vprintf(const char *fmt, va_list ap)
  217. {
  218. return vfprintf(stdout, fmt, ap);
  219. }
  220. #endif
  221. #ifdef L_vfprintf
  222. int vfprintf(FILE * op, register __const char *fmt, register va_list ap)
  223. {
  224. return vfnprintf(op, -1, fmt, ap);
  225. }
  226. #endif
  227. #ifdef L_vsprintf
  228. int vsprintf(char *sp, __const char *fmt, va_list ap)
  229. {
  230. return vsnprintf(sp, -1, fmt, ap);
  231. }
  232. #endif
  233. #ifdef L_vsnprintf
  234. int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap)
  235. {
  236. int rv;
  237. FILE f;
  238. /*
  239. * As we're only using the putc macro in vfnprintf, we don't need to
  240. * initialize all FILE f's fields.
  241. */
  242. f.bufwrite = (char *) ((unsigned) -1);
  243. f.bufpos = sp;
  244. f.mode = _IOFBF;
  245. rv = vfnprintf(&f, size, fmt, ap);
  246. if (size) { /* If this is going to a buffer, */
  247. *(f.bufpos) = 0; /* don't forget to nul-terminate. */
  248. }
  249. return rv;
  250. }
  251. #endif
  252. #ifdef L_vdprintf
  253. /*
  254. * Note: If fd has an associated buffered FILE, bad things happen.
  255. */
  256. extern int vdprintf(int fd, const char *fmt, va_list ap)
  257. {
  258. char buf[BUFSIZ];
  259. FILE f = {buf, 0, buf+sizeof(buf), buf, buf+sizeof(buf), 0, fd, _IOFBF};
  260. int rv;
  261. rv = vfnprintf(&f, -1, fmt, ap);
  262. if (fflush(&f)) {
  263. return -1;
  264. }
  265. return rv;
  266. }
  267. #endif
  268. #ifdef L_vfnprintf
  269. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  270. extern char *__ltostr(char *buf, long val, int base, int uppercase);
  271. extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase);
  272. extern char *__lltostr(char *buf, long long val, int base, int uppercase);
  273. extern int __dtostr(FILE * fp, size_t size, long double x,
  274. char flag[], int width, int preci, char mode);
  275. enum {
  276. FLAG_PLUS = 0,
  277. FLAG_MINUS_LJUSTIFY,
  278. FLAG_HASH,
  279. FLAG_0_PAD,
  280. FLAG_SPACE,
  281. };
  282. /* layout 01234 */
  283. static const char spec[] = "+-#0 ";
  284. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  285. static const char qual[] = "hlLq";
  286. #else
  287. static const char qual[] = "hl";
  288. #endif
  289. #if !defined(__UCLIBC_HAS_LONG_LONG__) && WANT_LONG_LONG_ERROR
  290. static const char ll_err[] = "<LONG-LONG>";
  291. #endif
  292. #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
  293. static const char dbl_err[] = "<DOUBLE>";
  294. #endif
  295. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  296. /* layout 012345678901234567 */
  297. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  298. #else
  299. /* layout 0123456789012 */
  300. static const char u_spec[] = "%nbopxXudics";
  301. #endif
  302. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  303. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  304. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  305. int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap)
  306. {
  307. int i, cnt, lval, len;
  308. char *p;
  309. const char *fmt0;
  310. int buffer_mode;
  311. int preci, width;
  312. #define upcase i
  313. int radix, dpoint /*, upcase*/;
  314. #if defined(__UCLIBC_HAS_LONG_LONG__)
  315. char tmp[65];
  316. #else
  317. char tmp[33];
  318. #endif
  319. char flag[sizeof(spec)];
  320. cnt = 0;
  321. /* This speeds things up a bit for line unbuffered */
  322. buffer_mode = (op->mode & __MODE_BUF);
  323. op->mode &= (~__MODE_BUF);
  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. #if INT_MAX != LONG_MAX
  333. lval = 0; /* sizeof(int) != sizeof(long) */
  334. #else
  335. lval = 1; /* sizeof(int) == sizeof(long) */
  336. #endif
  337. tmp[1] = 0; /* set things up for %c -- better done here */
  338. /* init flags */
  339. for (p =(char *) spec ; *p ; p++) {
  340. flag[p-spec] = '\0';
  341. }
  342. flag[FLAG_0_PAD] = ' ';
  343. /* process optional flags */
  344. for (p = (char *)spec ; *p ; ) {
  345. if (*fmt == *p) {
  346. flag[p-spec] = *fmt++;
  347. p = (char *)spec; /* restart scan */
  348. } else {
  349. p++;
  350. }
  351. }
  352. if (!flag[FLAG_PLUS]) {
  353. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  354. }
  355. /* process optional width and precision */
  356. do {
  357. if (*fmt == '.') {
  358. ++fmt;
  359. dpoint = 1;
  360. }
  361. if (*fmt == '*') { /* parameter width value */
  362. ++fmt;
  363. i = va_arg(ap, int);
  364. } else {
  365. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  366. i = (i * 10) + (*fmt - '0');
  367. }
  368. }
  369. if (dpoint) {
  370. preci = i;
  371. if (i<0) {
  372. preci = -5;
  373. }
  374. } else {
  375. width = i;
  376. if (i<0) {
  377. width = -i;
  378. flag[FLAG_MINUS_LJUSTIFY] = 1;
  379. }
  380. }
  381. } while ((*fmt == '.') && !dpoint );
  382. /* process optional qualifier */
  383. for (p = (char *) qual ; *p ; p++) {
  384. if (*p == *fmt) {
  385. lval = p - qual;
  386. ++fmt;
  387. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  388. if ((*p == 'l') && (*fmt == *p)) {
  389. ++lval;
  390. ++fmt;
  391. }
  392. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  393. }
  394. }
  395. #if WANT_GNU_ERRNO
  396. if (*fmt == 'm') {
  397. flag[FLAG_PLUS] = '\0';
  398. flag[FLAG_0_PAD] = ' ';
  399. p = strerror(errno);
  400. goto print;
  401. }
  402. #endif
  403. /* process format specifier */
  404. for (p = (char *) u_spec ; *p ; p++) {
  405. if (*fmt != *p) continue;
  406. if (p-u_spec < 1) { /* print a % */
  407. goto charout;
  408. }
  409. if (p-u_spec < 2) { /* store output count in int ptr */
  410. *(va_arg(ap, int *)) = cnt;
  411. goto nextfmt;
  412. }
  413. if (p-u_spec < 8) { /* unsigned conversion */
  414. radix = u_radix[p-u_spec-2];
  415. upcase = ((int)'x') - *p;
  416. if (*p == 'p') {
  417. lval = (sizeof(char *) == sizeof(long));
  418. upcase = 0;
  419. flag[FLAG_HASH] = 'p';
  420. }
  421. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  422. if (lval >= 2) {
  423. #if defined(__UCLIBC_HAS_LONG_LONG__)
  424. p = __ulltostr(tmp + sizeof(tmp) - 1,
  425. va_arg(ap, unsigned long long),
  426. radix, upcase);
  427. #else
  428. (void) va_arg(ap, unsigned long long); /* cary on */
  429. p = (char *) ll_err;
  430. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
  431. } else {
  432. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  433. #if UINT_MAX != ULONG_MAX
  434. /* sizeof(unsigned int) != sizeof(unsigned long) */
  435. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  436. ((lval)
  437. ? va_arg(ap, unsigned long)
  438. : va_arg(ap, unsigned int)),
  439. radix, upcase);
  440. #else
  441. /* sizeof(unsigned int) == sizeof(unsigned long) */
  442. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  443. va_arg(ap, unsigned long),
  444. radix, upcase);
  445. #endif
  446. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  447. }
  448. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  449. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  450. if (*p != '0') { /* non-zero */
  451. if (flag[FLAG_HASH]) {
  452. if (radix == 8) {
  453. *--p = '0'; /* add leadding zero */
  454. } else if (radix != 10) { /* either 2 or 16 */
  455. flag[FLAG_PLUS] = '0';
  456. *--p = 'b';
  457. if (radix == 16) {
  458. *p = 'x';
  459. if (*fmt == 'X') {
  460. *p = 'X';
  461. }
  462. }
  463. }
  464. }
  465. } else if (flag[FLAG_HASH] == 'p') { /* null pointer */
  466. p = "(nil)";
  467. }
  468. } else if (p-u_spec < 10) { /* signed conversion */
  469. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  470. if (lval >= 2) {
  471. #if defined(__UCLIBC_HAS_LONG_LONG__)
  472. p = __lltostr(tmp + sizeof(tmp) - 1,
  473. va_arg(ap, long long), 10, 0);
  474. #else
  475. (void) va_arg(ap, long long); /* carry on */
  476. p = (char *) ll_err;
  477. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
  478. } else {
  479. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  480. #if INT_MAX != LONG_MAX
  481. /* sizeof(int) != sizeof(long) */
  482. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  483. ((lval)
  484. ? va_arg(ap, long)
  485. : va_arg(ap, int)), 10, 0);
  486. #else
  487. /* sizeof(int) == sizeof(long) */
  488. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  489. va_arg(ap, long), 10, 0);
  490. #endif
  491. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  492. }
  493. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  494. } else if (p-u_spec < 12) { /* character or string */
  495. flag[FLAG_PLUS] = '\0';
  496. flag[FLAG_0_PAD] = ' ';
  497. if (*p == 'c') { /* character */
  498. p = tmp;
  499. *p = va_arg(ap, int);
  500. /* This takes care of the "%c",0 case */
  501. len = 1;
  502. goto print_len_set;
  503. } else { /* string */
  504. p = va_arg(ap, char *);
  505. if (!p) {
  506. p = "(null)";
  507. }
  508. }
  509. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  510. } else if (p-u_spec < 27) { /* floating point */
  511. #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
  512. #if defined(__UCLIBC_HAS_FLOATS__)
  513. if (preci < 0) {
  514. preci = 6;
  515. }
  516. cnt += __dtostr(op,
  517. (max_size > cnt ? max_size - cnt : 0),
  518. (long double) ((lval > 1)
  519. ? va_arg(ap, long double)
  520. : va_arg(ap, double)),
  521. flag, width, preci, *fmt);
  522. goto nextfmt;
  523. #elif WANT_FLOAT_ERROR
  524. (void) ((lval > 1) ? va_arg(ap, long double)
  525. : va_arg(ap, double)); /* carry on */
  526. p = (char *) dbl_err;
  527. #endif /* defined(__UCLIBC_HAS_FLOATS__) */
  528. }
  529. #if WANT_GNU_ERRNO
  530. print:
  531. #endif
  532. { /* this used to be printfield */
  533. /* cheaper than strlen call */
  534. for ( len = 0 ; p[len] ; len++ ) { }
  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. if (++cnt < max_size) {
  605. putc(ch, op);
  606. }
  607. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  608. fflush(op);
  609. }
  610. }
  611. }
  612. goto nextfmt;
  613. }
  614. fmt = fmt0; /* this was an illegal format */
  615. }
  616. charout:
  617. if (++cnt < max_size) {
  618. putc(*fmt, op); /* normal char out */
  619. }
  620. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  621. fflush(op);
  622. }
  623. nextfmt:
  624. ++fmt;
  625. }
  626. op->mode |= buffer_mode;
  627. if (buffer_mode == _IOLBF) {
  628. op->bufwrite = op->bufpos;
  629. }
  630. if (ferror(op)) {
  631. cnt = -1;
  632. }
  633. return (cnt);
  634. }
  635. #endif