printf.c 17 KB

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