home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib25.zoo / fseek.c < prev    next >
C/C++ Source or Header  |  1992-09-05  |  2KB  |  94 lines

  1. /* something like the origonal
  2.  * from Dale Schumacher's dLibs
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <unistd.h>
  7.  
  8. long ftell(fp)
  9. FILE *fp;
  10. {
  11.     long rv, count = fp->_cnt, adjust = 0;
  12.     unsigned int f = fp->_flag;
  13.  
  14.     if( ((f & _IOREAD) && (!(f & _IOBIN))) ||
  15.        (count == 0)               ||
  16.        (f & _IONBF) )
  17.     {
  18.     fflush(fp);    
  19.     rv = lseek(fp->_file, 0L, SEEK_CUR);
  20.     }
  21.     else
  22.     {
  23.     if(f & _IOREAD)
  24.         adjust = -count;
  25.     else if(f & (_IOWRT | _IORW))
  26.     {
  27.         if(f & _IOWRT)
  28.         adjust = count;
  29.     }
  30.     else return -1L;
  31.  
  32.     rv = lseek(fp->_file, 0L, SEEK_CUR);
  33.     }
  34.     return (rv < 0) ? -1L : rv + adjust;
  35. }
  36.  
  37. void rewind(fp)
  38. register FILE *fp;
  39. {
  40.     fflush(fp);
  41.     (void) lseek(fp->_file, 0L, SEEK_SET);
  42.     fp->_flag &= ~(_IOEOF|_IOERR);
  43. }
  44.  
  45. int fseek(fp, offset, origin)
  46. FILE *fp;
  47. long offset;
  48. int origin;
  49. {
  50.     long pos, count;
  51.     unsigned int f;
  52.     int rv;
  53.     
  54.     /* Clear end of file flag */
  55.     f = (fp->_flag &= ~_IOEOF);
  56.     count = fp->_cnt;
  57.     
  58.     if((!(f & _IOBIN))         ||
  59.        (f & (_IOWRT))        ||
  60.        (count == 0)         ||
  61.        (f & _IONBF)        || 
  62.        (origin == SEEK_END) )
  63.     {
  64.     rv = fflush(fp);
  65.     return ((rv == EOF) || (lseek(fp->_file, offset, origin) < 0)) ?
  66.         -1 : 0;
  67.     }
  68.     
  69.     /* figure out if where we are going is still within the buffer */
  70.     pos = offset;
  71.     if(origin == SEEK_SET)
  72.     {
  73.     register long realpos;
  74.     if((realpos = tell(fp->_file)) < 0)
  75.     {    /* no point carrying on */
  76.         return -1;
  77.     }
  78.     pos += count - realpos;
  79.     }
  80.     else offset -= count;    /* we were already count ahead */
  81.     
  82.     if( (!(f & _IORW)) && (pos <= count) && (pos >= (fp->_base - fp->_ptr)) )
  83.     {
  84.     fp->_ptr += pos;
  85.     fp->_cnt -= pos;
  86.     return 0;
  87.     }
  88.     /* otherwise */
  89.     fp->_ptr = fp->_base;
  90.     fp->_cnt = 0;
  91.     if(f & _IORW) fp->_flag &= ~_IOREAD;
  92.     return (lseek(fp->_file, offset, origin) < 0) ? -1 : 0;
  93. }
  94.