stdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Copyright (C) 1996 Robert de Bath <rdebath@cix.compulink.co.uk>
  2. * This file is part of the Linux-8086 C library and is distributed
  3. * under the GNU Library General Public License.
  4. */
  5. /* This is an implementation of the C standard IO package.
  6. *
  7. * Updates:
  8. * 29-Sep-2000 W. Greathouse 1. fgetc copying beyond end of buffer
  9. * 2. stdout needs flushed when input requested on
  10. * stdin.
  11. * 3. bufend was set incorrectly to 4 bytes beyond
  12. * bufin (sizeof a pointer) instead of BUFSIZ.
  13. * This resulted in 4 byte buffers for full
  14. * buffered stdin and stdout and an 8 byte
  15. * buffer for the unbuffered stderr!
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <malloc.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #undef STUB_FWRITE
  26. extern FILE *__IO_list; /* For fflush at exit */
  27. #define FIXED_BUFFERS 2
  28. struct fixed_buffer {
  29. unsigned char data[BUFSIZ];
  30. unsigned char used;
  31. };
  32. extern void __init_stdio(void);
  33. extern struct fixed_buffer _fixed_buffers[FIXED_BUFFERS];
  34. extern unsigned char *_alloc_stdio_buffer(size_t size);
  35. extern void _free_stdio_buffer(unsigned char *buf);
  36. #ifdef L__alloc_stdio_buffer
  37. unsigned char *_alloc_stdio_buffer(size_t size)
  38. {
  39. if (size == BUFSIZ) {
  40. int i;
  41. for (i = 0; i < FIXED_BUFFERS; i++)
  42. if (!_fixed_buffers[i].used) {
  43. _fixed_buffers[i].used = 1;
  44. return _fixed_buffers[i].data;
  45. }
  46. }
  47. return malloc(size);
  48. }
  49. #endif
  50. #ifdef L__free_stdio_buffer
  51. void _free_stdio_buffer(unsigned char *buf)
  52. {
  53. int i;
  54. for (i = 0; i < FIXED_BUFFERS; i++) {
  55. if (buf == _fixed_buffers[i].data) {
  56. _fixed_buffers[i].used = 0;
  57. return;
  58. }
  59. }
  60. free(buf);
  61. }
  62. #endif
  63. #ifdef L__stdio_init
  64. #if FIXED_BUFFERS < 2
  65. #error FIXED_BUFFERS must be >= 2
  66. #endif
  67. #define bufin (_fixed_buffers[0].data)
  68. #define bufout (_fixed_buffers[1].data)
  69. #define buferr (_stdio_streams[2].unbuf) /* Stderr is unbuffered */
  70. struct fixed_buffer _fixed_buffers[FIXED_BUFFERS];
  71. FILE _stdio_streams[3] = {
  72. {bufin, bufin, bufin, bufin, bufin + BUFSIZ,
  73. 0, _IOFBF | __MODE_READ | __MODE_IOTRAN | __MODE_FREEBUF},
  74. {bufout, bufout, bufout, bufout, bufout + BUFSIZ,
  75. 1, _IOFBF | __MODE_WRITE | __MODE_IOTRAN | __MODE_FREEBUF},
  76. {buferr, buferr, buferr, buferr, buferr + sizeof(buferr),
  77. 2, _IONBF | __MODE_WRITE | __MODE_IOTRAN}
  78. };
  79. /*
  80. * Note: the following forces linking of the __init_stdio function if
  81. * any of the stdio functions are used (except perror) since they all
  82. * call fflush directly or indirectly.
  83. */
  84. FILE *__IO_list = 0; /* For fflush at exit */
  85. /* Call the stdio initiliser; it's main job it to call atexit */
  86. void __stdio_close_all(void)
  87. {
  88. FILE *fp;
  89. fflush(stdout);
  90. fflush(stderr);
  91. for (fp = __IO_list; fp; fp = fp->next) {
  92. fflush(fp);
  93. close(fp->fd);
  94. /* Note we're not de-allocating the memory */
  95. /* There doesn't seem to be much point :-) */
  96. fp->fd = -1;
  97. }
  98. }
  99. void __init_stdio(void)
  100. {
  101. static int stdio_initialized = 0;
  102. #if FIXED_BUFFERS > 2
  103. int i;
  104. #endif
  105. if (stdio_initialized!=0)
  106. return;
  107. stdio_initialized++;
  108. #if FIXED_BUFFERS > 2
  109. for ( i = 2 ; i < FIXED_BUFFERS ; i++ ) {
  110. _fixed_buffers[i].used = 0;
  111. }
  112. #endif
  113. _fixed_buffers[0].used = 1;
  114. _fixed_buffers[1].used = 1;
  115. if (isatty(1)) {
  116. stdout->mode |= _IOLBF;
  117. }
  118. atexit(__stdio_close_all);
  119. }
  120. #endif
  121. #ifdef L_fputc
  122. int fputc(ch, fp)
  123. int ch;
  124. FILE *fp;
  125. {
  126. register int v;
  127. __init_stdio();
  128. v = fp->mode;
  129. /* If last op was a read ... */
  130. if ((v & __MODE_READING) && fflush(fp))
  131. return EOF;
  132. /* Can't write or there's been an EOF or error then return EOF */
  133. if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE)
  134. return EOF;
  135. /* In MSDOS translation mode */
  136. #if __MODE_IOTRAN
  137. if (ch == '\n' && (v & __MODE_IOTRAN) && fputc('\r', fp) == EOF)
  138. return EOF;
  139. #endif
  140. /* Buffer is full */
  141. if (fp->bufpos >= fp->bufend && fflush(fp))
  142. return EOF;
  143. /* Right! Do it! */
  144. *(fp->bufpos++) = ch;
  145. fp->mode |= __MODE_WRITING;
  146. /* Unbuffered or Line buffered and end of line */
  147. if (((ch == '\n' && (v & _IOLBF)) || (v & _IONBF))
  148. && fflush(fp))
  149. return EOF;
  150. /* Can the macro handle this by itself ? */
  151. if (v & (__MODE_IOTRAN | _IOLBF | _IONBF))
  152. fp->bufwrite = fp->bufstart; /* Nope */
  153. else
  154. fp->bufwrite = fp->bufend; /* Yup */
  155. /* Correct return val */
  156. return (unsigned char) ch;
  157. }
  158. #endif
  159. #ifdef L_fgetc
  160. int fgetc(fp)
  161. FILE *fp;
  162. {
  163. int ch;
  164. __init_stdio();
  165. if (fp->mode & __MODE_WRITING)
  166. fflush(fp);
  167. if ( (fp == stdin) && (stdout->fd != -1) && (stdout->mode & __MODE_WRITING) )
  168. fflush(stdout);
  169. #if __MODE_IOTRAN
  170. try_again:
  171. #endif
  172. /* Can't read or there's been an EOF or error then return EOF */
  173. if ((fp->mode & (__MODE_READ | __MODE_EOF | __MODE_ERR)) !=
  174. __MODE_READ) return EOF;
  175. /* Nothing in the buffer - fill it up */
  176. if (fp->bufpos >= fp->bufread) {
  177. fp->bufpos = fp->bufread = fp->bufstart;
  178. ch = fread(fp->bufpos, 1, fp->bufend - fp->bufstart, fp);
  179. if (ch == 0)
  180. return EOF;
  181. fp->bufread += ch;
  182. fp->mode |= __MODE_READING;
  183. fp->mode &= ~__MODE_UNGOT;
  184. }
  185. ch = *(fp->bufpos++);
  186. #if __MODE_IOTRAN
  187. /* In MSDOS translation mode; WARN: Doesn't work with UNIX macro */
  188. if (ch == '\r' && (fp->mode & __MODE_IOTRAN))
  189. goto try_again;
  190. #endif
  191. return ch;
  192. }
  193. #endif
  194. #ifdef L_fflush
  195. int fflush(fp)
  196. FILE *fp;
  197. {
  198. int len, cc, rv = 0;
  199. char *bstart;
  200. __init_stdio();
  201. if (fp == NULL) { /* On NULL flush the lot. */
  202. if (fflush(stdin))
  203. return EOF;
  204. if (fflush(stdout))
  205. return EOF;
  206. if (fflush(stderr))
  207. return EOF;
  208. for (fp = __IO_list; fp; fp = fp->next)
  209. if (fflush(fp))
  210. return EOF;
  211. return 0;
  212. }
  213. /* If there's output data pending */
  214. if (fp->mode & __MODE_WRITING) {
  215. len = fp->bufpos - fp->bufstart;
  216. if (len) {
  217. bstart = fp->bufstart;
  218. /*
  219. * The loop is so we don't get upset by signals or partial writes.
  220. */
  221. do {
  222. cc = write(fp->fd, bstart, len);
  223. if (cc > 0) {
  224. bstart += cc;
  225. len -= cc;
  226. }
  227. }
  228. while (cc > 0 || (cc == -1 && errno == EINTR));
  229. /*
  230. * If we get here with len!=0 there was an error, exactly what to
  231. * do about it is another matter ...
  232. *
  233. * I'll just clear the buffer.
  234. */
  235. if (len) {
  236. fp->mode |= __MODE_ERR;
  237. rv = EOF;
  238. }
  239. }
  240. }
  241. /* If there's data in the buffer sychronise the file positions */
  242. else if (fp->mode & __MODE_READING) {
  243. /* Humm, I think this means sync the file like fpurge() ... */
  244. /* Anyway the user isn't supposed to call this function when reading */
  245. len = fp->bufread - fp->bufpos; /* Bytes buffered but unread */
  246. /* If it's a file, make it good */
  247. if (len > 0 && lseek(fp->fd, (long) -len, 1) < 0) {
  248. /* Hummm - Not certain here, I don't think this is reported */
  249. /*
  250. * fp->mode |= __MODE_ERR; return EOF;
  251. */
  252. }
  253. }
  254. /* All done, no problem */
  255. fp->mode &=
  256. (~(__MODE_READING | __MODE_WRITING | __MODE_EOF | __MODE_UNGOT));
  257. fp->bufread = fp->bufwrite = fp->bufpos = fp->bufstart;
  258. return rv;
  259. }
  260. #endif
  261. #ifdef L_fgets
  262. /* Nothing special here ... */
  263. char *fgets(s, count, f)
  264. char *s;
  265. int count;
  266. FILE *f;
  267. {
  268. char *ret;
  269. register size_t i;
  270. register int ch;
  271. __init_stdio();
  272. ret = s;
  273. for (i = count-1; i > 0; i--) {
  274. ch = getc(f);
  275. if (ch == EOF) {
  276. if (s == ret)
  277. return 0;
  278. break;
  279. }
  280. *s++ = (char) ch;
  281. if (ch == '\n')
  282. break;
  283. }
  284. *s = 0;
  285. if (ferror(f))
  286. return 0;
  287. return ret;
  288. }
  289. #endif
  290. #ifdef L_gets
  291. char *gets(str) /* BAD function; DON'T use it! */
  292. char *str;
  293. {
  294. /* Auwlright it will work but of course _your_ program will crash */
  295. /* if it's given a too long line */
  296. register char *p = str;
  297. register int c;
  298. __init_stdio();
  299. while (((c = getc(stdin)) != EOF) && (c != '\n'))
  300. *p++ = c;
  301. *p = '\0';
  302. return (((c == EOF) && (p == str)) ? NULL : str); /* NULL == EOF */
  303. }
  304. #endif
  305. #ifdef L_fputs
  306. int fputs(str, fp)
  307. const char *str;
  308. FILE *fp;
  309. {
  310. register int n = 0;
  311. __init_stdio();
  312. while (*str) {
  313. if (putc(*str++, fp) == EOF)
  314. return (EOF);
  315. ++n;
  316. }
  317. return (n);
  318. }
  319. #endif
  320. #ifdef L_puts
  321. int puts(str)
  322. const char *str;
  323. {
  324. register int n;
  325. __init_stdio();
  326. if (((n = fputs(str, stdout)) == EOF)
  327. || (putc('\n', stdout) == EOF))
  328. return (EOF);
  329. return (++n);
  330. }
  331. #endif
  332. #ifdef L_fread
  333. /*
  334. * fread will often be used to read in large chunks of data calling read()
  335. * directly can be a big win in this case. Beware also fgetc calls this
  336. * function to fill the buffer.
  337. *
  338. * This ignores __MODE__IOTRAN; probably exactly what you want. (It _is_ what
  339. * fgetc wants)
  340. */
  341. size_t fread(buf, size, nelm, fp)
  342. void *buf;
  343. size_t size;
  344. size_t nelm;
  345. FILE *fp;
  346. {
  347. int len, v;
  348. unsigned bytes, got = 0;
  349. __init_stdio();
  350. v = fp->mode;
  351. /* Want to do this to bring the file pointer up to date */
  352. if (v & __MODE_WRITING)
  353. fflush(fp);
  354. /* Can't read or there's been an EOF or error then return zero */
  355. if ((v & (__MODE_READ | __MODE_EOF | __MODE_ERR)) != __MODE_READ)
  356. return 0;
  357. /* This could be long, doesn't seem much point tho */
  358. bytes = size * nelm;
  359. len = fp->bufread - fp->bufpos;
  360. if (len >= bytes) { /* Enough buffered */
  361. memcpy(buf, fp->bufpos, (unsigned) bytes);
  362. fp->bufpos += bytes;
  363. return bytes;
  364. } else if (len > 0) { /* Some buffered */
  365. memcpy(buf, fp->bufpos, len);
  366. got = len;
  367. }
  368. /* Need more; do it with a direct read */
  369. len = read(fp->fd, buf + got, (unsigned) (bytes - got));
  370. /* Possibly for now _or_ later */
  371. if (len < 0) {
  372. fp->mode |= __MODE_ERR;
  373. len = 0;
  374. } else if (len == 0)
  375. fp->mode |= __MODE_EOF;
  376. return (got + len) / size;
  377. }
  378. #endif
  379. #ifdef L_fwrite
  380. /*
  381. * Like fread, fwrite will often be used to write out large chunks of
  382. * data; calling write() directly can be a big win in this case.
  383. *
  384. * But first we check to see if there's space in the buffer.
  385. *
  386. * Again this ignores __MODE__IOTRAN.
  387. */
  388. size_t fwrite(buf, size, nelm, fp)
  389. const void *buf;
  390. size_t size;
  391. size_t nelm;
  392. FILE *fp;
  393. {
  394. register int v;
  395. int len;
  396. unsigned bytes, put;
  397. __init_stdio();
  398. #ifdef STUB_FWRITE
  399. bytes = size * nelm;
  400. while (bytes > 0) {
  401. len = write(fp->fd, buf, bytes);
  402. if (len <= 0) {
  403. break;
  404. }
  405. bytes -= len;
  406. buf += len;
  407. }
  408. return nelm;
  409. #else
  410. v = fp->mode;
  411. /* If last op was a read ... */
  412. if ((v & __MODE_READING) && fflush(fp))
  413. return 0;
  414. /* Can't write or there's been an EOF or error then return 0 */
  415. if ((v & (__MODE_WRITE | __MODE_EOF | __MODE_ERR)) != __MODE_WRITE)
  416. return 0;
  417. /* This could be long, doesn't seem much point tho */
  418. bytes = size * nelm;
  419. len = fp->bufend - fp->bufpos;
  420. /* Flush the buffer if not enough room */
  421. if (bytes > len)
  422. if (fflush(fp))
  423. return 0;
  424. len = fp->bufend - fp->bufpos;
  425. if (bytes <= len) { /* It'll fit in the buffer ? */
  426. fp->mode |= __MODE_WRITING;
  427. memcpy(fp->bufpos, buf, bytes);
  428. fp->bufpos += bytes;
  429. /* If we're not fully buffered */
  430. if (v & (_IOLBF | _IONBF))
  431. fflush(fp);
  432. return nelm;
  433. } else
  434. /* Too big for the buffer */
  435. {
  436. put = bytes;
  437. do {
  438. len = write(fp->fd, buf, bytes);
  439. if (len > 0) {
  440. buf += len;
  441. bytes -= len;
  442. }
  443. }
  444. while (len > 0 || (len == -1 && errno == EINTR));
  445. if (len < 0)
  446. fp->mode |= __MODE_ERR;
  447. put -= bytes;
  448. }
  449. return put / size;
  450. #endif
  451. }
  452. #endif
  453. #ifdef L_rewind
  454. void rewind(fp)
  455. FILE *fp;
  456. {
  457. __init_stdio();
  458. fseek(fp, (long) 0, 0);
  459. clearerr(fp);
  460. }
  461. #endif
  462. #ifdef L_fseek
  463. int fseek(fp, offset, ref)
  464. FILE *fp;
  465. long offset;
  466. int ref;
  467. {
  468. #if 0
  469. /* FIXME: this is broken! BROKEN!!!! */
  470. /* if __MODE_READING and no ungetc ever done can just move pointer */
  471. /* This needs testing! */
  472. if ((fp->mode & (__MODE_READING | __MODE_UNGOT)) == __MODE_READING &&
  473. (ref == SEEK_SET || ref == SEEK_CUR)) {
  474. long fpos = lseek(fp->fd, 0L, SEEK_CUR);
  475. if (fpos == -1)
  476. return EOF;
  477. if (ref == SEEK_CUR) {
  478. ref = SEEK_SET;
  479. offset = fpos + offset + fp->bufpos - fp->bufread;
  480. }
  481. if (ref == SEEK_SET) {
  482. if (offset < fpos
  483. && offset >= fpos + fp->bufstart - fp->bufread) {
  484. fp->bufpos = offset - fpos + fp->bufread;
  485. return 0;
  486. }
  487. }
  488. }
  489. #endif
  490. /* Use fflush to sync the pointers */
  491. if (fflush(fp) == EOF)
  492. return EOF;
  493. if (lseek(fp->fd, offset, ref) < 0)
  494. return EOF;
  495. return 0;
  496. }
  497. #endif
  498. #ifdef L_ftell
  499. long ftell(fp)
  500. FILE *fp;
  501. {
  502. if (fflush(fp) == EOF)
  503. return EOF;
  504. return lseek(fp->fd, 0L, SEEK_CUR);
  505. }
  506. #endif
  507. #ifdef L_fopen
  508. /*
  509. * This Fopen is all three of fopen, fdopen and freopen. The macros in
  510. * stdio.h show the other names.
  511. */
  512. FILE *__fopen(fname, fd, fp, mode)
  513. const char *fname;
  514. int fd;
  515. FILE *fp;
  516. const char *mode;
  517. {
  518. int open_mode = 0;
  519. #if __MODE_IOTRAN
  520. int do_iosense = 1;
  521. #endif
  522. int fopen_mode = 0;
  523. FILE *nfp = 0;
  524. __init_stdio();
  525. /* If we've got an fp close the old one (freopen) */
  526. if (fp) {
  527. /* Careful, don't de-allocate it */
  528. fopen_mode |=
  529. (fp->mode & (__MODE_BUF | __MODE_FREEFIL | __MODE_FREEBUF));
  530. fp->mode &= ~(__MODE_FREEFIL | __MODE_FREEBUF);
  531. fclose(fp);
  532. }
  533. /* decode the new open mode */
  534. while (*mode)
  535. switch (*mode++) {
  536. case 'r':
  537. fopen_mode |= __MODE_READ;
  538. break;
  539. case 'w':
  540. fopen_mode |= __MODE_WRITE;
  541. open_mode = (O_CREAT | O_TRUNC);
  542. break;
  543. case 'a':
  544. fopen_mode |= __MODE_WRITE;
  545. open_mode = (O_CREAT | O_APPEND);
  546. break;
  547. case '+':
  548. fopen_mode |= __MODE_RDWR;
  549. break;
  550. #if __MODE_IOTRAN
  551. case 'b': /* Binary */
  552. fopen_mode &= ~__MODE_IOTRAN;
  553. do_iosense = 0;
  554. break;
  555. case 't': /* Text */
  556. fopen_mode |= __MODE_IOTRAN;
  557. do_iosense = 0;
  558. break;
  559. #endif
  560. }
  561. /* Add in the read/write options to mode for open() */
  562. switch (fopen_mode & (__MODE_READ | __MODE_WRITE)) {
  563. case 0:
  564. return 0;
  565. case __MODE_READ:
  566. open_mode |= O_RDONLY;
  567. break;
  568. case __MODE_WRITE:
  569. open_mode |= O_WRONLY;
  570. break;
  571. default:
  572. open_mode |= O_RDWR;
  573. break;
  574. }
  575. /* Allocate the (FILE) before we do anything irreversable */
  576. if (fp == 0) {
  577. nfp = malloc(sizeof(FILE));
  578. if (nfp == 0)
  579. return 0;
  580. }
  581. /* Open the file itself */
  582. if (fname)
  583. fd = open(fname, open_mode, 0666);
  584. if (fd < 0) { /* Grrrr */
  585. if (nfp)
  586. free(nfp);
  587. return 0;
  588. }
  589. /* If this isn't freopen create a (FILE) and buffer for it */
  590. if (fp == 0) {
  591. fp = nfp;
  592. fp->next = __IO_list;
  593. __IO_list = fp;
  594. fp->mode = __MODE_FREEFIL;
  595. if (isatty(fd)) {
  596. fp->mode |= _IOLBF;
  597. #if __MODE_IOTRAN
  598. if (do_iosense)
  599. fopen_mode |= __MODE_IOTRAN;
  600. #endif
  601. } else
  602. fp->mode |= _IOFBF;
  603. fp->bufstart = _alloc_stdio_buffer(BUFSIZ);
  604. if (fp->bufstart == 0) { /* Oops, no mem *//* Humm, full buffering with a two(!) byte
  605. * buffer. */
  606. fp->bufstart = fp->unbuf;
  607. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  608. } else {
  609. fp->bufend = fp->bufstart + BUFSIZ;
  610. fp->mode |= __MODE_FREEBUF;
  611. }
  612. }
  613. /* Ok, file's ready clear the buffer and save important bits */
  614. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  615. fp->mode |= fopen_mode;
  616. fp->fd = fd;
  617. return fp;
  618. }
  619. #endif
  620. #ifdef L_fclose
  621. int fclose(fp)
  622. FILE *fp;
  623. {
  624. int rv = 0;
  625. __init_stdio();
  626. if (fp == 0) {
  627. errno = EINVAL;
  628. return EOF;
  629. }
  630. if (fflush(fp))
  631. return EOF;
  632. if (close(fp->fd))
  633. rv = EOF;
  634. fp->fd = -1;
  635. if (fp->mode & __MODE_FREEBUF) {
  636. _free_stdio_buffer(fp->bufstart);
  637. fp->mode &= ~__MODE_FREEBUF;
  638. fp->bufstart = fp->bufend = 0;
  639. }
  640. if (fp->mode & __MODE_FREEFIL) {
  641. FILE *prev = 0, *ptr;
  642. fp->mode = 0;
  643. for (ptr = __IO_list; ptr && ptr != fp; ptr = ptr->next);
  644. if (ptr == fp) {
  645. if (prev == 0)
  646. __IO_list = fp->next;
  647. else
  648. prev->next = fp->next;
  649. }
  650. free(fp);
  651. } else
  652. fp->mode = 0;
  653. return rv;
  654. }
  655. #endif
  656. #ifdef L_setbuffer
  657. void setbuffer(fp, buf, size)
  658. FILE *fp;
  659. char *buf;
  660. size_t size;
  661. {
  662. fflush(fp);
  663. if ((fp->bufstart == (unsigned char *) buf)
  664. && (fp->bufend == ((unsigned char *) buf + size)))
  665. return;
  666. if (fp->mode & __MODE_FREEBUF) {
  667. _free_stdio_buffer(fp->bufstart);
  668. }
  669. fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF);
  670. if (buf == 0) {
  671. fp->bufstart = fp->unbuf;
  672. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  673. fp->mode |= _IONBF;
  674. } else {
  675. fp->bufstart = buf;
  676. fp->bufend = buf + size;
  677. fp->mode |= _IOFBF;
  678. }
  679. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  680. }
  681. #endif
  682. #ifdef L_setvbuf
  683. int setvbuf(fp, buf, mode, size)
  684. FILE *fp;
  685. char *buf;
  686. int mode;
  687. size_t size;
  688. {
  689. fflush(fp);
  690. if (fp->mode & __MODE_FREEBUF) {
  691. _free_stdio_buffer(fp->bufstart);
  692. }
  693. fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF);
  694. fp->bufstart = fp->unbuf;
  695. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  696. fp->mode |= _IONBF;
  697. if (mode == _IOFBF || mode == _IOLBF) {
  698. if (size <= 0) {
  699. size = BUFSIZ;
  700. }
  701. if (buf == 0) {
  702. buf = _alloc_stdio_buffer(size);
  703. if (buf == 0)
  704. return EOF;
  705. }
  706. fp->bufstart = buf;
  707. fp->bufend = buf + size;
  708. fp->mode |= mode;
  709. }
  710. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  711. return 0;
  712. }
  713. #endif
  714. #ifdef L_ungetc
  715. int ungetc(c, fp)
  716. int c;
  717. FILE *fp;
  718. {
  719. __init_stdio();
  720. if (fp->mode & __MODE_WRITING)
  721. fflush(fp);
  722. /* Can't read or there's been an error then return EOF */
  723. if ((fp->mode & (__MODE_READ | __MODE_ERR)) != __MODE_READ)
  724. return EOF;
  725. /* Can't do fast fseeks */
  726. fp->mode |= __MODE_UNGOT;
  727. if (fp->bufpos > fp->bufstart)
  728. return *--fp->bufpos = (unsigned char) c;
  729. else if (fp->bufread == fp->bufstart)
  730. return *fp->bufread++ = (unsigned char) c;
  731. else
  732. return EOF;
  733. }
  734. #endif