xref: /freebsd/lib/libc/stdio/ftell.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Chris Torek.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
181d8053c5SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
364f02b68aSPeter Wemm static char sccsid[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
38333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
39333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
4058f0484fSRodney W. Grimes 
41d201fe46SDaniel Eischen #include "namespace.h"
427307d07dSDmitrij Tejblum #include <sys/types.h>
4358f0484fSRodney W. Grimes #include <errno.h>
44e54bc118SAndrey A. Chernov #include <limits.h>
45e54bc118SAndrey A. Chernov #include <stdio.h>
46d201fe46SDaniel Eischen #include "un-namespace.h"
4758f0484fSRodney W. Grimes #include "local.h"
48ec216c26SJohn Birrell #include "libc_private.h"
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes /*
517307d07dSDmitrij Tejblum  * standard ftell function.
5258f0484fSRodney W. Grimes  */
5358f0484fSRodney W. Grimes long
545a6307cfSEd Maste ftell(FILE *fp)
5558f0484fSRodney W. Grimes {
568fb3f3f6SDavid E. O'Brien 	off_t rv;
57e54bc118SAndrey A. Chernov 
587307d07dSDmitrij Tejblum 	rv = ftello(fp);
59e54bc118SAndrey A. Chernov 	if (rv > LONG_MAX) {
607307d07dSDmitrij Tejblum 		errno = EOVERFLOW;
617307d07dSDmitrij Tejblum 		return (-1);
627307d07dSDmitrij Tejblum 	}
637307d07dSDmitrij Tejblum 	return (rv);
647307d07dSDmitrij Tejblum }
657307d07dSDmitrij Tejblum 
667307d07dSDmitrij Tejblum /*
677307d07dSDmitrij Tejblum  * ftello: return current offset.
687307d07dSDmitrij Tejblum  */
697307d07dSDmitrij Tejblum off_t
705a6307cfSEd Maste ftello(FILE *fp)
717307d07dSDmitrij Tejblum {
7265efd812SAndrey A. Chernov 	fpos_t rv;
7365efd812SAndrey A. Chernov 	int ret;
742ff678f5SAndrey A. Chernov 
752ff678f5SAndrey A. Chernov 	FLOCKFILE(fp);
7665efd812SAndrey A. Chernov 	ret = _ftello(fp, &rv);
772ff678f5SAndrey A. Chernov 	FUNLOCKFILE(fp);
7865efd812SAndrey A. Chernov 	if (ret)
7965efd812SAndrey A. Chernov 		return (-1);
806ff604a7SAndrey A. Chernov 	if (rv < 0) {   /* Unspecified value because of ungetc() at 0 */
816ff604a7SAndrey A. Chernov 		errno = ESPIPE;
826ff604a7SAndrey A. Chernov 		return (-1);
836ff604a7SAndrey A. Chernov 	}
842ff678f5SAndrey A. Chernov 	return (rv);
852ff678f5SAndrey A. Chernov }
862ff678f5SAndrey A. Chernov 
8765efd812SAndrey A. Chernov int
885a6307cfSEd Maste _ftello(FILE *fp, fpos_t *offset)
892ff678f5SAndrey A. Chernov {
908fb3f3f6SDavid E. O'Brien 	fpos_t pos;
91e54bc118SAndrey A. Chernov 	size_t n;
9258f0484fSRodney W. Grimes 
9358f0484fSRodney W. Grimes 	if (fp->_seek == NULL) {
9458f0484fSRodney W. Grimes 		errno = ESPIPE;			/* historic practice */
9565efd812SAndrey 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 	 */
102881e47bbSAndrey A. Chernov 	if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
103881e47bbSAndrey A. Chernov 	    fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
104881e47bbSAndrey A. Chernov 	    ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
10508566281SAndrey A. Chernov 		pos = _sseek(fp, (fpos_t)0, SEEK_END);
10608566281SAndrey A. Chernov 		if (pos == -1)
107881e47bbSAndrey A. Chernov 			return (1);
108881e47bbSAndrey A. Chernov 	} else if (fp->_flags & __SOFF)
10958f0484fSRodney W. Grimes 		pos = fp->_offset;
110b13ed883SAndrey A. Chernov 	else {
111924888f9SAndrey A. Chernov 		pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
1122ff678f5SAndrey A. Chernov 		if (pos == -1)
11365efd812SAndrey A. Chernov 			return (1);
11458f0484fSRodney W. Grimes 	}
11558f0484fSRodney W. Grimes 	if (fp->_flags & __SRD) {
11658f0484fSRodney W. Grimes 		/*
11758f0484fSRodney W. Grimes 		 * Reading.  Any unread characters (including
11858f0484fSRodney W. Grimes 		 * those from ungetc) cause the position to be
11958f0484fSRodney W. Grimes 		 * smaller than that in the underlying object.
12058f0484fSRodney W. Grimes 		 */
12165efd812SAndrey A. Chernov 		if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) {
122b13ed883SAndrey A. Chernov 			fp->_flags |= __SERR;
123b13ed883SAndrey A. Chernov 			errno = EIO;
124b13ed883SAndrey A. Chernov 			return (1);
125e54bc118SAndrey A. Chernov 		}
12665efd812SAndrey A. Chernov 		if (HASUB(fp))
12765efd812SAndrey A. Chernov 			pos -= fp->_r;  /* Can be negative at this point. */
128190d73a7SAndrey A. Chernov 	} else if ((fp->_flags & __SWR) && fp->_p != NULL &&
129190d73a7SAndrey A. Chernov 	    (n = fp->_p - fp->_bf._base) > 0) {
130ec6cd152SAndrey A. Chernov 		/*
131ec6cd152SAndrey A. Chernov 		 * Writing.  Any buffered characters cause the
132ec6cd152SAndrey A. Chernov 		 * position to be greater than that in the
133ec6cd152SAndrey A. Chernov 		 * underlying object.
134ec6cd152SAndrey A. Chernov 		 */
1354fe4a788SAndrey A. Chernov 		if (pos > OFF_MAX - n) {
1364fe4a788SAndrey A. Chernov 			errno = EOVERFLOW;
1374fe4a788SAndrey A. Chernov 			return (1);
138b956b176SAndrey A. Chernov 		}
139e54bc118SAndrey A. Chernov 		pos += n;
14058f0484fSRodney W. Grimes 	}
14165efd812SAndrey A. Chernov 	*offset = pos;
14265efd812SAndrey A. Chernov 	return (0);
14358f0484fSRodney W. Grimes }
144