home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / filbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-13  |  1.7 KB  |  80 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. #include <libc/stubs.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <go32.h>
  10. #include <libc/file.h>
  11. #include <libc/stdiohk.h>
  12. #include <io.h>
  13.  
  14. int
  15. _filbuf(FILE *f)
  16. {
  17.   int size;
  18.   char c;
  19.  
  20.   if (f->_flag & _IORW)
  21.     f->_flag |= _IOREAD;
  22.  
  23.   if ((f->_flag&_IOREAD) == 0)
  24.     return EOF;
  25.   if (f->_flag&(_IOSTRG|_IOEOF))
  26.     return EOF;
  27.   f->_flag &= ~_IOUNGETC;
  28.  
  29.   if (f->_base==NULL && (f->_flag&_IONBF)==0) {
  30.     size = _go32_info_block.size_of_transfer_buffer;
  31.     if ((f->_base = malloc(size)) == NULL)
  32.     {
  33.       f->_flag |= _IONBF;
  34.       f->_flag &= ~(_IOFBF|_IOLBF);
  35.     }
  36.     else
  37.     {
  38.       f->_flag |= _IOMYBUF;
  39.       f->_bufsiz = size;
  40.     }
  41.   }
  42.  
  43.   if (f->_flag&_IONBF)
  44.     f->_base = &c;
  45.  
  46.   if (f == stdin) {
  47.     if (stdout->_flag&_IOLBF)
  48.       fflush(stdout);
  49.     if (stderr->_flag&_IOLBF)
  50.       fflush(stderr);
  51.   }
  52.   f->_cnt = _read(fileno(f), f->_base,
  53.            f->_flag & _IONBF ? 1 : f->_bufsiz);
  54.   if(__is_text_file(f) && f->_cnt>0)
  55.   {
  56.     /* truncate text file at Ctrl-Z */
  57.     char *cz=memchr(f->_base, 0x1A, f->_cnt);
  58.     if(cz)
  59.     {
  60.       int newcnt = cz - f->_base;
  61.       lseek(fileno(f), -(f->_cnt - newcnt), SEEK_CUR);
  62.       f->_cnt = newcnt;
  63.     }
  64.   }
  65.   f->_ptr = f->_base;
  66.   if (f->_flag & _IONBF)
  67.     f->_base = NULL;
  68.   if (--f->_cnt < 0) {
  69.     if (f->_cnt == -1) {
  70.       f->_flag |= _IOEOF;
  71.       if (f->_flag & _IORW)
  72.     f->_flag &= ~_IOREAD;
  73.     } else
  74.       f->_flag |= _IOERR;
  75.     f->_cnt = 0;
  76.     return EOF;
  77.   }
  78.   return *f->_ptr++ & 0377;
  79. }
  80.