fixed more binutils issues (newer gcc/libc)
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / stdio / ftell.c
blob1df8563663dc2eee41f7ca907a19ab6349e66bc3
1 /*
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 FUNCTION
20 <<ftell>>, <<ftello>>---return position in a stream or file
22 INDEX
23 ftell
24 INDEX
25 ftello
26 INDEX
27 _ftell_r
28 INDEX
29 _ftello_r
31 ANSI_SYNOPSIS
32 #include <stdio.h>
33 long ftell(FILE *<[fp]>);
34 off_t ftello(FILE *<[fp]>);
35 long _ftell_r(struct _reent *<[ptr]>, FILE *<[fp]>);
36 off_t _ftello_r(struct _reent *<[ptr]>, FILE *<[fp]>);
38 TRAD_SYNOPSIS
39 #include <stdio.h>
40 long ftell(<[fp]>)
41 FILE *<[fp]>;
43 off_t ftello(<[fp]>)
44 FILE *<[fp]>;
46 long _ftell_r(<[ptr]>, <[fp]>)
47 struct _reent *<[ptr]>;
48 FILE *<[fp]>;
50 off_t _ftello_r(<[ptr]>, <[fp]>)
51 struct _reent *<[ptr]>;
52 FILE *<[fp]>;
54 DESCRIPTION
55 Objects of type <<FILE>> can have a ``position'' that records how much
56 of the file your program has already read. Many of the <<stdio>> functions
57 depend on this position, and many change it as a side effect.
59 The result of <<ftell>>/<<ftello>> is the current position for a file
60 identified by <[fp]>. If you record this result, you can later
61 use it with <<fseek>>/<<fseeko>> to return the file to this
62 position. The difference between <<ftell>> and <<ftello>> is that
63 <<ftell>> returns <<long>> and <<ftello>> returns <<off_t>>.
65 In the current implementation, <<ftell>>/<<ftello>> simply uses a character
66 count to represent the file position; this is the same number that
67 would be recorded by <<fgetpos>>.
69 RETURNS
70 <<ftell>>/<<ftello>> return the file position, if possible. If they cannot do
71 this, they return <<-1L>>. Failure occurs on streams that do not support
72 positioning; the global <<errno>> indicates this condition with the
73 value <<ESPIPE>>.
75 PORTABILITY
76 <<ftell>> is required by the ANSI C standard, but the meaning of its
77 result (when successful) is not specified beyond requiring that it be
78 acceptable as an argument to <<fseek>>. In particular, other
79 conforming C implementations may return a different result from
80 <<ftell>> than what <<fgetpos>> records.
82 <<ftello>> is defined by the Single Unix specification.
84 No supporting OS subroutines are required.
87 #if defined(LIBC_SCCS) && !defined(lint)
88 static char sccsid[] = "%W% (Berkeley) %G%";
89 #endif /* LIBC_SCCS and not lint */
92 * ftell: return current offset.
95 #include <stdio.h>
96 #include <errno.h>
98 #include "local.h"
100 long
101 _DEFUN (_ftell_r, (ptr, fp),
102 struct _reent *ptr _AND
103 register FILE * fp)
105 _fpos_t pos;
107 _flockfile(fp);
109 /* Ensure stdio is set up. */
111 CHECK_INIT (fp);
113 if (fp->_seek == NULL)
115 ptr->_errno = ESPIPE;
116 _funlockfile(fp);
117 return -1L;
120 /* Find offset of underlying I/O object, then
121 adjust for buffered bytes. */
122 fflush(fp); /* may adjust seek offset on append stream */
123 if (fp->_flags & __SOFF)
124 pos = fp->_offset;
125 else
127 pos = (*fp->_seek) (fp->_cookie, (_fpos_t) 0, SEEK_CUR);
128 if (pos == -1L)
130 _funlockfile(fp);
131 return pos;
134 if (fp->_flags & __SRD)
137 * Reading. Any unread characters (including
138 * those from ungetc) cause the position to be
139 * smaller than that in the underlying object.
141 pos -= fp->_r;
142 if (HASUB (fp))
143 pos -= fp->_ur;
145 else if (fp->_flags & __SWR && fp->_p != NULL)
148 * Writing. Any buffered characters cause the
149 * position to be greater than that in the
150 * underlying object.
152 pos += fp->_p - fp->_bf._base;
155 _funlockfile(fp);
156 return pos;
159 #ifndef _REENT_ONLY
161 long
162 _DEFUN (ftell, (fp),
163 register FILE * fp)
165 return _ftell_r (_REENT, fp);
168 #endif /* !_REENT_ONLY */