xref: /netbsd-src/lib/libc/stdio/ftello.c (revision de001ba2a53bc20787a4ae96999d5ddddde49d02)
1*de001ba2Schristos /*	$NetBSD: ftello.c,v 1.7 2012/03/27 15:05:42 christos Exp $	*/
2dae36061Skleink 
3dae36061Skleink /*-
4dae36061Skleink  * Copyright (c) 1990, 1993
5dae36061Skleink  *	The Regents of the University of California.  All rights reserved.
6dae36061Skleink  *
7dae36061Skleink  * This code is derived from software contributed to Berkeley by
8dae36061Skleink  * Chris Torek.
9dae36061Skleink  *
10dae36061Skleink  * Redistribution and use in source and binary forms, with or without
11dae36061Skleink  * modification, are permitted provided that the following conditions
12dae36061Skleink  * are met:
13dae36061Skleink  * 1. Redistributions of source code must retain the above copyright
14dae36061Skleink  *    notice, this list of conditions and the following disclaimer.
15dae36061Skleink  * 2. Redistributions in binary form must reproduce the above copyright
16dae36061Skleink  *    notice, this list of conditions and the following disclaimer in the
17dae36061Skleink  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
19dae36061Skleink  *    may be used to endorse or promote products derived from this software
20dae36061Skleink  *    without specific prior written permission.
21dae36061Skleink  *
22dae36061Skleink  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23dae36061Skleink  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24dae36061Skleink  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25dae36061Skleink  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26dae36061Skleink  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27dae36061Skleink  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28dae36061Skleink  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29dae36061Skleink  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30dae36061Skleink  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31dae36061Skleink  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32dae36061Skleink  * SUCH DAMAGE.
33dae36061Skleink  */
34dae36061Skleink 
35dae36061Skleink #include <sys/cdefs.h>
36dae36061Skleink #if defined(LIBC_SCCS) && !defined(lint)
37*de001ba2Schristos __RCSID("$NetBSD: ftello.c,v 1.7 2012/03/27 15:05:42 christos Exp $");
38dae36061Skleink #endif /* LIBC_SCCS and not lint */
39dae36061Skleink 
40dae36061Skleink #include "namespace.h"
41dae36061Skleink #include <assert.h>
42dae36061Skleink #include <errno.h>
43dae36061Skleink #include <stdio.h>
44dae36061Skleink #include "reentrant.h"
453fdac2b8Sthorpej #include "local.h"
46dae36061Skleink 
477d93b767Skleink #ifdef __weak_alias
__weak_alias(ftello,_ftello)487d93b767Skleink __weak_alias(ftello, _ftello)
497d93b767Skleink #endif
507d93b767Skleink 
51dae36061Skleink /*
52dae36061Skleink  * ftell: return current offset.
53dae36061Skleink  */
54dae36061Skleink off_t
55526d9427Schristos ftello(FILE *fp)
56dae36061Skleink {
571897181aSchristos 	off_t pos;
58dae36061Skleink 
59dae36061Skleink 
60dae36061Skleink 	FLOCKFILE(fp);
61dae36061Skleink 
62dae36061Skleink 	if (fp->_seek == NULL) {
63dae36061Skleink 		FUNLOCKFILE(fp);
64dae36061Skleink 		errno = ESPIPE;			/* historic practice */
65526d9427Schristos 		return (off_t)-1;
66dae36061Skleink 	}
67dae36061Skleink 
68dae36061Skleink 	/*
69dae36061Skleink 	 * Find offset of underlying I/O object, then
70dae36061Skleink 	 * adjust for buffered bytes.
71dae36061Skleink 	 */
72*de001ba2Schristos 	(void)__sflush(fp); /* may adjust seek offset on append stream */
73dae36061Skleink 	if (fp->_flags & __SOFF)
74dae36061Skleink 		pos = fp->_offset;
75dae36061Skleink 	else {
761897181aSchristos 		pos = (*fp->_seek)(fp->_cookie, (off_t)0, SEEK_CUR);
771897181aSchristos 		if (pos == (off_t)-1) {
78dae36061Skleink 			FUNLOCKFILE(fp);
79526d9427Schristos 			return pos;
80dae36061Skleink 		}
81dae36061Skleink 	}
82dae36061Skleink 	if (fp->_flags & __SRD) {
83dae36061Skleink 		/*
84dae36061Skleink 		 * Reading.  Any unread characters (including
85dae36061Skleink 		 * those from ungetc) cause the position to be
86dae36061Skleink 		 * smaller than that in the underlying object.
87dae36061Skleink 		 */
88dae36061Skleink 		pos -= fp->_r;
89dae36061Skleink 		if (HASUB(fp))
90dae36061Skleink 			pos -= fp->_ur;
91dae36061Skleink 	} else if (fp->_flags & __SWR && fp->_p != NULL) {
92dae36061Skleink 		/*
93dae36061Skleink 		 * Writing.  Any buffered characters cause the
94dae36061Skleink 		 * position to be greater than that in the
95dae36061Skleink 		 * underlying object.
96dae36061Skleink 		 */
97dae36061Skleink 		pos += fp->_p - fp->_bf._base;
98dae36061Skleink 	}
99dae36061Skleink 	FUNLOCKFILE(fp);
100526d9427Schristos 	return pos;
101dae36061Skleink }
102