xref: /minix3/lib/libc/stdio/ftell.c (revision b706112487045bc1efd01e3d4d53d9a6b04a0bca)
1 /*
2  * ftell.c - obtain the value of the file-position indicator of a stream
3  */
4 /* $Header$ */
5 
6 #include	<stdio.h>
7 
8 #if	(SEEK_CUR != 1) || (SEEK_SET != 0) || (SEEK_END != 2)
9 #error SEEK_* values are wrong
10 #endif
11 
12 #include	"loc_incl.h"
13 
14 #include	<sys/types.h>
15 
16 off_t _lseek(int fildes, off_t offset, int whence);
17 
18 long ftell(FILE *stream)
19 {
20 	long result;
21 	int adjust = 0;
22 
23 	if (io_testflag(stream,_IOREADING))
24 		adjust = -stream->_count;
25 	else if (io_testflag(stream,_IOWRITING)
26 		    && stream->_buf
27 		    && !io_testflag(stream,_IONBF))
28 		adjust = stream->_ptr - stream->_buf;
29 	else adjust = 0;
30 
31 	result = _lseek(fileno(stream), (off_t)0, SEEK_CUR);
32 
33 	if ( result == -1 )
34 		return result;
35 
36 	result += (long) adjust;
37 	return result;
38 }
39