stdio.c 17 KB

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