stdio.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. Inline_init;
  514. /* If we've got an fp close the old one (freopen) */
  515. if (fp) {
  516. /* Careful, don't de-allocate it */
  517. fopen_mode |=
  518. (fp->mode & (__MODE_BUF | __MODE_FREEFIL | __MODE_FREEBUF));
  519. fp->mode &= ~(__MODE_FREEFIL | __MODE_FREEBUF);
  520. fclose(fp);
  521. }
  522. /* decode the new open mode */
  523. while (*mode)
  524. switch (*mode++) {
  525. case 'r':
  526. fopen_mode |= __MODE_READ;
  527. break;
  528. case 'w':
  529. fopen_mode |= __MODE_WRITE;
  530. open_mode = (O_CREAT | O_TRUNC);
  531. break;
  532. case 'a':
  533. fopen_mode |= __MODE_WRITE;
  534. open_mode = (O_CREAT | O_APPEND);
  535. break;
  536. case '+':
  537. fopen_mode |= __MODE_RDWR;
  538. break;
  539. #if __MODE_IOTRAN
  540. case 'b': /* Binary */
  541. fopen_mode &= ~__MODE_IOTRAN;
  542. do_iosense = 0;
  543. break;
  544. case 't': /* Text */
  545. fopen_mode |= __MODE_IOTRAN;
  546. do_iosense = 0;
  547. break;
  548. #endif
  549. }
  550. /* Add in the read/write options to mode for open() */
  551. switch (fopen_mode & (__MODE_READ | __MODE_WRITE)) {
  552. case 0:
  553. return 0;
  554. case __MODE_READ:
  555. open_mode |= O_RDONLY;
  556. break;
  557. case __MODE_WRITE:
  558. open_mode |= O_WRONLY;
  559. break;
  560. default:
  561. open_mode |= O_RDWR;
  562. break;
  563. }
  564. /* Allocate the (FILE) before we do anything irreversable */
  565. if (fp == 0) {
  566. nfp = malloc(sizeof(FILE));
  567. if (nfp == 0)
  568. return 0;
  569. }
  570. /* Open the file itself */
  571. if (fname)
  572. fd = open(fname, open_mode, 0666);
  573. if (fd < 0) { /* Grrrr */
  574. if (nfp)
  575. free(nfp);
  576. return 0;
  577. }
  578. /* If this isn't freopen create a (FILE) and buffer for it */
  579. if (fp == 0) {
  580. int i;
  581. fp = nfp;
  582. fp->next = __IO_list;
  583. __IO_list = fp;
  584. fp->mode = __MODE_FREEFIL;
  585. if (isatty(fd)) {
  586. fp->mode |= _IOLBF;
  587. #if __MODE_IOTRAN
  588. if (do_iosense)
  589. fopen_mode |= __MODE_IOTRAN;
  590. #endif
  591. } else
  592. fp->mode |= _IOFBF;
  593. for (i = 0; i < FIXED_BUFFERS; i++)
  594. if (!_fixed_buffers[i].used) {
  595. fp->bufstart = _fixed_buffers[i].data;
  596. _fixed_buffers[i].used = 1;
  597. break;
  598. }
  599. if (i == FIXED_BUFFERS)
  600. fp->bufstart = malloc(BUFSIZ);
  601. if (fp->bufstart == 0) { /* Oops, no mem *//* Humm, full buffering with a two(!) byte
  602. * buffer. */
  603. fp->bufstart = fp->unbuf;
  604. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  605. } else {
  606. fp->bufend = fp->bufstart + BUFSIZ;
  607. fp->mode |= __MODE_FREEBUF;
  608. }
  609. }
  610. /* Ok, file's ready clear the buffer and save important bits */
  611. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  612. fp->mode |= fopen_mode;
  613. fp->fd = fd;
  614. return fp;
  615. }
  616. #endif
  617. #ifdef L_fclose
  618. int fclose(fp)
  619. FILE *fp;
  620. {
  621. int rv = 0;
  622. if (fp == 0) {
  623. errno = EINVAL;
  624. return EOF;
  625. }
  626. if (fflush(fp))
  627. return EOF;
  628. if (close(fp->fd))
  629. rv = EOF;
  630. fp->fd = -1;
  631. if (fp->mode & __MODE_FREEBUF) {
  632. int i;
  633. for (i = 0; i < FIXED_BUFFERS; i++)
  634. if (fp->bufstart == _fixed_buffers[i].data) {
  635. _fixed_buffers[i].used = 0;
  636. break;
  637. }
  638. if (i == FIXED_BUFFERS)
  639. free(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. int i;
  671. for (i = 0; i < FIXED_BUFFERS; i++)
  672. if (fp->bufstart == _fixed_buffers[i].data) {
  673. _fixed_buffers[i].used = 0;
  674. break;
  675. }
  676. if (i == FIXED_BUFFERS)
  677. free(fp->bufstart);
  678. }
  679. fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF);
  680. if (buf == 0) {
  681. fp->bufstart = fp->unbuf;
  682. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  683. fp->mode |= _IONBF;
  684. } else {
  685. fp->bufstart = buf;
  686. fp->bufend = buf + size;
  687. fp->mode |= _IOFBF;
  688. }
  689. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  690. }
  691. #endif
  692. #ifdef L_setvbuf
  693. int setvbuf(fp, buf, mode, size)
  694. FILE *fp;
  695. char *buf;
  696. int mode;
  697. size_t size;
  698. {
  699. fflush(fp);
  700. if (fp->mode & __MODE_FREEBUF) {
  701. int i;
  702. for (i = 0; i < FIXED_BUFFERS; i++)
  703. if (fp->bufstart == _fixed_buffers[i].data) {
  704. _fixed_buffers[i].used = 0;
  705. break;
  706. }
  707. if (i == FIXED_BUFFERS)
  708. free(fp->bufstart);
  709. }
  710. fp->mode &= ~(__MODE_FREEBUF | __MODE_BUF);
  711. fp->bufstart = fp->unbuf;
  712. fp->bufend = fp->unbuf + sizeof(fp->unbuf);
  713. fp->mode |= _IONBF;
  714. if (mode == _IOFBF || mode == _IOLBF) {
  715. if (size <= 0) {
  716. size = BUFSIZ;
  717. }
  718. if (buf == 0) {
  719. if (size == BUFSIZ) {
  720. int i;
  721. for (i = 0; i < FIXED_BUFFERS; i++)
  722. if (!_fixed_buffers[i].used) {
  723. _fixed_buffers[i].used = 1;
  724. buf = _fixed_buffers[i].data;
  725. break;
  726. }
  727. if (i == FIXED_BUFFERS)
  728. buf = malloc(size);
  729. } else {
  730. buf = malloc(size);
  731. }
  732. }
  733. if (buf == 0)
  734. return EOF;
  735. fp->bufstart = buf;
  736. fp->bufend = buf + size;
  737. fp->mode |= mode;
  738. }
  739. fp->bufpos = fp->bufread = fp->bufwrite = fp->bufstart;
  740. return 0;
  741. }
  742. #endif
  743. #ifdef L_ungetc
  744. int ungetc(c, fp)
  745. int c;
  746. FILE *fp;
  747. {
  748. if (fp->mode & __MODE_WRITING)
  749. fflush(fp);
  750. /* Can't read or there's been an error then return EOF */
  751. if ((fp->mode & (__MODE_READ | __MODE_ERR)) != __MODE_READ)
  752. return EOF;
  753. /* Can't do fast fseeks */
  754. fp->mode |= __MODE_UNGOT;
  755. if (fp->bufpos > fp->bufstart)
  756. return *--fp->bufpos = (unsigned char) c;
  757. else if (fp->bufread == fp->bufstart)
  758. return *fp->bufread++ = (unsigned char) c;
  759. else
  760. return EOF;
  761. }
  762. #endif