xref: /illumos-gate/usr/src/lib/libc/port/stdio/fseeko.c (revision 9b664393d4fdda96221e6ea9ea95790d3c15be70)
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 /*
31  * Seek for standard library.  Coordinates with buffering.
32  */
33 
34 #include <sys/feature_tests.h>
35 
36 #if !defined(_LP64)
37 #pragma weak _fseeko64 = fseeko64
38 #endif
39 #pragma weak _fseeko = fseeko
40 
41 #include "lint.h"
42 #include "file64.h"
43 #include "mtlib.h"
44 #include <sys/types.h>
45 #include <unistd.h>
46 #include <limits.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <thread.h>
50 #include <synch.h>
51 #include "stdiom.h"
52 
53 #if !defined(_LP64)
54 int
55 fseeko64(FILE *iop, off64_t offset, int ptrname)
56 {
57 	off64_t	p;
58 	rmutex_t *lk;
59 
60 	FLOCKFILE(lk, iop);
61 	iop->_flag &= ~_IOEOF;
62 
63 	if (!(iop->_flag & _IOREAD) && !(iop->_flag & (_IOWRT | _IORW))) {
64 		errno = EBADF;
65 		FUNLOCKFILE(lk);
66 		return (-1);
67 	}
68 
69 	if (iop->_flag & _IOREAD) {
70 		if (ptrname == 1 && iop->_base && !(iop->_flag&_IONBF)) {
71 			offset -= iop->_cnt;
72 		}
73 	} else if (iop->_flag & (_IOWRT | _IORW)) {
74 		if (_fflush_u(iop) == EOF) {
75 			FUNLOCKFILE(lk);
76 			return (-1);
77 		}
78 	}
79 	iop->_cnt = 0;
80 	iop->_ptr = iop->_base;
81 	if (iop->_flag & _IORW) {
82 		iop->_flag &= ~(_IOREAD | _IOWRT);
83 	}
84 	p = _xseek64(iop, offset, ptrname);
85 	FUNLOCKFILE(lk);
86 	return ((p == -1)? -1: 0);
87 }
88 #endif	/* _LP64 */
89 
90 int
91 fseeko(FILE *iop, off_t offset, int ptrname)
92 {
93 	return (fseek(iop, offset, ptrname));
94 }
95