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$");
381573Srgrimes
3971579Sdeischen#include "namespace.h"
4043782Sdt#include <sys/types.h>
4182588Sache#include <errno.h>
4282588Sache#include <limits.h>
431573Srgrimes#include <stdio.h>
4471579Sdeischen#include "un-namespace.h"
451573Srgrimes#include "local.h"
4635129Sjb#include "libc_private.h"
471573Srgrimes
481573Srgrimes/*
4943782Sdt * standard ftell function.
501573Srgrimes */
511573Srgrimeslong
52249810Semasteftell(FILE *fp)
531573Srgrimes{
5492889Sobrien	off_t rv;
5582588Sache
5643782Sdt	rv = ftello(fp);
5782588Sache	if (rv > LONG_MAX) {
5843782Sdt		errno = EOVERFLOW;
5943782Sdt		return (-1);
6043782Sdt	}
6143782Sdt	return (rv);
6243782Sdt}
6343782Sdt
6443782Sdt/*
6543782Sdt * ftello: return current offset.
6643782Sdt */
6743782Sdtoff_t
68249810Semasteftello(FILE *fp)
6943782Sdt{
7082709Sache	fpos_t rv;
7182709Sache	int ret;
7282668Sache
7382668Sache	FLOCKFILE(fp);
7482709Sache	ret = _ftello(fp, &rv);
7582668Sache	FUNLOCKFILE(fp);
7682709Sache	if (ret)
7782709Sache		return (-1);
7882736Sache	if (rv < 0) {   /* Unspecified value because of ungetc() at 0 */
7982736Sache		errno = ESPIPE;
8082736Sache		return (-1);
8182736Sache	}
8282668Sache	return (rv);
8382668Sache}
8482668Sache
8582709Sacheint
86249810Semaste_ftello(FILE *fp, fpos_t *offset)
8782668Sache{
8892889Sobrien	fpos_t pos;
8982588Sache	size_t n;
901573Srgrimes
911573Srgrimes	if (fp->_seek == NULL) {
921573Srgrimes		errno = ESPIPE;			/* historic practice */
9382709Sache		return (1);
941573Srgrimes	}
951573Srgrimes
961573Srgrimes	/*
971573Srgrimes	 * Find offset of underlying I/O object, then
981573Srgrimes	 * adjust for buffered bytes.
991573Srgrimes	 */
100291050Sache	if (!(fp->_flags & __SRD) && (fp->_flags & __SWR) &&
101291050Sache	    fp->_p != NULL && fp->_p - fp->_bf._base > 0 &&
102291050Sache	    ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
103291050Sache		pos = _sseek(fp, (fpos_t)0, SEEK_END);
104291050Sache		if (pos == -1)
105291050Sache			return (1);
106291050Sache	} else if (fp->_flags & __SOFF)
1071573Srgrimes		pos = fp->_offset;
10882734Sache	else {
10982807Sache		pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
11082668Sache		if (pos == -1)
11182709Sache			return (1);
1121573Srgrimes	}
1131573Srgrimes	if (fp->_flags & __SRD) {
1141573Srgrimes		/*
1151573Srgrimes		 * Reading.  Any unread characters (including
1161573Srgrimes		 * those from ungetc) cause the position to be
1171573Srgrimes		 * smaller than that in the underlying object.
1181573Srgrimes		 */
11982709Sache		if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) {
12082734Sache			fp->_flags |= __SERR;
12182734Sache			errno = EIO;
12282734Sache			return (1);
12382588Sache		}
12482709Sache		if (HASUB(fp))
12582709Sache			pos -= fp->_r;  /* Can be negative at this point. */
126290544Sache	} else if ((fp->_flags & __SWR) && fp->_p != NULL &&
127290544Sache	    (n = fp->_p - fp->_bf._base) > 0) {
1281573Srgrimes		/*
1291573Srgrimes		 * Writing.  Any buffered characters cause the
1301573Srgrimes		 * position to be greater than that in the
1311573Srgrimes		 * underlying object.
1321573Srgrimes		 */
13382588Sache		if (pos > OFF_MAX - n) {
13482588Sache			errno = EOVERFLOW;
13582709Sache			return (1);
13682588Sache		}
13782588Sache		pos += n;
1381573Srgrimes	}
13982709Sache	*offset = pos;
14082709Sache	return (0);
1411573Srgrimes}
142