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

  1. /***
  2. *flength.c - find length of a file
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _filelength() - find the length of a file
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifndef _MAC
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #include <io.h>
  18. #include <internal.h>
  19. #include <msdos.h>
  20. #include <mtdll.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23.  
  24. /***
  25. *long _filelength(filedes) - find length of a file
  26. *
  27. *Purpose:
  28. *       Returns the length in bytes of the specified file.
  29. *
  30. *Entry:
  31. *       int filedes - handle referring to file to find length of
  32. *
  33. *Exit:
  34. *       returns length of file in bytes
  35. *       returns -1L if fails
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. long __cdecl _filelength (
  42.         int filedes
  43.         )
  44. {
  45.         long length;
  46.         long here;
  47.  
  48.         if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  49.              !(_osfile(filedes) & FOPEN) )
  50.         {
  51.                 errno = EBADF;
  52.                 _doserrno = 0L;         /* not an OS error */
  53.                 return(-1L);
  54.         }
  55.  
  56.         _lock_fh(filedes);
  57.  
  58.         /* Seek to end to get length of file. */
  59.         if ( (here = _lseek_lk(filedes, 0L, SEEK_CUR)) == -1L )
  60.                 length = -1L;   /* return error */
  61.         else {
  62.                 length = _lseek_lk(filedes, 0L, SEEK_END);
  63.                 if ( here != length )
  64.                         _lseek_lk(filedes, here, SEEK_SET);
  65.         }
  66.  
  67.         _unlock_fh(filedes);
  68.  
  69.         return(length);
  70. }
  71.  
  72.  
  73. #else  /* _MAC */
  74.  
  75.  
  76. #include <cruntime.h>
  77. #include <stdio.h>
  78. #include <errno.h>
  79. #include <io.h>
  80. #include <internal.h>
  81. #include <memory.h>
  82. #include <msdos.h>
  83. #include <stddef.h>
  84. #include <stdlib.h>
  85. #include <macos\files.h>
  86. #include <macos\errors.h>
  87.  
  88. /***
  89. *long _filelength(fh) - find length of a file
  90. *
  91. *Purpose:
  92. *       Returns the length in bytes of the specified file.
  93. *
  94. *Entry:
  95. *       int fh - handle referring to file to find length of
  96. *
  97. *Exit:
  98. *       returns length of file in bytes
  99. *       returns -1L if fails
  100. *
  101. *Exceptions:
  102. *
  103. *******************************************************************************/
  104.  
  105. long __cdecl _filelength (
  106.         int fh
  107.         )
  108. {
  109.         ParamBlockRec parm;
  110.         OSErr osErr;
  111.  
  112.         /* validate handle */
  113.         if ( (unsigned)fh >= (unsigned)_nfile ||
  114.              !(_osfile[fh] & FOPEN) ||
  115.              _osfile[fh] & FDEV )
  116.         {
  117.                 /* out of range -- return error */
  118.                 errno = EBADF;
  119.                 _macerrno = 0;
  120.                 return -1;
  121.         }
  122.  
  123.         memset(&parm, 0, sizeof(ParamBlockRec));
  124.         parm.ioParam.ioRefNum = _osfhnd[fh];
  125.         osErr = PBGetEOFSync(&parm);
  126.         if (osErr)
  127.         {
  128.                 _dosmaperr(osErr);
  129.                 return -1;
  130.         }
  131.         return (long)parm.ioParam.ioMisc;
  132. }
  133.  
  134.  
  135. #endif  /* _MAC */
  136.