ftell.c revision 290230
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 290230 2015-11-01 06:15:14Z 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	 */
10182734Sache	if (fp->_flags & __SOFF)
1021573Srgrimes		pos = fp->_offset;
10382734Sache	else {
10482807Sache		pos = _sseek(fp, (fpos_t)0, SEEK_CUR);
10582668Sache		if (pos == -1)
10682709Sache			return (1);
1071573Srgrimes	}
1081573Srgrimes	if (fp->_flags & __SRD) {
1091573Srgrimes		/*
1101573Srgrimes		 * Reading.  Any unread characters (including
1111573Srgrimes		 * those from ungetc) cause the position to be
1121573Srgrimes		 * smaller than that in the underlying object.
1131573Srgrimes		 */
11482709Sache		if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) {
11582734Sache			fp->_flags |= __SERR;
11682734Sache			errno = EIO;
11782734Sache			return (1);
11882588Sache		}
11982709Sache		if (HASUB(fp))
12082709Sache			pos -= fp->_r;  /* Can be negative at this point. */
12182588Sache	} else if ((fp->_flags & __SWR) && fp->_p != NULL) {
122290230Sache		/*
123290230Sache		 * Writing.  Any buffered characters cause the
124290230Sache		 * position to be greater than that in the
125290230Sache		 * underlying object.
126290230Sache		 */
127290230Sache		n = fp->_p - fp->_bf._base;
128290230Sache		if (pos > OFF_MAX - n) {
129290230Sache			errno = EOVERFLOW;
130290230Sache			return (1);
131290230Sache		}
132290230Sache		if (n > 0 &&
133290230Sache		    ((fp->_flags & __SAPP) || (fp->_flags2 & __S2OAP))) {
134289863Sache			int serrno = errno;
135289863Sache
136289863Sache			errno = 0;
137289863Sache			if ((pos = _sseek(fp, (fpos_t)0, SEEK_END)) == -1) {
138289863Sache				if (errno == ESPIPE ||
139289863Sache				    (fp->_flags & __SOPT) || __sflush(fp) ||
140289863Sache				    (pos =
141289863Sache				    _sseek(fp, (fpos_t)0, SEEK_CUR)) == -1)
142289863Sache					return (1);
143289863Sache				else {
144289863Sache					errno = serrno;
145289863Sache					*offset = pos;
146289863Sache					return (0);
147289863Sache				}
148268997Sache			}
149289863Sache			errno = serrno;
150268997Sache		}
15182588Sache		pos += n;
1521573Srgrimes	}
15382709Sache	*offset = pos;
15482709Sache	return (0);
1551573Srgrimes}
156