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.

110 lines
3.5 KiB

  1. /***
  2. *flength.c - find length of a file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _filelength() - find the length of a file
  8. *
  9. *Revision History:
  10. * 10-22-84 RN initial version
  11. * 10-27-87 JCR Multi-thread support; also, cleaned up code to save
  12. * an lseek() in some cases.
  13. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  14. * 05-25-88 PHG Merged DLL and normal versions
  15. * 03-12-90 GJF Replaced _LOAD_DS with _CALLTYPE1, added #include
  16. * <cruntime.h>, removed #include <register.h> and fixed
  17. * the copyright. Also, cleaned up the formatting a bit.
  18. * 07-23-90 SBM Removed #include <assertm.h>
  19. * 09-28-90 GJF New-style function declarator.
  20. * 12-04-90 GJF Improved range check of file handle. Also, replaced
  21. * numeric values with symbolic constants for seek
  22. * methods.
  23. * 01-16-91 GJF ANSI naming.
  24. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  25. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  26. * 01-04-95 GJF _WIN32_ -> _WIN32
  27. * 02-15-95 GJF Appended Mac version of source file (somewhat cleaned
  28. * up), with appropriate #ifdef-s.
  29. * 06-27-95 GJF Added check that the file handle is open.
  30. * 07-08-96 GJF Replaced defined(_WIN32) with !defined(_MAC), and
  31. * defined(_M_M68K) || defined(_M_MPPC) with
  32. * defined(_MAC). Removed obsolete REG1 macro. Also,
  33. * detab-ed and cleaned up the format a bit.
  34. * 12-19-97 GJF Exception-safe locking.
  35. * 05-17-99 PML Remove all Macintosh support.
  36. *
  37. *******************************************************************************/
  38. #include <cruntime.h>
  39. #include <stdio.h>
  40. #include <errno.h>
  41. #include <io.h>
  42. #include <internal.h>
  43. #include <msdos.h>
  44. #include <mtdll.h>
  45. #include <stddef.h>
  46. #include <stdlib.h>
  47. /***
  48. *long _filelength(filedes) - find length of a file
  49. *
  50. *Purpose:
  51. * Returns the length in bytes of the specified file.
  52. *
  53. *Entry:
  54. * int filedes - handle referring to file to find length of
  55. *
  56. *Exit:
  57. * returns length of file in bytes
  58. * returns -1L if fails
  59. *
  60. *Exceptions:
  61. *
  62. *******************************************************************************/
  63. long __cdecl _filelength (
  64. int filedes
  65. )
  66. {
  67. long length;
  68. long here;
  69. if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  70. !(_osfile(filedes) & FOPEN) )
  71. {
  72. errno = EBADF;
  73. _doserrno = 0L; /* not an OS error */
  74. return(-1L);
  75. }
  76. #ifdef _MT
  77. _lock_fh(filedes);
  78. __try {
  79. if ( _osfile(filedes) & FOPEN ) {
  80. #endif /* _MT */
  81. /* Seek to end to get length of file. */
  82. if ( (here = _lseek_lk(filedes, 0L, SEEK_CUR)) == -1L )
  83. length = -1L; /* return error */
  84. else {
  85. length = _lseek_lk(filedes, 0L, SEEK_END);
  86. if ( here != length )
  87. _lseek_lk(filedes, here, SEEK_SET);
  88. }
  89. #ifdef _MT
  90. }
  91. else {
  92. errno = EBADF;
  93. _doserrno = 0L;
  94. length = -1L;
  95. }
  96. }
  97. __finally {
  98. _unlock_fh(filedes);
  99. }
  100. #endif /* _MT */
  101. return(length);
  102. }