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