Leaked source code of windows server 2003
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.

189 lines
6.3 KiB

  1. /***
  2. *lseek.c - change file position
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _lseek() - move the file pointer
  8. *
  9. *Revision History:
  10. * 06-20-89 PHG Module created, based on asm version
  11. * 03-12-90 GJF Made calling type _CALLTYPE2 (for now), added #include
  12. * <cruntime.h> and fixed the copyright. Also, cleaned up
  13. * the formatting a bit.
  14. * 04-03-90 GJF Now _CALLTYPE1.
  15. * 07-24-90 SBM Removed '32' from API names
  16. * 08-14-90 SBM Compiles cleanly with -W3
  17. * 09-28-90 GJF New-style function declarators.
  18. * 12-04-90 GJF Appended Win32 version with #ifdef-s. It's probably
  19. * worth coming back and doing a more complete merge later
  20. * (much later).
  21. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  22. * 12-06-90 SRW Changed to use _osfile and _osfhnd instead of _osfinfo
  23. * 01-16-91 GJF ANSI naming.
  24. * 02-07-91 SRW Changed to call _get_osfhandle [_WIN32_]
  25. * 04-09-91 PNT Added _MAC_ conditional
  26. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  27. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  28. * 09-06-94 CFW Remove Cruiser support.
  29. * 09-06-94 CFW Replace MTHREAD with _MT.
  30. * 12-03-94 SKS Clean up OS/2 references
  31. * 01-04-95 GJF _WIN32_ -> _WIN32
  32. * 02-15-95 GJF Appended Mac version of source file (somewhat cleaned
  33. * up), with appropriate #ifdef-s.
  34. * 03-13-95 CFW Verify handles before passing to OS.
  35. * 06-06-95 GJF Replaced _osfile[] with _osfile() (macro referencing
  36. * field in ioinfo struct).
  37. * 06-27-95 GJF Added check that the file handle is open.
  38. * 07-08-96 GJF Replaced defined(_WIN32) with !defined(_MAC), and
  39. * defined(_M_M68K) || defined(_M_MPPC) with
  40. * defined(_MAC). Also, detab-ed and cleaned up the
  41. * format a bit.
  42. * 12-30-97 GJF Exception-safe locking.
  43. * 05-17-99 PML Remove all Macintosh support.
  44. *
  45. *******************************************************************************/
  46. #include <cruntime.h>
  47. #include <oscalls.h>
  48. #include <mtdll.h>
  49. #include <io.h>
  50. #include <internal.h>
  51. #include <stdlib.h>
  52. #include <errno.h>
  53. #include <msdos.h>
  54. #include <stdio.h>
  55. /***
  56. *long _lseek(fh,pos,mthd) - move the file pointer
  57. *
  58. *Purpose:
  59. * Moves the file pointer associated with fh to a new position.
  60. * The new position is pos bytes (pos may be negative) away
  61. * from the origin specified by mthd.
  62. *
  63. * If mthd == SEEK_SET, the origin in the beginning of file
  64. * If mthd == SEEK_CUR, the origin is the current file pointer position
  65. * If mthd == SEEK_END, the origin is the end of the file
  66. *
  67. * Multi-thread:
  68. * _lseek() = locks/unlocks the file
  69. * _lseek_lk() = does NOT lock/unlock the file (it is assumed that
  70. * the caller has the aquired the file lock,if needed).
  71. *
  72. *Entry:
  73. * int fh - file handle to move file pointer on
  74. * long pos - position to move to, relative to origin
  75. * int mthd - specifies the origin pos is relative to (see above)
  76. *
  77. *Exit:
  78. * returns the offset, in bytes, of the new position from the beginning
  79. * of the file.
  80. * returns -1L (and sets errno) if fails.
  81. * Note that seeking beyond the end of the file is not an error.
  82. * (although seeking before the beginning is.)
  83. *
  84. *Exceptions:
  85. *
  86. *******************************************************************************/
  87. #ifdef _MT
  88. /* define locking/validating lseek */
  89. long __cdecl _lseek (
  90. int fh,
  91. long pos,
  92. int mthd
  93. )
  94. {
  95. int r;
  96. /* validate fh */
  97. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  98. !(_osfile(fh) & FOPEN) )
  99. {
  100. /* bad file handle */
  101. errno = EBADF;
  102. _doserrno = 0; /* not o.s. error */
  103. return -1;
  104. }
  105. _lock_fh(fh); /* lock file handle */
  106. __try {
  107. if ( _osfile(fh) & FOPEN )
  108. r = _lseek_lk(fh, pos, mthd); /* seek */
  109. else {
  110. errno = EBADF;
  111. _doserrno = 0;
  112. r = -1;
  113. }
  114. }
  115. __finally {
  116. _unlock_fh(fh); /* unlock file handle */
  117. }
  118. return r;
  119. }
  120. /* define core _lseek -- doesn't lock or validate fh */
  121. long __cdecl _lseek_lk (
  122. int fh,
  123. long pos,
  124. int mthd
  125. )
  126. {
  127. ULONG newpos; /* new file position */
  128. ULONG dosretval; /* o.s. return value */
  129. HANDLE osHandle; /* o.s. handle value */
  130. #else
  131. /* define normal _lseek */
  132. long __cdecl _lseek (
  133. int fh,
  134. long pos,
  135. int mthd
  136. )
  137. {
  138. ULONG newpos; /* new file position */
  139. ULONG dosretval; /* o.s. return value */
  140. HANDLE osHandle; /* o.s. handle value */
  141. /* validate fh */
  142. if ( ((unsigned)fh >= (unsigned)_nhandle) ||
  143. !(_osfile(fh) & FOPEN) )
  144. {
  145. /* bad file handle */
  146. errno = EBADF;
  147. _doserrno = 0; /* not o.s. error */
  148. return -1;
  149. }
  150. #endif
  151. /* tell o.s. to seek */
  152. #if SEEK_SET != FILE_BEGIN || SEEK_CUR != FILE_CURRENT || SEEK_END != FILE_END /*IFSTRIP=IGN*/
  153. #error Xenix and Win32 seek constants not compatible
  154. #endif
  155. if ((osHandle = (HANDLE)_get_osfhandle(fh)) == (HANDLE)-1)
  156. {
  157. errno = EBADF;
  158. return -1;
  159. }
  160. if ((newpos = SetFilePointer(osHandle, pos, NULL, mthd)) == -1)
  161. dosretval = GetLastError();
  162. else
  163. dosretval = 0;
  164. if (dosretval) {
  165. /* o.s. error */
  166. _dosmaperr(dosretval);
  167. return -1;
  168. }
  169. _osfile(fh) &= ~FEOFLAG; /* clear the ctrl-z flag on the file */
  170. return newpos; /* return */
  171. }