stdio.c 17 KB

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