Source code of Windows XP (NT5)
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.

88 lines
2.1 KiB

  1. /***
  2. *fleni64.c - find length of a file
  3. *
  4. * Copyright (c) 1994-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _filelengthi64() - find the length of a file
  8. *
  9. *Revision History:
  10. * 11-18-94 GJF Created. Adapted from flength.c
  11. * 06-27-95 GJF Added check that the file handle is open.
  12. * 12-19-97 GJF Exception-safe locking.
  13. *
  14. *******************************************************************************/
  15. #include <cruntime.h>
  16. #include <stdio.h>
  17. #include <errno.h>
  18. #include <io.h>
  19. #include <internal.h>
  20. #include <msdos.h>
  21. #include <mtdll.h>
  22. #include <stddef.h>
  23. #include <stdlib.h>
  24. /***
  25. *__int64 _filelengthi64(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 -1i64 if fails
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40. __int64 __cdecl _filelengthi64 (
  41. int filedes
  42. )
  43. {
  44. __int64 length;
  45. __int64 here;
  46. if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  47. !(_osfile(filedes) & FOPEN) )
  48. {
  49. errno = EBADF;
  50. _doserrno = 0L; /* not an OS error */
  51. return(-1i64);
  52. }
  53. #ifdef _MT
  54. _lock_fh(filedes);
  55. __try {
  56. if ( _osfile(filedes) & FOPEN ) {
  57. #endif /* _MT */
  58. /* Seek to end (and back) to get the file length. */
  59. if ( (here = _lseeki64_lk( filedes, 0i64, SEEK_CUR )) == -1i64 )
  60. length = -1i64; /* return error */
  61. else {
  62. length = _lseeki64_lk( filedes, 0i64, SEEK_END );
  63. if ( here != length )
  64. _lseeki64_lk( filedes, here, SEEK_SET );
  65. }
  66. #ifdef _MT
  67. }
  68. else {
  69. errno = EBADF;
  70. _doserrno = 0L;
  71. length = -1i64;
  72. }
  73. }
  74. __finally {
  75. _unlock_fh(filedes);
  76. }
  77. #endif /* _MT */
  78. return( length );
  79. }