home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 2.ddi / CLIBSRC3.ZIP / FSEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  4.6 KB  |  160 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fseek.c
  3.  *
  4.  * function(s)
  5.  *        fseek        - repositions a file pointer on a stream
  6.  *        ftell        - returns the current file pointer
  7.  *        Displacement - internal function
  8.  *-----------------------------------------------------------------------*/
  9.  
  10. /*
  11.  *      C/C++ Run Time Library - Version 5.0
  12.  *
  13.  *      Copyright (c) 1987, 1992 by Borland International
  14.  *      All Rights Reserved.
  15.  *
  16.  */
  17.  
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <io.h>
  22. #include <_io.h>
  23. #include <fcntl.h>
  24.  
  25. /*---------------------------------------------------------------------*
  26.  
  27. Name            Displacement
  28.  
  29. Usage           static int pascal near Displacement(FILE *fp);
  30.  
  31. *---------------------------------------------------------------------*/
  32. static int pascal near Displacement (FILE *fp)
  33. {
  34.     register    level;
  35.     int         disp;
  36.     register    unsigned char *P;
  37.  
  38.     if (fp->level < 0)
  39.         disp = level = fp->bsize + fp->level + 1;
  40.     else
  41.         disp = level = abs( fp->level );
  42.  
  43.     if (fp->flags & _F_BIN)
  44.         return disp;
  45.  
  46.     P = fp->curp;
  47.  
  48.     if (fp->level < 0)          /* If writing */
  49.         {
  50.         while (level--)
  51.             if ('\n' == *--P)
  52.                 disp++;
  53.         }
  54.     else                        /* Else reading */
  55.         {
  56.         while (level--)
  57.             if ('\n' == *P++)
  58.                 disp++;
  59.         }
  60.  
  61.         return  disp;
  62. }
  63.  
  64.  
  65. /*---------------------------------------------------------------------*
  66.  
  67. Name            fseek - repositions a file pointer on a stream
  68.  
  69. Usage           #include <stdio.h>
  70.                 int fseek(FILE * stream, long offset, int fromwhere);
  71.  
  72. Related
  73. functions usage long ftell(FILE *stream);
  74.                 int rewind(FILE *stream);
  75.  
  76. Prototype in    stdio.h
  77.  
  78. Description     fseek sets the file pointer associated with stream
  79.                 to a new position that is offset bytes beyond the file location
  80.                 given by fromwhere.
  81.  
  82.                 fromwhere must be one of the values 0, 1 or 2, which
  83.                 represent three symbolic constants (defined in stdio.h)
  84.                 as follows:
  85.  
  86.                 fromwhere               File Location
  87.                 SEEK_SET        (0)     file beginning
  88.                 SEEK_CUR        (1)     current file pointer position
  89.                 SEEK_END        (2)     end-ofC3ile
  90.  
  91.                 fseek discards any character pushed back using ungetc.
  92.  
  93.                 rewind(stream) is equivalent to fseek(stream, 0L, SEEK_SET),
  94.                 except that rewind clears the end-of-file and error
  95.                 indicators while fseek only clears the end-of-file indicator.
  96.  
  97.                 After fseek or rewind, the next operation on an update file
  98.                 may be either input or output.
  99.  
  100. Return value    fseek and rewind return 0 if the pointer
  101.                 successfully moved and a non-zero value on failure.
  102.  
  103. *---------------------------------------------------------------------*/
  104. int _FARFUNC fseek(register FILE *fp, long offset, int whence)
  105. {
  106.     if (fflush (fp))
  107.         return(EOF);
  108.  
  109.     if (SEEK_CUR == whence && fp->level > 0)
  110.         offset -= Displacement (fp);
  111.  
  112.     fp->flags &= ~(_F_OUT | _F_IN | _F_EOF);
  113.     fp->level = 0;
  114.     fp->curp = fp->buffer;
  115.  
  116.     return((lseek (fp->fd, offset, whence) == -1L) ? EOF : 0);
  117. }
  118.  
  119.  
  120. /*---------------------------------------------------------------------*
  121.  
  122. Name            ftell - returns the current file pointer
  123.  
  124. Usage           #include <stdio.h>
  125.                 long ftell(FILE * stream);
  126.  
  127. Prototype in    stdio.h
  128.  
  129. Description     ftell returns the current file pointer located in stream. The
  130.                 offset is measured in bytes from the beginning of the file.
  131.  
  132. Return value    ftell returns the current file-pointer position on success or
  133.                 -1L on error.
  134.  
  135. *---------------------------------------------------------------------*/
  136. long _FARFUNC ftell(register FILE *fp)
  137. {
  138.     long    a, l;
  139.  
  140.     if ((a = lseek(fp->fd, 0L, SEEK_CUR)) == -1)            // Current fpos
  141.         return a;
  142.  
  143.     if (fp->level < 0)  /* if writing */
  144.         {
  145.         if (_openfd[fp->fd] & O_APPEND)
  146.             {
  147.             if ((l = lseek(fp->fd, 0L, SEEK_END)) == -1)    // End of file
  148.                 return l;
  149.             if (lseek(fp->fd, a, SEEK_SET) == -1)           // Restore fpos
  150.                 return -1;
  151.             a = l;
  152.             }
  153.         a += Displacement(fp);
  154.         }
  155.     else
  156.         a -= Displacement(fp);
  157.  
  158.     return a;
  159. }
  160.