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

  1. /***
  2. *rewind.c - rewind a stream
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines rewind() - rewinds a stream to the beginning.
  8. *
  9. *
  10. *******************************************************************************/
  11.  
  12. #include <cruntime.h>
  13. #include <stdio.h>
  14. #include <file2.h>
  15. #include <dbgint.h>
  16. #include <io.h>
  17. #include <mtdll.h>
  18. #include <msdos.h>
  19. #include <internal.h>
  20.  
  21. /***
  22. *void rewind(stream) - rewind a string
  23. *
  24. *Purpose:
  25. *       Back up a stream to the beginning (if not terminal).  First flush it.
  26. *       If read/write, allow next i/o operation to set mode.
  27. *
  28. *Entry:
  29. *       FILE *stream - file to rewind
  30. *
  31. *Exit:
  32. *       returns 0 if success
  33. *       returns -1 if fails
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. void __cdecl rewind (
  40.         FILE *str
  41.         )
  42. {
  43.         REG1 FILE *stream;
  44.         REG2 int fd;
  45.  
  46.         _ASSERTE(str != NULL);
  47.  
  48.         /* Init stream pointer */
  49.         stream = str;
  50.  
  51.         fd = _fileno(stream);
  52.  
  53.         /* Lock the file */
  54.         _lock_str(stream);
  55.  
  56.         /* Flush the stream */
  57.         _flush(stream);
  58.  
  59.         /* Clear errors */
  60.         stream->_flag &= ~(_IOERR|_IOEOF);
  61. #ifdef _WIN32
  62.         _osfile_safe(fd) &= ~(FEOFLAG);
  63. #else  /* _WIN32 */
  64.         _osfile[fd] &= ~(FEOFLAG);
  65. #endif  /* _WIN32 */
  66.  
  67.         /* Set flags */
  68.         /* [note: _flush set _cnt=0 and _ptr=_base] */
  69.         if (stream->_flag & _IORW)
  70.                 stream->_flag &= ~(_IOREAD|_IOWRT);
  71.  
  72.         /* Position to beginning of file */
  73.         _lseek(fd,0L,0);
  74.  
  75.         /* unlock stream */
  76.         _unlock_str(stream);
  77. }
  78.