Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <libc/file.h>
  8. #include <libc/stdiohk.h>
  9.  
  10. int _filbuf(FILE *f)
  11. {
  12.   int size;
  13.   char c;
  14.  
  15.   if (f->_flag & _IORW)
  16.     f->_flag |= _IOREAD;
  17.  
  18.   if ((f->_flag&_IOREAD) == 0)
  19.     return EOF;
  20.   if (f->_flag&(_IOSTRG|_IOEOF))
  21.     return EOF;
  22.  tryagain:
  23.   if (f->_base==NULL) {
  24.     if (f->_flag&_IONBF) {
  25.       f->_base = &c;
  26.       goto tryagain;
  27.     }
  28.     size = 512;
  29.     if ((f->_base = malloc(size)) == NULL) {
  30.       f->_flag |= _IONBF;
  31.       goto tryagain;
  32.     }
  33.     f->_flag |= _IOMYBUF;
  34.     f->_bufsiz = size;
  35.   }
  36.   if (f == stdin) {
  37.     if (stdout->_flag&_IOLBF)
  38.       fflush(stdout);
  39.     if (stderr->_flag&_IOLBF)
  40.       fflush(stderr);
  41.   }
  42.   if(f->std_ops && STM_OP(f,read))
  43.   {
  44.    f->_cnt=STM_OP(f,read)(f,f->_base,f->_flag & _IONBF ? 1 : f->_bufsiz);
  45.   } else {
  46.    f->_cnt = read(fileno(f), f->_base,f->_flag & _IONBF ? 1 : f->_bufsiz);
  47.   }
  48.   f->_ptr = f->_base;
  49.   if (f->_flag & _IONBF && f->_base == &c)
  50.     f->_base = NULL;
  51.   if (--f->_cnt < 0) {
  52.     if (f->_cnt == -1) {
  53.       f->_flag |= _IOEOF;
  54.       if (f->_flag & _IORW)
  55.         f->_flag &= ~_IOREAD;
  56.     } else
  57.       f->_flag |= _IOERR;
  58.     f->_cnt = 0;
  59.     return EOF;
  60.   }
  61.   return *f->_ptr++ & 0377;
  62. }
  63.