25109101fee91f955bd1ddd5fc79d491b4d61426
[reactos.git] / lib / crtdll / 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 <msvcrt/fcntl.h>
6 #include <msvcrt/io.h>
7 #include <errno.h>
8 #include <msvcrt/internal/file.h>
9
10
11 /*
12  * @implemented
13  */
14 long ftell(FILE *f)
15 {
16   long tres;
17   int adjust=0;
18
19   if (!f) 
20   {
21     __set_errno(EBADF);
22     return -1;
23   }
24
25   if (f->_cnt < 0)
26     f->_cnt = 0;
27   else if (f->_flag&_IOREAD)
28   {
29     adjust = - f->_cnt;
30   }
31   else if (f->_flag&(_IOWRT))
32   {
33     if (f->_base && (f->_flag&_IONBF)==0)
34       adjust = f->_ptr - f->_base;
35   }
36   else
37     return -1;
38
39   tres = lseek(fileno(f), 0L, SEEK_CUR);
40   if (tres<0)
41     return tres;
42   tres += adjust;
43   
44   //f->_cnt = f->_bufsiz - tres;
45   //f->_ptr = f->_base + tres;
46
47   return tres;
48 }