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