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

  1. /***
  2. *fleni64.c - find length of a file
  3. *
  4. *       Copyright (c) 1994-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _filelengthi64() - find the length of a file
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <io.h>
  15. #include <internal.h>
  16. #include <msdos.h>
  17. #include <mtdll.h>
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20.  
  21. /***
  22. *__int64 _filelengthi64(filedes) - find length of a file
  23. *
  24. *Purpose:
  25. *       Returns the length in bytes of the specified file.
  26. *
  27. *Entry:
  28. *       int filedes - handle referring to file to find length of
  29. *
  30. *Exit:
  31. *       returns length of file in bytes
  32. *       returns -1i64 if fails
  33. *
  34. *Exceptions:
  35. *
  36. *******************************************************************************/
  37.  
  38. __int64 __cdecl _filelengthi64 (
  39.         int filedes
  40.         )
  41. {
  42.         __int64 length;
  43.         __int64 here;
  44.  
  45.         if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  46.              !(_osfile(filedes) & FOPEN) )
  47.         {
  48.             errno = EBADF;
  49.             _doserrno = 0L;     /* not an OS error */
  50.             return(-1L);
  51.         }
  52.  
  53.         _lock_fh( filedes );
  54.  
  55.         /* Seek to end (and back) to get the file length. */
  56.  
  57.         if ( (here = _lseeki64_lk( filedes, 0i64, SEEK_CUR )) == -1i64 )
  58.             length = -1i64;     /* return error */
  59.         else {
  60.             length = _lseeki64_lk( filedes, 0i64, SEEK_END );
  61.             if ( here != length )
  62.                 _lseeki64_lk( filedes, here, SEEK_SET );
  63.         }
  64.  
  65.         _unlock_fh( filedes );
  66.  
  67.         return( length );
  68. }
  69.