stdio.c 16 KB

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