xref: /titanic_50/usr/src/lib/libc/port/stdio/ftello.c (revision 275c9da86e89f8abf71135cf63d9fc23671b2e60)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * Return file offset.
35  * Coordinates with buffering.
36  */
37 
38 #include <sys/feature_tests.h>
39 
40 #if !defined(_LP64)
41 #pragma weak ftello64 = _ftello64
42 #endif
43 #pragma weak ftello = _ftello
44 
45 #include "synonyms.h"
46 #include "file64.h"
47 #include "mtlib.h"
48 #include <sys/types.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <thread.h>
52 #include <synch.h>
53 #include <errno.h>
54 #include <limits.h>
55 #include <fcntl.h>
56 #include <stddef.h>
57 #include "stdiom.h"
58 
59 #if !defined(_LP64)
60 
61 off64_t
62 ftello64(FILE *iop)
63 {
64 	ptrdiff_t adjust;
65 	off64_t	tres;
66 	rmutex_t *lk;
67 
68 	FLOCKFILE(lk, iop);
69 	if (iop->_cnt < 0)
70 		iop->_cnt = 0;
71 	if (iop->_flag & _IOREAD)
72 		adjust = (ptrdiff_t)-iop->_cnt;
73 	else if (iop->_flag & (_IOWRT | _IORW)) {
74 		adjust = 0;
75 		if (((iop->_flag & (_IOWRT | _IONBF)) == _IOWRT) &&
76 						(iop->_base != 0))
77 			adjust = iop->_ptr - iop->_base;
78 	} else {
79 		errno = EBADF;	/* file descriptor refers to no open file */
80 		FUNLOCKFILE(lk);
81 		return ((off64_t)EOF);
82 	}
83 	tres = lseek64(FILENO(iop), 0, SEEK_CUR);
84 	if (tres >= 0)
85 		tres += (off64_t)adjust;
86 	FUNLOCKFILE(lk);
87 	return (tres);
88 }
89 
90 #endif	/* _LP64 */
91 
92 off_t
93 ftello(FILE *iop)
94 {
95 	return ((off_t)ftell(iop));
96 }
97