stdio.c 17 KB

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