home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / FSEEK.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  4.3 KB  |  151 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. /*|                                                              |*/
  12. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  13. /*|                                                              |*/
  14. /*|                                                              |*/
  15. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  16. /*|     All Rights Reserved.                                     |*/
  17. /*|                                                              |*/
  18. /*[]------------------------------------------------------------[]*/
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <io.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 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 ftell(register FILE *fp)
  137. {
  138.     long    a;
  139.  
  140. /*  if (fflush (fp))  return(-1L);  */
  141.  
  142.     a = tell( fp->fd );
  143.  
  144.     if (fp->level < 0)  /* if writing */
  145.         a += Displacement(fp);
  146.     else
  147.         a -= Displacement(fp);
  148.  
  149.     return( a );
  150. }
  151.