BSD 3 development
[unix-history] / usr / src / lib / libNS / fseek.c
CommitLineData
1bd0b214
BJ
1/* Copyright (c) 1979 Regents of the University of California */
2#include <stdio.h>
3
4/*
5 * Seek for standard library. Coordinates with buffering.
6 */
7
8long fseek(iop, offset, ptrname)
9register FILE *iop;
10long offset;
11{
12 register n, resync;
13
14 if (iop->_flag&_IODIRT) {
15 fflush(iop);
16 return(lseek(fileno(iop), offset, ptrname));
17 }
18 if (iop->_flag&(_IOREAD|_IOWRT)) {
19 resync = 0;
20 if (ptrname==1) { /* relative */
21 n = iop->_cnt;
22 if (n<0)
23 n = 0;
24 } else {
25 n = offset&01;
26 resync = n;
27 }
28 n = lseek(fileno(iop), offset - n, ptrname);
29 iop->_cnt = 0;
30 iop->_ptr = iop->_base ;
31 iop->_delta = 0;
32 if (resync)
33 getc(iop);
34 return(n);
35 }
36 _error("fseek\n");
37}
38
39/* The current character is always iop->_cnt characters behind the current
40position in the file, except when a file is open and in use for pure writing
41in which case it is _IODIRT(y), and will be correctly positioned by fflush */