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.

285 lines
10 KiB

  1. /***
  2. *ftell.c - get current file position
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines ftell() - find current current position of file pointer
  8. *
  9. *Revision History:
  10. * 09-02-83 RN initial version
  11. * ??-??-?? TC added code to allow variable buffer sizes
  12. * 05-22-86 TC added code to seek to send if last operation was a
  13. * write and append mode specified
  14. * 11-20-86 SKS do not seek to end of file in append mode
  15. * 12-01-86 SKS fix off-by-1 problem in text mode when last byte in
  16. * buffer was a '\r', and it was followed by a '\n'. Since
  17. * the \n was pushed back and the \r was discarded, we
  18. * must adjust the computed position for the \r.
  19. * 02-09-87 JCR Added errno set code (if flag (_IORW not set)
  20. * 09-09-87 JCR Optimized to eliminate two lseek() calls in binary mode.
  21. * 09-28-87 JCR Corrected _iob2 indexing (now uses _iob_index() macro).
  22. * 11-04-87 JCR Multi-thread version
  23. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  24. * 01-13-88 JCR Removed unnecessary calls to mthread fileno/feof/ferror
  25. * 05-27-88 PHG Merged DLL and normal versions
  26. * 06-06-88 JCR Use _iob2_ macro instead of _iob_index
  27. * 06-15-88 JCR Near reference to _iob[] entries; improve REG variables
  28. * 07-27-88 JCR Changed some variables from int to unsigned (bug fix)
  29. * 08-25-88 GJF Don't use FP_OFF() macro for the 386
  30. * 12-05-88 JCR Added _IOCTRLZ support (fixes bug pertaining to ^Z at
  31. * eof)
  32. * 08-17-89 GJF Cleanup, now specific to OS/2 2.0 (i.e., 386 flat
  33. * model), also fixed copyright
  34. * 02-15-90 GJF _iob[], _iob2[] merge. Also, fixed copyright and
  35. * indents.
  36. * 03-19-90 GJF Made calling type _CALLTYPE1, added #include
  37. * <cruntime.h> and removed #include <register.h>.
  38. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  39. * 10-02-90 GJF New-style function declarators.
  40. * 01-21-91 GJF ANSI naming.
  41. * 03-27-92 DJM POSIX support.
  42. * 08-26-92 GJF Include unistd.h for POSIX build.
  43. * 09-01-92 GJF Fixed POSIX support (was returning -1 for all except
  44. * read-write streams).
  45. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  46. * 06-29-93 GJF Fixed bug related to variable buffer sizing (Cuda
  47. * #5456).
  48. * 09-06-94 CFW Replace MTHREAD with _MT.
  49. * 02-06-94 CFW assert -> _ASSERTE.
  50. * 02-20-95 GJF Merged in Mac version.
  51. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  52. * 06-12-95 GJF Replaced _osfile[] with _osfile() (macro referencing
  53. * field in ioinfo struct).
  54. * 02-27-98 RKP Add 64 bit support.
  55. * 03-02-98 GJF Exception-safe locking.
  56. * 05-17-99 PML Remove all Macintosh support.
  57. * 07-23-02 BWT If the second lseek fails when doing a text read, return -1
  58. * technically, we're SOL here because we just finished seeking
  59. * to the end of the image...
  60. *
  61. *******************************************************************************/
  62. #include <cruntime.h>
  63. #include <stdio.h>
  64. #include <file2.h>
  65. #include <dbgint.h>
  66. #include <errno.h>
  67. #ifdef _POSIX_
  68. #include <unistd.h>
  69. #else
  70. #include <msdos.h>
  71. #endif
  72. #include <stddef.h>
  73. #include <io.h>
  74. #include <internal.h>
  75. #ifndef _POSIX_
  76. #include <mtdll.h>
  77. #endif
  78. /***
  79. *long ftell(stream) - query stream file pointer
  80. *
  81. *Purpose:
  82. * Find out what stream's position is. coordinate with buffering; adjust
  83. * backward for read-ahead and forward for write-behind. This is NOT
  84. * equivalent to fseek(stream,0L,1), because fseek will remove an ungetc,
  85. * may flush buffers, etc.
  86. *
  87. *Entry:
  88. * FILE *stream - stream to query for position
  89. *
  90. *Exit:
  91. * return present file position if succeeds
  92. * returns -1L and sets errno if fails
  93. *
  94. *Exceptions:
  95. *
  96. *******************************************************************************/
  97. #ifdef _MT /* multi-thread; define both ftell() and _lk_ftell() */
  98. long __cdecl ftell (
  99. FILE *stream
  100. )
  101. {
  102. long retval;
  103. _ASSERTE(stream != NULL);
  104. _lock_str(stream);
  105. __try {
  106. retval = _ftell_lk (stream);
  107. }
  108. __finally {
  109. _unlock_str(stream);
  110. }
  111. return(retval);
  112. }
  113. /***
  114. *_ftell_lk() - Ftell() core routine (assumes stream is locked).
  115. *
  116. *Purpose:
  117. * Core ftell() routine; assumes caller has aquired stream lock).
  118. *
  119. * [See ftell() above for more info.]
  120. *
  121. *Entry: [See ftell()]
  122. *
  123. *Exit: [See ftell()]
  124. *
  125. *Exceptions:
  126. *
  127. *******************************************************************************/
  128. long __cdecl _ftell_lk (
  129. #else /* non multi-thread; define only ftell() */
  130. long __cdecl ftell (
  131. #endif /* rejoin common code */
  132. FILE *str
  133. )
  134. {
  135. REG1 FILE *stream;
  136. unsigned int offset;
  137. long filepos;
  138. #if !defined(_POSIX_)
  139. REG2 char *p;
  140. char *max;
  141. #endif
  142. int fd;
  143. unsigned int rdcnt;
  144. _ASSERTE(str != NULL);
  145. /* Init stream pointer and file descriptor */
  146. stream = str;
  147. #ifdef _POSIX_
  148. fd = fileno(stream);
  149. #else
  150. fd = _fileno(stream);
  151. #endif
  152. if (stream->_cnt < 0)
  153. stream->_cnt = 0;
  154. #ifdef _POSIX_
  155. if ((filepos = lseek(fd, 0L, SEEK_CUR)) < 0L)
  156. #else
  157. if ((filepos = _lseek(fd, 0L, SEEK_CUR)) < 0L)
  158. #endif
  159. return(-1L);
  160. if (!bigbuf(stream)) /* _IONBF or no buffering designated */
  161. return(filepos - stream->_cnt);
  162. offset = (unsigned)(stream->_ptr - stream->_base);
  163. #ifndef _POSIX_
  164. if (stream->_flag & (_IOWRT|_IOREAD)) {
  165. if (_osfile(fd) & FTEXT)
  166. for (p = stream->_base; p < stream->_ptr; p++)
  167. if (*p == '\n') /* adjust for '\r' */
  168. offset++;
  169. }
  170. else if (!(stream->_flag & _IORW)) {
  171. errno=EINVAL;
  172. return(-1L);
  173. }
  174. #endif
  175. if (filepos == 0L)
  176. return((long)offset);
  177. if (stream->_flag & _IOREAD) /* go to preceding sector */
  178. if (stream->_cnt == 0) /* filepos holds correct location */
  179. offset = 0;
  180. else {
  181. /* Subtract out the number of unread bytes left in the buffer.
  182. [We can't simply use _iob[]._bufsiz because the last read
  183. may have hit EOF and, thus, the buffer was not completely
  184. filled.] */
  185. rdcnt = stream->_cnt + (unsigned)(stream->_ptr - stream->_base);
  186. #if !defined(_POSIX_)
  187. /* If text mode, adjust for the cr/lf substitution. If binary
  188. mode, we're outta here. */
  189. if (_osfile(fd) & FTEXT) {
  190. /* (1) If we're not at eof, simply copy _bufsiz onto rdcnt
  191. to get the # of untranslated chars read. (2) If we're at
  192. eof, we must look through the buffer expanding the '\n'
  193. chars one at a time. */
  194. /* [NOTE: Performance issue -- it is faster to do the two
  195. _lseek() calls than to blindly go through and expand the
  196. '\n' chars regardless of whether we're at eof or not.] */
  197. if (_lseek(fd, 0L, SEEK_END) == filepos) {
  198. max = stream->_base + rdcnt;
  199. for (p = stream->_base; p < max; p++)
  200. if (*p == '\n')
  201. /* adjust for '\r' */
  202. rdcnt++;
  203. /* If last byte was ^Z, the lowio read didn't tell us
  204. about it. Check flag and bump count, if necessary. */
  205. if (stream->_flag & _IOCTRLZ)
  206. ++rdcnt;
  207. }
  208. else {
  209. if (_lseek(fd, filepos, SEEK_SET) < 0)
  210. return (-1);
  211. /* We want to set rdcnt to the number of bytes
  212. originally read into the stream buffer (before
  213. crlf->lf translation). In most cases, this will
  214. just be _bufsiz. However, the buffer size may have
  215. been changed, due to fseek optimization, at the
  216. END of the last _filbuf call. */
  217. if ( (rdcnt <= _SMALL_BUFSIZ) &&
  218. (stream->_flag & _IOMYBUF) &&
  219. !(stream->_flag & _IOSETVBUF) )
  220. {
  221. /* The translated contents of the buffer is small
  222. and we are not at eof. The buffer size must have
  223. been set to _SMALL_BUFSIZ during the last
  224. _filbuf call. */
  225. rdcnt = _SMALL_BUFSIZ;
  226. }
  227. else
  228. rdcnt = stream->_bufsiz;
  229. /* If first byte in untranslated buffer was a '\n',
  230. assume it was preceeded by a '\r' which was
  231. discarded by the previous read operation and count
  232. the '\n'. */
  233. if (_osfile(fd) & FCRLF)
  234. ++rdcnt;
  235. }
  236. } /* end if FTEXT */
  237. #endif
  238. filepos -= (long)rdcnt;
  239. } /* end else stream->_cnt != 0 */
  240. return(filepos + (long)offset);
  241. }