stdio.c 18 KB

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