xref: /freebsd/lib/libc/stdio/ftell.c (revision 2ff678f5bb48d68e0635f3986a9068e72289c919)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Chris Torek.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
38ce51cf03SJames Raynard #if 0
394f02b68aSPeter Wemm static char sccsid[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
40ce51cf03SJames Raynard #endif
41ce51cf03SJames Raynard static const char rcsid[] =
427f3dea24SPeter Wemm   "$FreeBSD$";
4358f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4458f0484fSRodney W. Grimes 
45d201fe46SDaniel Eischen #include "namespace.h"
467307d07dSDmitrij Tejblum #include <sys/types.h>
4758f0484fSRodney W. Grimes #include <errno.h>
48e54bc118SAndrey A. Chernov #include <limits.h>
49e54bc118SAndrey A. Chernov #include <stdio.h>
50d201fe46SDaniel Eischen #include "un-namespace.h"
5158f0484fSRodney W. Grimes #include "local.h"
52ec216c26SJohn Birrell #include "libc_private.h"
5358f0484fSRodney W. Grimes 
5458f0484fSRodney W. Grimes /*
557307d07dSDmitrij Tejblum  * standard ftell function.
5658f0484fSRodney W. Grimes  */
5758f0484fSRodney W. Grimes long
5858f0484fSRodney W. Grimes ftell(fp)
59880787f0SPaul Richards 	register FILE *fp;
6058f0484fSRodney W. Grimes {
617307d07dSDmitrij Tejblum 	register off_t rv;
62e54bc118SAndrey A. Chernov 
637307d07dSDmitrij Tejblum 	rv = ftello(fp);
64e54bc118SAndrey A. Chernov 	if (rv > LONG_MAX) {
657307d07dSDmitrij Tejblum 		errno = EOVERFLOW;
667307d07dSDmitrij Tejblum 		return (-1);
677307d07dSDmitrij Tejblum 	}
687307d07dSDmitrij Tejblum 	return (rv);
697307d07dSDmitrij Tejblum }
707307d07dSDmitrij Tejblum 
717307d07dSDmitrij Tejblum /*
727307d07dSDmitrij Tejblum  * ftello: return current offset.
737307d07dSDmitrij Tejblum  */
747307d07dSDmitrij Tejblum off_t
757307d07dSDmitrij Tejblum ftello(fp)
767307d07dSDmitrij Tejblum 	register FILE *fp;
777307d07dSDmitrij Tejblum {
782ff678f5SAndrey A. Chernov 	register off_t rv;
792ff678f5SAndrey A. Chernov 
802ff678f5SAndrey A. Chernov 	FLOCKFILE(fp);
812ff678f5SAndrey A. Chernov 	rv = _ftello(fp);
822ff678f5SAndrey A. Chernov 	FUNLOCKFILE(fp);
832ff678f5SAndrey A. Chernov 	return (rv);
842ff678f5SAndrey A. Chernov }
852ff678f5SAndrey A. Chernov 
862ff678f5SAndrey A. Chernov off_t
872ff678f5SAndrey A. Chernov _ftello(fp)
882ff678f5SAndrey A. Chernov 	register FILE *fp;
892ff678f5SAndrey A. Chernov {
90ee758104SAndrey A. Chernov 	register fpos_t pos, spos;
91e54bc118SAndrey A. Chernov 	size_t n;
9258f0484fSRodney W. Grimes 
9358f0484fSRodney W. Grimes 	if (fp->_seek == NULL) {
9458f0484fSRodney W. Grimes 		errno = ESPIPE;			/* historic practice */
95e54bc118SAndrey A. Chernov 		return (-1);
9658f0484fSRodney W. Grimes 	}
9758f0484fSRodney W. Grimes 
9858f0484fSRodney W. Grimes 	/*
9958f0484fSRodney W. Grimes 	 * Find offset of underlying I/O object, then
10058f0484fSRodney W. Grimes 	 * adjust for buffered bytes.
10158f0484fSRodney W. Grimes 	 */
1022ff678f5SAndrey A. Chernov 	if (fp->_flags & __SOFF) {
10358f0484fSRodney W. Grimes 		pos = fp->_offset;
1042ff678f5SAndrey A. Chernov 		spos = -1;
1052ff678f5SAndrey A. Chernov 	} else {
1062ff678f5SAndrey A. Chernov get_real_pos:
1072ff678f5SAndrey A. Chernov 		spos = pos = (*fp->_seek)(fp->_cookie, (fpos_t)0, SEEK_CUR);
1082ff678f5SAndrey A. Chernov 		if (pos == -1)
109e54bc118SAndrey A. Chernov 			return (-1);
11058f0484fSRodney W. Grimes 	}
11158f0484fSRodney W. Grimes 	if (fp->_flags & __SRD) {
11258f0484fSRodney W. Grimes 		/*
11358f0484fSRodney W. Grimes 		 * Reading.  Any unread characters (including
11458f0484fSRodney W. Grimes 		 * those from ungetc) cause the position to be
11558f0484fSRodney W. Grimes 		 * smaller than that in the underlying object.
11658f0484fSRodney W. Grimes 		 */
11758f0484fSRodney W. Grimes 		pos -= fp->_r;
118e54bc118SAndrey A. Chernov 		if (pos < 0) {
119e54bc118SAndrey A. Chernov 			if (HASUB(fp)) {
120e54bc118SAndrey A. Chernov 				fp->_p -= pos;
121e54bc118SAndrey A. Chernov 				fp->_r += pos;
122e54bc118SAndrey A. Chernov 				pos = 0;
123e54bc118SAndrey A. Chernov 			} else {
1242ff678f5SAndrey A. Chernov 				if (spos == -1)
1252ff678f5SAndrey A. Chernov 					goto get_real_pos;
126ee758104SAndrey A. Chernov 				fp->_p = fp->_bf._base;
127ee758104SAndrey A. Chernov 				fp->_r = 0;
128ee758104SAndrey A. Chernov 				pos = spos;
129e54bc118SAndrey A. Chernov 			}
130e54bc118SAndrey A. Chernov 		}
131e54bc118SAndrey A. Chernov 		if (HASUB(fp)) {
13258f0484fSRodney W. Grimes 			pos -= fp->_ur;
133e54bc118SAndrey A. Chernov 			if (pos < 0) {
13457935eebSAndrey A. Chernov 				if (-pos <= fp->_r) {
13557935eebSAndrey A. Chernov 					fp->_p -= pos;
13657935eebSAndrey A. Chernov 					fp->_r += pos;
13757935eebSAndrey A. Chernov 					pos = 0;
13857935eebSAndrey A. Chernov 				} else {
1392ff678f5SAndrey A. Chernov 					if (spos == -1)
1402ff678f5SAndrey A. Chernov 						goto get_real_pos;
141ee758104SAndrey A. Chernov 					fp->_p = fp->_bf._base;
142ee758104SAndrey A. Chernov 					fp->_r = 0;
143ee758104SAndrey A. Chernov 					FREEUB(fp);
144ee758104SAndrey A. Chernov 					pos = spos;
145e54bc118SAndrey A. Chernov 				}
146e54bc118SAndrey A. Chernov 			}
14757935eebSAndrey A. Chernov 		}
148e54bc118SAndrey A. Chernov 	} else if ((fp->_flags & __SWR) && fp->_p != NULL) {
14958f0484fSRodney W. Grimes 		/*
15058f0484fSRodney W. Grimes 		 * Writing.  Any buffered characters cause the
15158f0484fSRodney W. Grimes 		 * position to be greater than that in the
15258f0484fSRodney W. Grimes 		 * underlying object.
15358f0484fSRodney W. Grimes 		 */
154e54bc118SAndrey A. Chernov 		n = fp->_p - fp->_bf._base;
155e54bc118SAndrey A. Chernov 		if (pos > OFF_MAX - n) {
156e54bc118SAndrey A. Chernov 			errno = EOVERFLOW;
157e54bc118SAndrey A. Chernov 			return (-1);
158e54bc118SAndrey A. Chernov 		}
159e54bc118SAndrey A. Chernov 		pos += n;
16058f0484fSRodney W. Grimes 	}
16158f0484fSRodney W. Grimes 	return (pos);
16258f0484fSRodney W. Grimes }
163