home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / fseeki64.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  4KB  |  147 lines

  1. /***
  2. *fseeki64.c - reposition file pointer on a stream
  3. *
  4. *       Copyright (c) 1994-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _fseeki64() - move the file pointer to new place in file
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <file2.h>
  14. #include <dbgint.h>
  15. #include <msdos.h>
  16. #include <errno.h>
  17. #include <malloc.h>
  18. #include <io.h>
  19. #include <stddef.h>
  20. #include <internal.h>
  21. #include <mtdll.h>
  22.  
  23. /***
  24. *int _fseeki64(stream, offset, whence) - reposition file pointer
  25. *
  26. *Purpose:
  27. *
  28. *       Reposition file pointer to the desired location.  The new location
  29. *       is calculated as follows:
  30. *                                { whence=0, beginning of file }
  31. *               <offset> bytes + { whence=1, current position  }
  32. *                                { whence=2, end of file       }
  33. *
  34. *       Be careful to coordinate with buffering.
  35. *
  36. *Entry:
  37. *       FILE *stream  - file to reposition file pointer on
  38. *       _int64 offset - offset to seek to
  39. *       int whence    - origin offset is measured from (0=beg, 1=current pos,
  40. *                       2=end)
  41. *
  42. *Exit:
  43. *       returns 0 if succeeds
  44. *       returns -1 and sets errno if fails
  45. *       fields of FILE struct will be changed
  46. *
  47. *Exceptions:
  48. *
  49. *******************************************************************************/
  50.  
  51. #ifdef _MT
  52.  
  53. int __cdecl _fseeki64 (
  54.         FILE *stream,
  55.         __int64 offset,
  56.         int whence
  57.         )
  58. {
  59.         int retval;
  60.  
  61.         _ASSERTE(stream != NULL);
  62.  
  63.         _lock_str(stream);
  64.  
  65.         retval = _fseeki64_lk (stream, offset, whence);
  66.  
  67.         _unlock_str(stream);
  68.  
  69.         return(retval);
  70. }
  71.  
  72.  
  73. /***
  74. *_fseeki64_lk() - Core _fseeki64() routine (stream is locked)
  75. *
  76. *Purpose:
  77. *       Core _fseeki64() routine; assumes that caller has the stream locked.
  78. *
  79. *Entry:
  80. *
  81. *Exit:
  82. *
  83. *Exceptions:
  84. *
  85. *******************************************************************************/
  86.  
  87. int __cdecl _fseeki64_lk (
  88.  
  89. #else  /* _MT */
  90.  
  91. int __cdecl _fseeki64 (
  92.  
  93. #endif  /* _MT */
  94.  
  95.         FILE *str,
  96.         __int64 offset,
  97.         int whence
  98.         )
  99. {
  100.  
  101.  
  102.         REG1 FILE *stream;
  103.  
  104.         _ASSERTE(str != NULL);
  105.  
  106.         /* Init stream pointer */
  107.         stream = str;
  108.  
  109.         if ( !inuse(stream) || ((whence != SEEK_SET) && (whence != SEEK_CUR) &&
  110.             (whence != SEEK_END)) ) {
  111.                 errno=EINVAL;
  112.                 return(-1);
  113.         }
  114.  
  115.         /* Clear EOF flag */
  116.  
  117.         stream->_flag &= ~_IOEOF;
  118.  
  119.         /* If seeking relative to current location, then convert to
  120.            a seek relative to beginning of file.  This accounts for
  121.            buffering, etc. by letting fseek() tell us where we are. */
  122.  
  123.         if (whence == SEEK_CUR) {
  124.                 offset += _ftelli64_lk(stream);
  125.                 whence = SEEK_SET;
  126.         }
  127.  
  128.         /* Flush buffer as necessary */
  129.  
  130.         _flush(stream);
  131.  
  132.         /* If file opened for read/write, clear flags since we don't know
  133.            what the user is going to do next. If the file was opened for
  134.            read access only, decrease _bufsiz so that the next _filbuf
  135.            won't cost quite so much */
  136.  
  137.         if (stream->_flag & _IORW)
  138.                 stream->_flag &= ~(_IOWRT|_IOREAD);
  139.         else if ( (stream->_flag & _IOREAD) && (stream->_flag & _IOMYBUF) &&
  140.                   !(stream->_flag & _IOSETVBUF) )
  141.                 stream->_bufsiz = _SMALL_BUFSIZ;
  142.  
  143.         /* Seek to the desired locale and return. */
  144.  
  145.         return(_lseeki64(_fileno(stream), offset, whence) == -1i64 ? -1 : 0);
  146. }
  147.