xref: /titanic_44/usr/src/lib/libbc/libc/stdio/common/fseek.c (revision 2b4a78020b9c38d1b95e2f3fefa6d6e4be382d1f)
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 1986 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 1.3 */
31 
32 /*LINTLIBRARY*/
33 /*
34  * Seek for standard library.  Coordinates with buffering.
35  */
36 #include <stdio.h>
37 
38 extern long lseek();
39 extern int fflush();
40 
41 int
42 fseek(iop, offset, ptrname)
43 register FILE *iop;
44 long	offset;
45 int	ptrname;
46 {
47 	register int resync, c;
48 	long	p = -1;			/* can't happen? */
49 
50 	iop->_flag &= ~_IOEOF;
51 	if(iop->_flag & _IOREAD) {
52 		if(ptrname < 2 && iop->_base && !(iop->_flag&_IONBF)) {
53 			c = iop->_cnt;
54 			p = offset;
55 			if(ptrname == 0) {
56 				long curpos = lseek(fileno(iop), 0L, 1);
57 				if (curpos == -1)
58 					return (-1);
59 				p += c - curpos;
60 				resync = offset&01;
61 			} else {
62 				offset -= (long)c;
63 				resync = 0;
64 			}
65 			if(!(iop->_flag&_IORW) && c > 0 && p <= c &&
66 					p >= iop->_base - iop->_ptr) {
67 				iop->_ptr += (int)p;
68 				iop->_cnt -= (int)p;
69 				return(0);
70 			}
71 		} else
72 			resync = 0;
73 		if(iop->_flag & _IORW) {
74 			iop->_ptr = iop->_base;
75 			iop->_flag &= ~_IOREAD;
76 			resync = 0;
77 		}
78 		p = lseek(fileno(iop), offset-resync, ptrname);
79 		iop->_cnt = 0;
80 		if (resync && p != -1)
81 			if (getc(iop) == EOF)
82 				p = -1;
83 	} else if(iop->_flag & (_IOWRT | _IORW)) {
84 		p = fflush(iop);
85 		iop->_cnt = 0;
86 		if(iop->_flag & _IORW) {
87 			iop->_flag &= ~_IOWRT;
88 			iop->_ptr = iop->_base;
89 		}
90 		return(lseek(fileno(iop), offset, ptrname) == -1 || p == EOF ?
91 		    -1 : 0);
92 	}
93 	return((p == -1)? -1: 0);
94 }
95