stdio.c 17 KB

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