Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

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