fillbuf.c 849 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* $Id$ */
  2. #include <stdio.h>
  3. char *malloc();
  4. _fillbuf(iop)
  5. register FILE *iop;
  6. {
  7. static unsigned char ch[_NFILES];
  8. iop->_count = 0;
  9. if (fileno(iop) < 0) return EOF;
  10. if ( io_testflag(iop, (IO_EOF | IO_ERR )))
  11. return (EOF);
  12. if ( !io_testflag(iop, IO_READMODE) )
  13. return (EOF);
  14. if (! io_testflag(iop, IO_UNBUFF) && ! iop->_buf) {
  15. iop->_buf = (unsigned char *) malloc(_BUFSIZ);
  16. if (! iop->_buf) {
  17. iop->_flags |= IO_UNBUFF;
  18. }
  19. else {
  20. iop->_flags |= IO_MYBUF;
  21. iop->_bufsiz = _BUFSIZ;
  22. }
  23. }
  24. if (! iop->_buf) {
  25. iop->_buf = &ch[fileno(iop)];
  26. iop->_bufsiz = 1;
  27. }
  28. iop->_ptr = iop->_buf;
  29. iop->_count = read(iop->_fd, iop->_buf, iop->_bufsiz);
  30. if (iop->_count <= 0){
  31. if (iop->_count == 0) {
  32. iop->_flags |= IO_EOF;
  33. }
  34. else
  35. iop->_flags |= IO_ERR;
  36. return (EOF);
  37. }
  38. iop->_count--;
  39. return *iop->_ptr++;
  40. }