stdio.c 18 KB

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