ReactOS 0.4.16-dev-1098-g0ca6002
filelength.cpp
Go to the documentation of this file.
1//
2// filelength.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _filelength(), which computes the length of a file.
7//
9#include <stddef.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13
14
15// Computes the length, in bytes, of the given file. Returns -1 on failure.
16template <typename Integer>
17_Success_(return != -1)
18static Integer __cdecl common_filelength(int const fh) throw()
19{
20 typedef __crt_integer_traits<Integer> traits;
21
23 _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
25
27 Integer end = -1;
28 __try
29 {
30 if ((_osfile(fh) & FOPEN) == 0)
31 {
32 errno = EBADF;
33 _doserrno = 0;
34 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
35 __leave;
36 }
37
38 // Seek to the end to get the length:
39 Integer const here = traits::lseek_nolock(fh, 0, SEEK_CUR);
40 if (here == -1)
41 __leave;
42
43 end = traits::lseek_nolock(fh, 0, SEEK_END);
44 if (here != end)
45 traits::lseek_nolock(fh, here, SEEK_SET);
46 }
48 {
50 }
52 return end;
53}
54
55extern "C" long __cdecl _filelength(int const fh)
56{
57 return common_filelength<long>(fh);
58}
59
60extern "C" __int64 __cdecl _filelengthi64(int const fh)
61{
62 return common_filelength<__int64>(fh);
63}
#define EBADF
Definition: acclib.h:82
#define __cdecl
Definition: accygwin.h:79
#define __int64
Definition: basetyps.h:16
#define SEEK_END
Definition: cabinet.c:29
#define _CHECK_FH_CLEAR_OSSERR_RETURN(handle, errorcode, retexpr)
int _nhandle
Definition: ioinit.cpp:34
void __cdecl __acrt_lowio_lock_fh(_In_ int _FileHandle)
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
result_buffer_count char *const _In_ int const _In_ bool const _In_ unsigned const _In_ STRFLT const _In_ bool const _Inout_ __crt_cached_ptd_host &ptd throw()
Definition: cvt.cpp:119
long __cdecl _filelength(int const fh)
Definition: filelength.cpp:55
__int64 __cdecl _filelengthi64(int const fh)
Definition: filelength.cpp:60
#define FOPEN
GLuint GLuint end
Definition: gl.h:1545
#define _doserrno
Definition: stdlib.h:131
#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr)
#define SEEK_SET
Definition: jmemansi.c:26
#define SEEK_CUR
Definition: util.h:63
#define _Success_(c)
Definition: no_sal2.h:84
#define __try
Definition: pseh2_64.h:188
#define __leave
Definition: pseh2_64.h:192
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190
#define errno
Definition: errno.h:18
#define _osfile(i)
Definition: internal.h:72
#define const
Definition: zconf.h:233