printf.c 17 KB

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