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.

130 lines
3.6 KiB

  1. /***
  2. *rewind.c - rewind a stream
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines rewind() - rewinds a stream to the beginning.
  8. *
  9. *
  10. *Revision History:
  11. * 09-02-83 RN initial version
  12. * 11-02-87 JCR Multi-thread support
  13. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  14. * 05-27-88 PHG Merged DLL/normal versions
  15. * 06-01-88 JCR Clear lowio flags as well as stdio flags
  16. * 06-14-88 JCR Near reference to _iob[] entries; improve REG variables
  17. * 08-25-88 GJF Don't use FP_OFF() macro for the 386
  18. * 08-18-89 GJF Clean up, now specific to OS/2 2.0 (i.e., 386 flat
  19. * model). Also fixed copyright and indents.
  20. * 02-15-90 GJF Fixed copyright
  21. * 03-19-90 GJF Made calling type _CALLTYPE1, added #include
  22. * <cruntime.h> and removed #include <register.h>.
  23. * 05-29-90 SBM Use _flush, not [_]fflush[_lk]
  24. * 07-23-90 SBM Replaced <assertm.h> by <assert.h>
  25. * 10-03-90 GJF New-style function declarators.
  26. * 01-22-91 GJF ANSI naming.
  27. * 03-27-92 DJM POSIX support.
  28. * 08-26-92 GJF Include unistd.h for POSIX build.
  29. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  30. * 11-09-93 GJF Merged in NT SDK version (a fix for POSIX bug).
  31. * Replaced MTHREAD with _MT.
  32. * 02-06-94 CFW assert -> _ASSERTE.
  33. * 03-07-95 GJF _[un]lock_str macros now take FILE * arg.
  34. * 06-12-95 GJF Replaced _osfile[] with _osfile() (macro referencing
  35. * field in ioinfo struct).
  36. * 07-25-95 GJF Replaced _osfile() with _osfile_safe().
  37. * 03-02-98 GJF Exception-safe locking.
  38. * 05-17-99 PML Remove all Macintosh support.
  39. *
  40. *******************************************************************************/
  41. #include <cruntime.h>
  42. #include <stdio.h>
  43. #include <file2.h>
  44. #include <dbgint.h>
  45. #include <io.h>
  46. #include <mtdll.h>
  47. #ifdef _POSIX_
  48. #include <unistd.h>
  49. #else
  50. #include <msdos.h>
  51. #endif
  52. #include <internal.h>
  53. /***
  54. *void rewind(stream) - rewind a string
  55. *
  56. *Purpose:
  57. * Back up a stream to the beginning (if not terminal). First flush it.
  58. * If read/write, allow next i/o operation to set mode.
  59. *
  60. *Entry:
  61. * FILE *stream - file to rewind
  62. *
  63. *Exit:
  64. * returns 0 if success
  65. * returns -1 if fails
  66. *
  67. *Exceptions:
  68. *
  69. *******************************************************************************/
  70. void __cdecl rewind (
  71. FILE *str
  72. )
  73. {
  74. REG1 FILE *stream;
  75. REG2 int fd;
  76. _ASSERTE(str != NULL);
  77. /* Init stream pointer */
  78. stream = str;
  79. #ifdef _POSIX_
  80. fd = fileno(stream);
  81. #else
  82. fd = _fileno(stream);
  83. #endif
  84. #ifdef _MT
  85. /* Lock the file */
  86. _lock_str(stream);
  87. __try {
  88. #endif
  89. /* Flush the stream */
  90. _flush(stream);
  91. /* Clear errors */
  92. stream->_flag &= ~(_IOERR|_IOEOF);
  93. #ifndef _POSIX_
  94. _osfile_safe(fd) &= ~(FEOFLAG);
  95. #endif
  96. /* Set flags */
  97. /* [note: _flush set _cnt=0 and _ptr=_base] */
  98. if (stream->_flag & _IORW)
  99. stream->_flag &= ~(_IOREAD|_IOWRT);
  100. /* Position to beginning of file */
  101. #ifdef _POSIX_
  102. /* [note: posix _flush doesn't discard buffer */
  103. stream->_ptr = stream->_base;
  104. stream->_cnt = 0;
  105. lseek(fd,0L,0);
  106. #else
  107. _lseek(fd,0L,0);
  108. #endif
  109. #ifdef _MT
  110. }
  111. __finally {
  112. /* unlock stream */
  113. _unlock_str(stream);
  114. }
  115. #endif
  116. }