install C version of _doprnt
[unix-history] / usr / src / lib / libc / stdio / fseek.c
CommitLineData
2ce81398
DS
1#if defined(LIBC_SCCS) && !defined(lint)
2static char sccsid[] = "@(#)fseek.c 5.3 (Berkeley) %G%";
3#endif LIBC_SCCS and not lint
b8f253e8 4
a9e59a21
BJ
5/*
6 * Seek for standard library. Coordinates with buffering.
7 */
8
9#include <stdio.h>
10
11long lseek();
12
13fseek(iop, offset, ptrname)
4530e41c
S
14 register FILE *iop;
15 long offset;
a9e59a21
BJ
16{
17 register resync, c;
058013b1 18 long p = -1; /* can't happen? */
a9e59a21
BJ
19
20 iop->_flag &= ~_IOEOF;
21 if (iop->_flag&_IOREAD) {
22 if (ptrname<2 && iop->_base &&
23 !(iop->_flag&_IONBF)) {
24 c = iop->_cnt;
25 p = offset;
4530e41c 26 if (ptrname==0) {
058013b1 27 long curpos = lseek(fileno(iop), 0L, 1);
4530e41c
S
28 if (curpos == -1)
29 return (-1);
30 p += c - curpos;
31 } else
a9e59a21 32 offset -= c;
d8af6b8b
MT
33 if(!(iop->_flag&_IORW) && c>0&&p<=c
34 && p>=iop->_base-iop->_ptr){
a9e59a21
BJ
35 iop->_ptr += (int)p;
36 iop->_cnt -= (int)p;
058013b1 37 return(0);
a9e59a21
BJ
38 }
39 resync = offset&01;
40 } else
41 resync = 0;
d8af6b8b
MT
42 if (iop->_flag & _IORW) {
43 iop->_ptr = iop->_base;
44 iop->_flag &= ~_IOREAD;
82e40ae3 45 resync = 0;
d8af6b8b 46 }
a9e59a21
BJ
47 p = lseek(fileno(iop), offset-resync, ptrname);
48 iop->_cnt = 0;
058013b1
JL
49 if (resync && p != -1)
50 if (getc(iop) == EOF)
51 p = -1;
a9e59a21 52 }
d8af6b8b 53 else if (iop->_flag & (_IOWRT|_IORW)) {
058013b1 54 p = fflush(iop);
d8af6b8b
MT
55 if (iop->_flag & _IORW) {
56 iop->_cnt = 0;
57 iop->_flag &= ~_IOWRT;
58 iop->_ptr = iop->_base;
59 }
058013b1
JL
60 return(lseek(fileno(iop), offset, ptrname) == -1 || p == EOF ?
61 -1 : 0);
a9e59a21 62 }
058013b1 63 return(p==-1?-1:0);
a9e59a21 64}