ftell.c revision 290549
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1990, 1993
31573Srgrimes *	The Regents of the University of California.  All rights reserved.
41573Srgrimes *
51573Srgrimes * This code is derived from software contributed to Berkeley by
61573Srgrimes * Chris Torek.
71573Srgrimes *
81573Srgrimes * Redistribution and use in source and binary forms, with or without
91573Srgrimes * modification, are permitted provided that the following conditions
101573Srgrimes * are met:
111573Srgrimes * 1. Redistributions of source code must retain the above copyright
121573Srgrimes *    notice, this list of conditions and the following disclaimer.
131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer in the
151573Srgrimes *    documentation and/or other materials provided with the distribution.
16249808Semaste * 3. Neither the name of the University nor the names of its contributors
171573Srgrimes *    may be used to endorse or promote products derived from this software
181573Srgrimes *    without specific prior written permission.
191573Srgrimes *
201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
231573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
301573Srgrimes * SUCH DAMAGE.
311573Srgrimes */
321573Srgrimes
331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint)
3423663Speterstatic char sccsid[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
351573Srgrimes#endif /* LIBC_SCCS and not lint */
3692986Sobrien#include <sys/cdefs.h>
3792986Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdio/ftell.c 290549 2015-11-08 18:00:44Z ache $");
381573Srgrimes
3971579Sdeischen#include "namespace.h"
4043782Sdt#include <sys/types.h>
4182588Sache#include <errno.h>
42268997Sache#include <fcntl.h>
4382588Sache#include <limits.h>
441573Srgrimes#include <stdio.h>
4571579Sdeischen#include "un-namespace.h"
461573Srgrimes#include "local.h"
4735129Sjb#include "libc_private.h"
481573Srgrimes
491573Srgrimes/*
5043782Sdt * standard ftell function.
511573Srgrimes */
521573Srgrimeslong
53249810Semasteftell(FILE *fp)
541573Srgrimes{
5592889Sobrien	off_t rv;
5682588Sache
5743782Sdt	rv = ftello(fp);
5882588Sache	if (rv > LONG_MAX) {
5943782Sdt		errno = EOVERFLOW;
6043782Sdt		return (-1);
6143782Sdt	}
6243782Sdt	return (rv);
6343782Sdt}
6443782Sdt
6543782Sdt/*
6643782Sdt * ftello: return current offset.
6743782Sdt */
6843782Sdtoff_t
69249810Semasteftello(FILE *fp)
7043782Sdt{
7182709Sache	fpos_t rv;
7282709Sache	int ret;
7382668Sache
7482668Sache	FLOCKFILE(fp);
7582709Sache	ret = _ftello(fp, &rv);
7682668Sache	FUNLOCKFILE(fp);
7782709Sache	if (ret)
7882709Sache		return (-1);
7982736Sache	if (rv < 0) {   /* Unspecified value because of ungetc() at 0 */
8082736Sache		errno = ESPIPE;
8182736Sache		return (-1);
8282736Sache	}
8382668Sache	return (rv);
8482668Sache}
8582668Sache
8682709Sacheint
87249810Semaste_ftello(FILE *fp, fpos_t *offset)
8882668Sache{
8992889Sobrien	fpos_t pos;
9082588Sache	size_t n;
911573Srgrimes
921573Srgrimes	if (fp->_seek == NULL) {
931573Srgrimes		errno = ESPIPE;			/* historic practice */
9482709Sache		return (1);
951573Srgrimes	}
961573Srgrimes
971573Srgrimes	/*
981573Srgrimes	 * Find offset of underlying I/O object, then
991573Srgrimes	 * adjust for buffered bytes.
1001573Srgrimes	 */
101290549Sache	if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
102290549Sache	    fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
103290549Sache	    ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
104290549Sache		if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
105290549Sache			if (errno == ESPIPE ||
106290549Sache			    (fp->_flags & __SOPT) || __sflush(fp) ||
107290549Sache			    (pos = _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
108290549Sache				return (1);
109290549Sache			else {
110290549Sache				*offset = pos;
111290549Sache				return (0);
112290549Sache			}
113290549Sache		}
114290549Sache	} else if (fp->_flags & __SOFF)
1151573Srgrimes		pos = fp->_offset;
11682734Sache	else {
11782807Sache		pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
11882668Sache		if (pos == -1)
11982709Sache			return (1);
1201573Srgrimes	}
1211573Srgrimes	if (fp->_flags & __SRD) {
1221573Srgrimes		/*
1231573Srgrimes		 * Reading.  Any unread characters (including
1241573Srgrimes		 * those from ungetc) cause the position to be
1251573Srgrimes		 * smaller than that in the underlying object.
1261573Srgrimes		 */
12782709Sache		if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) {
12882734Sache			fp->_flags |= __SERR;
12982734Sache			errno = EIO;
13082734Sache			return (1);
13182588Sache		}
13282709Sache		if (HASUB(fp))
13382709Sache			pos -= fp->_r;  /* Can be negative at this point. */
134290232Sache	} else if ((fp->_flags & __SWR) && fp->_p != NULL &&
135290232Sache	    (n = fp->_p - fp->_bf._base) > 0) {
136290230Sache		/*
137290230Sache		 * Writing.  Any buffered characters cause the
138290230Sache		 * position to be greater than that in the
139290230Sache		 * underlying object.
140290230Sache		 */
141290231Sache		if (pos > OFF_MAX - n) {
142290231Sache			errno = EOVERFLOW;
143290231Sache			return (1);
144290231Sache		}
14582588Sache		pos += n;
1461573Srgrimes	}
14782709Sache	*offset = pos;
14882709Sache	return (0);
1491573Srgrimes}
150