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 WANT_LONG_LONG 0
  87. #define WANT_DOUBLE 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_DOUBLE_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 | __MODE_WRITE;
  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. #if 0
  250. FILE f = {f.unbuf, f.unbuf, f.unbuf, f.unbuf, f.unbuf + sizeof(f.unbuf),
  251. fd, _IONBF | __MODE_WRITE};
  252. assert(fd >= 0); /* fd==0 may no longer be stdin */
  253. return vfnprintf(&f, -1, fmt, ap);
  254. #else
  255. char buf[BUFSIZ];
  256. FILE f = {buf, buf, buf, buf, buf + sizeof(buf),
  257. fd, _IOFBF | __MODE_WRITE};
  258. int rv;
  259. assert(fd >= 0); /* fd==0 may no longer be stdin */
  260. rv = vfnprintf(&f, -1, fmt, ap);
  261. fflush(&f);
  262. return rv;
  263. #endif
  264. }
  265. #endif
  266. #ifdef L_vfnprintf
  267. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  268. extern char *__ltostr(char *buf, long val, int base, int uppercase);
  269. extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase);
  270. extern char *__lltostr(char *buf, long long val, int base, int uppercase);
  271. extern int __dtostr(FILE * fp, size_t size, double x,
  272. char flag[], int width, int preci, char mode);
  273. enum {
  274. FLAG_PLUS = 0,
  275. FLAG_MINUS_LJUSTIFY,
  276. FLAG_HASH,
  277. FLAG_0_PAD,
  278. FLAG_SPACE,
  279. };
  280. /* layout 01234 */
  281. static const char spec[] = "+-#0 ";
  282. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  283. static const char qual[] = "hlLq";
  284. #else
  285. static const char qual[] = "hl";
  286. #endif
  287. #if !WANT_LONG_LONG && WANT_LONG_LONG_ERROR
  288. static const char ll_err[] = "<LONG-LONG>";
  289. #endif
  290. #if !WANT_DOUBLE && WANT_DOUBLE_ERROR
  291. static const char dbl_err[] = "<DOUBLE>";
  292. #endif
  293. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  294. /* layout 012345678901234567 */
  295. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  296. #else
  297. /* layout 0123456789012 */
  298. static const char u_spec[] = "%nbopxXudics0";
  299. #endif
  300. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  301. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  302. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  303. int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap)
  304. {
  305. int i, cnt = 0, lval;
  306. char *p;
  307. const char *fmt0;
  308. int buffer_mode;
  309. int preci, width;
  310. #define upcase i
  311. int radix, dpoint /*, upcase*/;
  312. #if WANT_LONG_LONG
  313. char tmp[65];
  314. #else
  315. char tmp[33];
  316. #endif
  317. char flag[sizeof(spec)];
  318. /* This speeds things up a bit for unbuffered */
  319. buffer_mode = (op->mode & __MODE_BUF);
  320. op->mode &= (~__MODE_BUF);
  321. while (*fmt) {
  322. if (*fmt == '%') {
  323. fmt0 = fmt; /* save our position in case of bad format */
  324. ++fmt;
  325. if (buffer_mode == _IONBF) {
  326. fflush(op);
  327. }
  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 WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  388. if ((*p == 'l') && (*fmt == *p)) {
  389. ++lval;
  390. ++fmt;
  391. }
  392. #endif /* WANT_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. }
  420. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  421. if (lval >= 2) {
  422. #if WANT_LONG_LONG
  423. p = __ulltostr(tmp + sizeof(tmp) - 1,
  424. va_arg(ap, unsigned long long),
  425. radix, upcase);
  426. #else
  427. (void) va_arg(ap, unsigned long long); /* cary on */
  428. p = (char *) ll_err;
  429. #endif /* WANT_LONG_LONG */
  430. } else {
  431. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  432. #if UINT_MAX != ULONG_MAX
  433. /* sizeof(unsigned int) != sizeof(unsigned long) */
  434. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  435. ((lval)
  436. ? va_arg(ap, unsigned long)
  437. : va_arg(ap, unsigned int)),
  438. radix, upcase);
  439. #else
  440. /* sizeof(unsigned int) == sizeof(unsigned long) */
  441. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  442. va_arg(ap, unsigned long),
  443. radix, upcase);
  444. #endif
  445. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  446. }
  447. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  448. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  449. if (flag[FLAG_HASH] && (*p != '0')) { /* non-zero */
  450. if (radix == 8) {
  451. *--p = '0'; /* add leadding zero */
  452. } else { /* 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 (p-u_spec < 10) { /* signed conversion */
  464. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  465. if (lval >= 2) {
  466. #if WANT_LONG_LONG
  467. p = __lltostr(tmp + sizeof(tmp) - 1,
  468. va_arg(ap, long long), 10, 0);
  469. #else
  470. (void) va_arg(ap, long long); /* carry on */
  471. p = (char *) ll_err;
  472. #endif /* WANT_LONG_LONG */
  473. } else {
  474. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  475. #if INT_MAX != LONG_MAX
  476. /* sizeof(int) != sizeof(long) */
  477. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  478. ((lval)
  479. ? va_arg(ap, long)
  480. : va_arg(ap, int)), 10, 0);
  481. #else
  482. /* sizeof(int) == sizeof(long) */
  483. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  484. va_arg(ap, long), 10, 0);
  485. #endif
  486. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  487. }
  488. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  489. } else if (p-u_spec < 12) { /* character or string */
  490. flag[FLAG_PLUS] = '\0';
  491. flag[FLAG_0_PAD] = ' ';
  492. if (*p == 'c') { /* character */
  493. p = tmp;
  494. *p = va_arg(ap, int);
  495. } else { /* string */
  496. p = va_arg(ap, char *);
  497. }
  498. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  499. } else if (p-u_spec < 27) { /* floating point */
  500. #endif /* WANT_DOUBLE || WANT_DOUBLE_ERROR */
  501. #if WANT_DOUBLE
  502. if (preci < 0) {
  503. preci = 6;
  504. }
  505. cnt += __dtostr(op, max_size, va_arg(ap, double),
  506. flag, width, preci, *fmt);
  507. goto nextfmt;
  508. #elif WANT_DOUBLE_ERROR
  509. (void) va_arg(ap,double); /* carry on */
  510. p = (char *) dbl_err;
  511. #endif /* WANT_DOUBLE */
  512. }
  513. #if WANT_GNU_ERRNO
  514. print:
  515. #endif
  516. { /* this used to be printfield */
  517. int len;
  518. /* cheaper than strlen call */
  519. for ( len = 0 ; p[len] ; len++ ) { }
  520. if ((*p == '-')
  521. #if WANT_GNU_ERRNO
  522. && (*fmt != 'm')
  523. #endif
  524. && (*fmt != 's')) {
  525. flag[FLAG_PLUS] = *p++;
  526. --len;
  527. }
  528. if (flag[FLAG_PLUS]) {
  529. ++len;
  530. ++preci;
  531. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  532. ++preci; /* account for x or X */
  533. }
  534. }
  535. if (preci >= 0) {
  536. if ((*fmt == 's')
  537. #if WANT_GNU_ERRNO
  538. || (*fmt == 'm')
  539. #endif
  540. ) {
  541. len = preci;
  542. }
  543. preci -= len;
  544. if (preci < 0) {
  545. preci = 0;
  546. }
  547. width -= preci;
  548. }
  549. width -= len;
  550. if (width < 0) {
  551. width = 0;
  552. }
  553. if (preci < 0) {
  554. preci = 0;
  555. if (!flag[FLAG_MINUS_LJUSTIFY]
  556. /* && flag[FLAG_PLUS] */
  557. && (flag[FLAG_0_PAD] == '0')) {
  558. preci = width;
  559. width = 0;
  560. }
  561. }
  562. while (width + len + preci) {
  563. unsigned char ch;
  564. /* right padding || left padding */
  565. if ((!len && !preci)
  566. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  567. ch = ' ';
  568. --width;
  569. } else if (flag[FLAG_PLUS]) {
  570. ch = flag[FLAG_PLUS]; /* sign */
  571. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  572. flag[FLAG_PLUS] = *p++; /* get the x|X */
  573. } else {
  574. flag[FLAG_PLUS] = '\0';
  575. }
  576. --len;
  577. } else if (preci) {
  578. ch = '0';
  579. --preci;
  580. } else {
  581. ch = *p++; /* main field */
  582. --len;
  583. }
  584. if (++cnt < max_size) {
  585. putc(ch, op);
  586. }
  587. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  588. fflush(op);
  589. }
  590. }
  591. }
  592. goto nextfmt;
  593. }
  594. fmt = fmt0; /* this was an illegal format */
  595. }
  596. charout:
  597. if (++cnt < max_size) {
  598. putc(*fmt, op); /* normal char out */
  599. }
  600. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  601. fflush(op);
  602. }
  603. nextfmt:
  604. ++fmt;
  605. }
  606. op->mode |= buffer_mode;
  607. if (buffer_mode == _IONBF) {
  608. fflush(op);
  609. }
  610. if (buffer_mode == _IOLBF) {
  611. op->bufwrite = op->bufstart;
  612. }
  613. if (ferror(op)) {
  614. cnt = -1;
  615. }
  616. return (cnt);
  617. }
  618. #endif