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.

121 lines
3.6 KiB

  1. /***
  2. *eof.c - test a handle for end of file
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _eof() - determine if a file is at eof
  8. *
  9. *Revision History:
  10. * 09-07-83 RN initial version
  11. * 10-28-87 JCR Multi-thread support
  12. * 12-11-87 JCR Added "_LOAD_DS" to declaration
  13. * 05-25-88 PHG DLL replaces normal version
  14. * 07-11-88 JCR Added REG allocation to declarations
  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. * 09-28-90 GJF New-style function declarator.
  19. * 12-04-90 GJF Improved range check of file handle.
  20. * 01-16-91 GJF ANSI naming.
  21. * 02-13-92 GJF Replaced _nfile by _nhandle for Win32.
  22. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  23. * 01-04-95 GJF _WIN32_ -> _WIN32
  24. * 02-15-95 GJF Appended Mac version of source file (somewhat cleaned
  25. * up), with appropriate #ifdef-s.
  26. * 06-27-95 GJF Added check that the file handle is open.
  27. * 07-08-96 GJF Replaced defined(_WIN32) with !defined(_MAC), and
  28. * defined(_M_M68K) || defined(_M_MPPC) with
  29. * defined(_MAC). Removed obsolete REG* macros. Also,
  30. * detab-ed and cleaned up the format a bit.
  31. * 12-17-97 GJF Exception-safe locking.
  32. * 09-23-98 GJF Use _lseeki64_lk so _eof works on BIG files
  33. * 05-17-99 PML Remove all Macintosh support.
  34. *
  35. *******************************************************************************/
  36. #include <cruntime.h>
  37. #include <io.h>
  38. #include <errno.h>
  39. #include <stddef.h>
  40. #include <stdlib.h>
  41. #include <stdio.h>
  42. #include <internal.h>
  43. #include <msdos.h>
  44. #include <mtdll.h>
  45. /***
  46. *int _eof(filedes) - test a file for eof
  47. *
  48. *Purpose:
  49. * see if the file length is the same as the present position. if so, return
  50. * 1. if not, return 0. if an error occurs, return -1
  51. *
  52. *Entry:
  53. * int filedes - handle of file to test
  54. *
  55. *Exit:
  56. * returns 1 if at eof
  57. * returns 0 if not at eof
  58. * returns -1 and sets errno if fails
  59. *
  60. *Exceptions:
  61. *
  62. *******************************************************************************/
  63. int __cdecl _eof (
  64. int filedes
  65. )
  66. {
  67. __int64 here;
  68. __int64 end;
  69. int retval;
  70. if ( ((unsigned)filedes >= (unsigned)_nhandle) ||
  71. !(_osfile(filedes) & FOPEN) )
  72. {
  73. errno = EBADF;
  74. _doserrno = 0;
  75. return(-1);
  76. }
  77. #ifdef _MT
  78. /* Lock the file */
  79. _lock_fh(filedes);
  80. __try {
  81. if ( _osfile(filedes) & FOPEN ) {
  82. #endif /* _MT */
  83. /* See if the current position equals the end of the file. */
  84. if ( ((here = _lseeki64_lk(filedes, 0i64, SEEK_CUR)) == -1i64) ||
  85. ((end = _lseeki64_lk(filedes, 0i64, SEEK_END)) == -1i64) )
  86. retval = -1;
  87. else if ( here == end )
  88. retval = 1;
  89. else {
  90. _lseeki64_lk(filedes, here, SEEK_SET);
  91. retval = 0;
  92. }
  93. #ifdef _MT
  94. }
  95. else {
  96. errno = EBADF;
  97. _doserrno = 0;
  98. retval = -1;
  99. }
  100. }
  101. __finally {
  102. /* Unlock the file */
  103. _unlock_fh(filedes);
  104. }
  105. #endif /* _MT */
  106. /* Done */
  107. return(retval);
  108. }