bb486734b73fab67658646c0a2885837576df42b
[reactos.git] / reactos / lib / msvcrt / stdio / ftell.c
1 /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
2 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
3
4 #include <msvcrt/stdio.h>
5 //#include <crtdll/unistd.h>
6 #include <msvcrt/fcntl.h>
7 #include <msvcrt/io.h>
8 #include <msvcrt/errno.h>
9 #include <msvcrt/internal/file.h>
10
11
12 long ftell(FILE *f)
13 {
14 long tres;
15 int adjust=0;
16
17 if (!f)
18 {
19 __set_errno(EBADF);
20 return -1;
21 }
22
23 if (f->_cnt < 0)
24 f->_cnt = 0;
25 else if (f->_flag&(_IOWRT))
26 {
27 if (f->_base && (f->_flag&_IONBF)==0)
28 adjust = f->_ptr - f->_base;
29 }
30 else if (f->_flag&_IOREAD)
31 {
32 adjust = - f->_cnt;
33 }
34 else
35 return -1;
36
37 tres = lseek(fileno(f), 0L, SEEK_CUR);
38 if (tres<0)
39 return tres;
40 tres += adjust;
41
42 //f->_cnt = f->_bufsiz - tres;
43 //f->_ptr = f->_base + tres;
44
45 return tres;
46 }