xref: /csrg-svn/lib/libc/stdio/fseek.c (revision 2009)
1 /* @(#)fseek.c	4.1 (Berkeley) 12/21/80 */
2 /*
3  * Seek for standard library.  Coordinates with buffering.
4  */
5 
6 #include	<stdio.h>
7 
8 long lseek();
9 
10 fseek(iop, offset, ptrname)
11 FILE *iop;
12 long offset;
13 {
14 	register resync, c;
15 	long p;
16 
17 	iop->_flag &= ~_IOEOF;
18 	if (iop->_flag&_IOREAD) {
19 		if (ptrname<2 && iop->_base &&
20 			!(iop->_flag&_IONBF)) {
21 			c = iop->_cnt;
22 			p = offset;
23 			if (ptrname==0)
24 				p += c - lseek(fileno(iop),0L,1);
25 			else
26 				offset -= c;
27 			if(c>0&&p<=c&&p>=iop->_base-iop->_ptr){
28 				iop->_ptr += (int)p;
29 				iop->_cnt -= (int)p;
30 				return(0);
31 			}
32 			resync = offset&01;
33 		} else
34 			resync = 0;
35 		p = lseek(fileno(iop), offset-resync, ptrname);
36 		iop->_cnt = 0;
37 		if (resync)
38 			getc(iop);
39 	}
40 	else if (iop->_flag&_IOWRT) {
41 		fflush(iop);
42 		p = lseek(fileno(iop), offset, ptrname);
43 	}
44 	return(p==-1?-1:0);
45 }
46