stdio.c 18 KB

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