printf.c 17 KB

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