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. 161573Srgrimes * 4. 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 521573Srgrimesftell(fp) 5392889Sobrien 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 6943782Sdtftello(fp) 7092889Sobrien FILE *fp; 7143782Sdt{ 7282709Sache fpos_t rv; 7382709Sache int ret; 7482668Sache 7582668Sache FLOCKFILE(fp); 7682709Sache ret = _ftello(fp, &rv); 7782668Sache FUNLOCKFILE(fp); 7882709Sache if (ret) 7982709Sache return (-1); 8082736Sache if (rv < 0) { /* Unspecified value because of ungetc() at 0 */ 8182736Sache errno = ESPIPE; 8282736Sache return (-1); 8382736Sache } 8482668Sache return (rv); 8582668Sache} 8682668Sache 8782709Sacheint 8882709Sache_ftello(fp, offset) 8992889Sobrien FILE *fp; 9082709Sache fpos_t *offset; 9182668Sache{ 9292889Sobrien fpos_t pos; 9382588Sache size_t n; 941573Srgrimes 951573Srgrimes if (fp->_seek == NULL) { 961573Srgrimes errno = ESPIPE; /* historic practice */ 9782709Sache return (1); 981573Srgrimes } 991573Srgrimes 1001573Srgrimes /* 1011573Srgrimes * Find offset of underlying I/O object, then 1021573Srgrimes * adjust for buffered bytes. 1031573Srgrimes */ 10482734Sache if (fp->_flags & __SOFF) 1051573Srgrimes pos = fp->_offset; 10682734Sache else { 10782807Sache pos = _sseek(fp, (fpos_t)0, SEEK_CUR); 10882668Sache if (pos == -1) 10982709Sache return (1); 1101573Srgrimes } 1111573Srgrimes if (fp->_flags & __SRD) { 1121573Srgrimes /* 1131573Srgrimes * Reading. Any unread characters (including 1141573Srgrimes * those from ungetc) cause the position to be 1151573Srgrimes * smaller than that in the underlying object. 1161573Srgrimes */ 11782709Sache if ((pos -= (HASUB(fp) ? fp->_ur : fp->_r)) < 0) { 11882734Sache fp->_flags |= __SERR; 11982734Sache errno = EIO; 12082734Sache return (1); 12182588Sache } 12282709Sache if (HASUB(fp)) 12382709Sache pos -= fp->_r; /* Can be negative at this point. */ 12482588Sache } else if ((fp->_flags & __SWR) && fp->_p != NULL) { 1251573Srgrimes /* 1261573Srgrimes * Writing. Any buffered characters cause the 1271573Srgrimes * position to be greater than that in the 1281573Srgrimes * underlying object. 1291573Srgrimes */ 13082588Sache n = fp->_p - fp->_bf._base; 13182588Sache if (pos > OFF_MAX - n) { 13282588Sache errno = EOVERFLOW; 13382709Sache return (1); 13482588Sache } 13582588Sache pos += n; 1361573Srgrimes } 13782709Sache *offset = pos; 13882709Sache return (0); 1391573Srgrimes} 140