import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / stdio / ftello.c
blob2eafdf1fef70461104caffb05a0e296844effac2
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
33 * Return file offset.
34 * Coordinates with buffering.
37 #include <sys/feature_tests.h>
39 #if !defined(_LP64)
40 #pragma weak _ftello64 = ftello64
41 #endif
42 #pragma weak _ftello = ftello
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"
58 #if !defined(_LP64)
60 off64_t
61 ftello64(FILE *iop)
63 ptrdiff_t adjust;
64 off64_t tres;
65 rmutex_t *lk;
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);
82 tres = lseek64(FILENO(iop), 0, SEEK_CUR);
83 if (tres >= 0)
84 tres += (off64_t)adjust;
85 FUNLOCKFILE(lk);
86 return (tres);
89 #endif /* _LP64 */
91 off_t
92 ftello(FILE *iop)
94 return ((off_t)ftell(iop));