printf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. }
  415. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  416. if (lval >= 2) {
  417. #if defined(__UCLIBC_HAS_LONG_LONG__)
  418. p = __ulltostr(tmp + sizeof(tmp) - 1,
  419. va_arg(ap, unsigned long long),
  420. radix, upcase);
  421. #else
  422. (void) va_arg(ap, unsigned long long); /* cary on */
  423. p = (char *) ll_err;
  424. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
  425. } else {
  426. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  427. #if UINT_MAX != ULONG_MAX
  428. /* sizeof(unsigned int) != sizeof(unsigned long) */
  429. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  430. ((lval)
  431. ? va_arg(ap, unsigned long)
  432. : va_arg(ap, unsigned int)),
  433. radix, upcase);
  434. #else
  435. /* sizeof(unsigned int) == sizeof(unsigned long) */
  436. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  437. va_arg(ap, unsigned long),
  438. radix, upcase);
  439. #endif
  440. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  441. }
  442. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  443. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  444. if (flag[FLAG_HASH] && (*p != '0')) { /* non-zero */
  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 (p-u_spec < 10) { /* signed conversion */
  459. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  460. if (lval >= 2) {
  461. #if defined(__UCLIBC_HAS_LONG_LONG__)
  462. p = __lltostr(tmp + sizeof(tmp) - 1,
  463. va_arg(ap, long long), 10, 0);
  464. #else
  465. (void) va_arg(ap, long long); /* carry on */
  466. p = (char *) ll_err;
  467. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) */
  468. } else {
  469. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  470. #if INT_MAX != LONG_MAX
  471. /* sizeof(int) != sizeof(long) */
  472. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  473. ((lval)
  474. ? va_arg(ap, long)
  475. : va_arg(ap, int)), 10, 0);
  476. #else
  477. /* sizeof(int) == sizeof(long) */
  478. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  479. va_arg(ap, long), 10, 0);
  480. #endif
  481. #if defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR
  482. }
  483. #endif /* defined(__UCLIBC_HAS_LONG_LONG__) || WANT_LONG_LONG_ERROR */
  484. } else if (p-u_spec < 12) { /* character or string */
  485. flag[FLAG_PLUS] = '\0';
  486. flag[FLAG_0_PAD] = ' ';
  487. if (*p == 'c') { /* character */
  488. p = tmp;
  489. *p = va_arg(ap, int);
  490. /* This takes care of the "%c",0 case */
  491. len = 1;
  492. goto print_len_set;
  493. } else { /* string */
  494. p = va_arg(ap, char *);
  495. if (!p) {
  496. p = "(null)";
  497. }
  498. }
  499. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  500. } else if (p-u_spec < 27) { /* floating point */
  501. #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
  502. #if defined(__UCLIBC_HAS_FLOATS__)
  503. if (preci < 0) {
  504. preci = 6;
  505. }
  506. cnt += __dtostr(op,
  507. (max_size > cnt ? max_size - cnt : 0),
  508. (long double) ((lval > 1)
  509. ? va_arg(ap, long double)
  510. : va_arg(ap, double)),
  511. flag, width, preci, *fmt);
  512. goto nextfmt;
  513. #elif WANT_FLOAT_ERROR
  514. (void) ((lval > 1) ? va_arg(ap, long double)
  515. : va_arg(ap, double)); /* carry on */
  516. p = (char *) dbl_err;
  517. #endif /* defined(__UCLIBC_HAS_FLOATS__) */
  518. }
  519. #if WANT_GNU_ERRNO
  520. print:
  521. #endif
  522. { /* this used to be printfield */
  523. /* cheaper than strlen call */
  524. for ( len = 0 ; p[len] ; len++ ) { }
  525. print_len_set:
  526. if ((*p == '-')
  527. #if WANT_GNU_ERRNO
  528. && (*fmt != 'm')
  529. #endif
  530. && (*fmt != 's')) {
  531. flag[FLAG_PLUS] = *p++;
  532. --len;
  533. }
  534. if (flag[FLAG_PLUS]) {
  535. ++len;
  536. ++preci;
  537. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  538. ++preci; /* account for x or X */
  539. }
  540. }
  541. if (preci >= 0) {
  542. if ((*fmt == 's')
  543. #if WANT_GNU_ERRNO
  544. || (*fmt == 'm')
  545. #endif
  546. ) {
  547. if (len > preci) {
  548. len = preci;
  549. } else {
  550. preci = len;
  551. }
  552. }
  553. preci -= len;
  554. if (preci < 0) {
  555. preci = 0;
  556. }
  557. width -= preci;
  558. }
  559. width -= len;
  560. if (width < 0) {
  561. width = 0;
  562. }
  563. if (preci < 0) {
  564. preci = 0;
  565. if (!flag[FLAG_MINUS_LJUSTIFY]
  566. /* && flag[FLAG_PLUS] */
  567. && (flag[FLAG_0_PAD] == '0')) {
  568. preci = width;
  569. width = 0;
  570. }
  571. }
  572. while (width + len + preci) {
  573. unsigned char ch;
  574. /* right padding || left padding */
  575. if ((!len && !preci)
  576. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  577. ch = ' ';
  578. --width;
  579. } else if (flag[FLAG_PLUS]) {
  580. ch = flag[FLAG_PLUS]; /* sign */
  581. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  582. flag[FLAG_PLUS] = *p++; /* get the x|X */
  583. } else {
  584. flag[FLAG_PLUS] = '\0';
  585. }
  586. --len;
  587. } else if (preci) {
  588. ch = '0';
  589. --preci;
  590. } else {
  591. ch = *p++; /* main field */
  592. --len;
  593. }
  594. if (++cnt < max_size) {
  595. putc(ch, op);
  596. }
  597. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  598. fflush(op);
  599. }
  600. }
  601. }
  602. goto nextfmt;
  603. }
  604. fmt = fmt0; /* this was an illegal format */
  605. }
  606. charout:
  607. if (++cnt < max_size) {
  608. putc(*fmt, op); /* normal char out */
  609. }
  610. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  611. fflush(op);
  612. }
  613. nextfmt:
  614. ++fmt;
  615. }
  616. op->mode |= buffer_mode;
  617. if (buffer_mode == _IOLBF) {
  618. op->bufwrite = op->bufpos;
  619. }
  620. if (ferror(op)) {
  621. cnt = -1;
  622. }
  623. return (cnt);
  624. }
  625. #endif