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

  1. /***
  2. *eof.c - test a handle for end of file
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _eof() - determine if a file is at eof
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <io.h>
  16. #include <errno.h>
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <internal.h>
  21. #include <msdos.h>
  22. #include <mtdll.h>
  23.  
  24. /***
  25. *int _eof(filedes) - test a file for eof
  26. *
  27. *Purpose:
  28. *       see if the file length is the same as the present position. if so, return
  29. *       1. if not, return 0. if an error occurs, return -1
  30. *
  31. *Entry:
  32. *       int filedes - handle of file to test
  33. *
  34. *Exit:
  35. *       returns 1 if at eof
  36. *       returns 0 if not at eof
  37. *       returns -1 and sets errno if fails
  38. *
  39. *Exceptions:
  40. *
  41. *******************************************************************************/
  42.  
  43. int __cdecl _eof (
  44.         int filedes
  45.         )
  46. {
  47.         long here;
  48.         long end;
  49.         int retval;
  50.  
  51.         if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  52.              !(_osfile(filedes) & FOPEN) )
  53.         {
  54.                 errno = EBADF;
  55.                 _doserrno = 0;
  56.                 return(-1);
  57.         }
  58.  
  59.         /* Lock the file */
  60.         _lock_fh(filedes);
  61.  
  62.         /* See if the current position equals the end of the file. */
  63.  
  64.         if ( ((here = _lseek_lk(filedes, 0L, SEEK_CUR)) == -1L) ||
  65.              ((end = _lseek_lk(filedes, 0L, SEEK_END)) == -1L) )
  66.                 retval = -1;
  67.         else if ( here == end )
  68.                 retval = 1;
  69.         else {
  70.                 _lseek_lk(filedes, here, SEEK_SET);
  71.                 retval = 0;
  72.         }
  73.  
  74.         /* Unlock the file */
  75.         _unlock_fh(filedes);
  76.  
  77.         /* Done */
  78.         return(retval);
  79. }
  80.  
  81.  
  82. #else  /* _MAC */
  83.  
  84.  
  85. #include <cruntime.h>
  86. #include <io.h>
  87. #include <errno.h>
  88. #include <stddef.h>
  89. #include <stdlib.h>
  90. #include <stdio.h>
  91. #include <internal.h>
  92. #include <memory.h>
  93. #include <msdos.h>
  94. #include <macos\files.h>
  95. #include <macos\errors.h>
  96.  
  97. /***
  98. *int _eof(fh) - test a file for eof
  99. *
  100. *Purpose:
  101. *       see if the file length is the same as the present position. if so, return
  102. *       1. if not, return 0. if an error occurs, return -1
  103. *
  104. *Entry:
  105. *       int filedes - handle of file to test
  106. *
  107. *Exit:
  108. *       returns 1 if at eof
  109. *       returns 0 if not at eof
  110. *       returns -1 and sets errno if fails
  111. *
  112. *Exceptions:
  113. *
  114. *******************************************************************************/
  115.  
  116. int __cdecl _eof (
  117.         int fh
  118.         )
  119. {
  120.         long end;
  121.         ParamBlockRec param;
  122.         OSErr osErr;
  123.  
  124.  
  125.         if ( (unsigned)fh >= (unsigned)_nfile || !(_osfile[fh] & FOPEN) )
  126.         {
  127.                 errno = EBADF;
  128.                 _macerrno = 0;
  129.                 return -1;
  130.         }
  131.  
  132.         if (_osfile[fh] & FDEV)
  133.         {
  134.                 return 0;                 /*console is never at eof*/
  135.         }
  136.  
  137.         /* See if the current position equals the end of the file. */
  138.  
  139.         memset(¶m, 0, sizeof(ParamBlockRec));
  140.         param.ioParam.ioRefNum = _osfhnd[fh];
  141.         osErr = PBGetEOFSync(¶m);
  142.         if (osErr == noErr)
  143.         {
  144.                 end = (long)param.ioParam.ioMisc;
  145.                 osErr = PBGetFPosSync(¶m);
  146.                 if (osErr == noErr)
  147.                 {
  148.                         if (end == param.ioParam.ioPosOffset)
  149.                         {
  150.                                 return 1;
  151.                         }
  152.                         else
  153.                         {
  154.                                 return 0;
  155.                         }
  156.                 }
  157.         }
  158.  
  159.         _dosmaperr(osErr);
  160.  
  161.         return -1;
  162. }
  163.  
  164.  
  165. #endif  /* _MAC */
  166.