home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / libc / stdio / fseek.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-05-05  |  964 b   |  49 lines

  1. /*
  2.  * Seek for standard library.  Coordinates with buffering.
  3.  */
  4.  
  5. #include    <stdio.h>
  6.  
  7. long lseek();
  8.  
  9. fseek(iop, offset, ptrname)
  10.     register FILE *iop;
  11.     long offset;
  12. {
  13.     register int c;
  14.     long p;
  15.  
  16.     iop->_flag &= ~_IOEOF;
  17.     if (iop->_flag & _IOREAD) {
  18.         if (ptrname < 2 && iop->_base && !(iop->_flag&_IONBF)) {
  19.             c = iop->_cnt;
  20.             p = offset;
  21.             if (ptrname == 0)
  22.                 p += c - lseek(fileno(iop), 0L, 1);
  23.             else
  24.                 offset -= c;
  25.             if (!(iop->_flag&_IORW) && c > 0 && p <= c
  26.                 && p >= iop->_base - iop->_ptr){
  27.                 iop->_ptr += (int) p;
  28.                 iop->_cnt -= (int) p;
  29.                 return(0);
  30.             }
  31.         }
  32.         if (iop->_flag & _IORW) {
  33.             iop->_ptr = iop->_base;
  34.             iop->_flag &= ~_IOREAD;
  35.         }
  36.         p = lseek(fileno(iop), offset, ptrname);
  37.         iop->_cnt = 0;
  38.     } else if(iop->_flag & (_IOWRT|_IORW)) {
  39.         fflush(iop);
  40.         if (iop->_flag & _IORW) {
  41.             iop->_cnt = 0;
  42.             iop->_flag &= ~_IOWRT;
  43.             iop->_ptr = iop->_base;
  44.         }
  45.         p = lseek(fileno(iop), offset, ptrname);
  46.     }
  47.     return(p==-1? -1: 0);
  48. }
  49.