Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / stdio / ftell.c
1 /*
2  * ftell.c - obtain the value of the file-position indicator of a stream
3  */
4 /* $Id: ftell.c,v 1.5 1994/06/24 11:50:10 ceriel Exp $ */
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), 0, SEEK_CUR);
32
33         if ( result == -1 )
34                 return result;
35
36         result += (long) adjust;
37         return result;
38 }