Reactos
at master 50 lines 1.4 kB view raw
1// 2// eof.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// Defines _eof(), which tests whether a file is at EOF. 7// 8#include <corecrt_internal_lowio.h> 9 10 11 12// Tests if a file is at EOF. Returns: 13// * 1 if at EOF 14// * 0 if not at EOF 15// * -1 on failure (errno is set) 16extern "C" int __cdecl _eof(int const fh) 17{ 18 _CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1); 19 _VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1); 20 _VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1); 21 22 return __acrt_lowio_lock_fh_and_call(fh, [&]() 23 { 24 if ((_osfile(fh) & FOPEN) == 0) 25 { 26 errno = EBADF; 27 _doserrno = 0; 28 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0)); 29 return -1; 30 } 31 32 __int64 const here = _lseeki64_nolock(fh, 0ll, SEEK_CUR); 33 if (here == -1ll) 34 return -1; 35 36 37 __int64 const end = _lseeki64_nolock(fh, 0ll, SEEK_END); 38 if (end == -1ll) 39 return -1; 40 41 // Now we can test if we're at the end: 42 if (here == end) 43 return 1; 44 45 // If we aren't at the end, we need to reset the stream to its original 46 // state before we return: 47 _lseeki64_nolock(fh, here, SEEK_SET); 48 return 0; 49 }); 50}