1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Set the IO pointer to a specific location in the stream
25da2e3ebdSchin **
26da2e3ebdSchin ** Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin
29da2e3ebdSchin #if __STD_C
newpos(Sfio_t * f,Sfoff_t p)30da2e3ebdSchin static void newpos(Sfio_t* f, Sfoff_t p)
31da2e3ebdSchin #else
32da2e3ebdSchin static void newpos(f, p)
33da2e3ebdSchin Sfio_t* f;
34da2e3ebdSchin Sfoff_t p;
35da2e3ebdSchin #endif
36da2e3ebdSchin {
37da2e3ebdSchin #ifdef MAP_TYPE
38da2e3ebdSchin if((f->bits&SF_MMAP) && f->data)
39da2e3ebdSchin { SFMUNMAP(f, f->data, f->endb-f->data);
40da2e3ebdSchin f->data = NIL(uchar*);
41da2e3ebdSchin }
42da2e3ebdSchin #endif
43da2e3ebdSchin f->next = f->endr = f->endw = f->data;
44da2e3ebdSchin f->endb = (f->mode&SF_WRITE) ? f->data+f->size : f->data;
45da2e3ebdSchin if((f->here = p) < 0)
46da2e3ebdSchin { f->extent = -1;
47da2e3ebdSchin f->here = 0;
48da2e3ebdSchin }
49da2e3ebdSchin }
50da2e3ebdSchin
51da2e3ebdSchin #if __STD_C
sfseek(Sfio_t * f,Sfoff_t p,int type)52da2e3ebdSchin Sfoff_t sfseek(Sfio_t* f, Sfoff_t p, int type)
53da2e3ebdSchin #else
54da2e3ebdSchin Sfoff_t sfseek(f,p,type)
55da2e3ebdSchin Sfio_t* f; /* seek to a new location in this stream */
56da2e3ebdSchin Sfoff_t p; /* place to seek to */
57da2e3ebdSchin int type; /* 0: from org, 1: from here, 2: from end */
58da2e3ebdSchin #endif
59da2e3ebdSchin {
60da2e3ebdSchin Sfoff_t r, s;
61da2e3ebdSchin int mode, local, hardseek, mustsync;
627c2fbfb3SApril Chin SFMTXDECL(f);
63da2e3ebdSchin
647c2fbfb3SApril Chin SFMTXENTER(f, (Sfoff_t)(-1));
65da2e3ebdSchin
66da2e3ebdSchin GETLOCAL(f,local);
67da2e3ebdSchin
68da2e3ebdSchin hardseek = (type|f->flags)&(SF_SHARE|SF_PUBLIC);
69da2e3ebdSchin
70da2e3ebdSchin if(hardseek && f->mode == (SF_READ|SF_SYNCED) )
71da2e3ebdSchin { newpos(f,f->here);
72da2e3ebdSchin f->mode = SF_READ;
73da2e3ebdSchin }
74da2e3ebdSchin
75da2e3ebdSchin /* set and initialize the stream to a definite mode */
76da2e3ebdSchin if((int)SFMODE(f,local) != (mode = f->mode&SF_RDWR))
77da2e3ebdSchin { int flags = f->flags;
78da2e3ebdSchin
79da2e3ebdSchin if(hardseek&SF_PUBLIC) /* seek ptr must follow file descriptor */
80da2e3ebdSchin f->flags |= SF_SHARE|SF_PUBLIC;
81da2e3ebdSchin mode = _sfmode(f,mode,local);
82da2e3ebdSchin if(hardseek&SF_PUBLIC)
83da2e3ebdSchin f->flags = flags;
84da2e3ebdSchin
85da2e3ebdSchin if(mode < 0)
86da2e3ebdSchin SFMTXRETURN(f, (Sfoff_t)(-1));
87da2e3ebdSchin }
88da2e3ebdSchin
89da2e3ebdSchin mustsync = (type&SF_SHARE) && !(type&SF_PUBLIC) &&
90da2e3ebdSchin (f->mode&SF_READ) && !(f->flags&SF_STRING);
91da2e3ebdSchin
92da2e3ebdSchin /* Xopen-compliant */
93da2e3ebdSchin if((type &= (SEEK_SET|SEEK_CUR|SEEK_END)) != SEEK_SET &&
94da2e3ebdSchin type != SEEK_CUR && type != SEEK_END )
95da2e3ebdSchin { errno = EINVAL;
96da2e3ebdSchin SFMTXRETURN(f, (Sfoff_t)(-1));
97da2e3ebdSchin }
98da2e3ebdSchin
99da2e3ebdSchin if(f->extent < 0)
100da2e3ebdSchin { /* let system call set errno */
101da2e3ebdSchin (void)SFSK(f,(Sfoff_t)0,SEEK_CUR,f->disc);
102da2e3ebdSchin SFMTXRETURN(f, (Sfoff_t)(-1));
103da2e3ebdSchin }
104da2e3ebdSchin
105da2e3ebdSchin /* throw away ungetc data */
106da2e3ebdSchin if(f->disc == _Sfudisc)
107da2e3ebdSchin (void)sfclose((*_Sfstack)(f,NIL(Sfio_t*)));
108da2e3ebdSchin
109da2e3ebdSchin /* lock the stream for internal manipulations */
110da2e3ebdSchin SFLOCK(f,local);
111da2e3ebdSchin
112da2e3ebdSchin /* clear error and eof bits */
113da2e3ebdSchin f->flags &= ~(SF_EOF|SF_ERROR);
114da2e3ebdSchin
115da2e3ebdSchin while(f->flags&SF_STRING)
116da2e3ebdSchin { SFSTRSIZE(f);
117da2e3ebdSchin
118da2e3ebdSchin if(type == SEEK_CUR)
119da2e3ebdSchin r = p + (f->next - f->data);
120da2e3ebdSchin else if(type == SEEK_END)
121da2e3ebdSchin r = p + f->extent;
122da2e3ebdSchin else r = p;
123da2e3ebdSchin
124da2e3ebdSchin if(r >= 0 && r <= f->size)
125da2e3ebdSchin { p = r;
126da2e3ebdSchin f->next = f->data+p;
127da2e3ebdSchin f->here = p;
128da2e3ebdSchin if(p > f->extent)
129da2e3ebdSchin memclear((char*)(f->data+f->extent),(int)(p-f->extent));
130da2e3ebdSchin goto done;
131da2e3ebdSchin }
132da2e3ebdSchin
133da2e3ebdSchin /* check exception handler, note that this may pop stream */
134da2e3ebdSchin if(SFSK(f,r,SEEK_SET,f->disc) != r)
135da2e3ebdSchin { p = -1;
136da2e3ebdSchin goto done;
137da2e3ebdSchin }
138da2e3ebdSchin else if(!(f->flags&SF_STRING))
139da2e3ebdSchin { p = r;
140da2e3ebdSchin goto done;
141da2e3ebdSchin }
142da2e3ebdSchin }
143da2e3ebdSchin
144da2e3ebdSchin if(f->mode&SF_WRITE)
145da2e3ebdSchin { /* see if we can avoid flushing buffer */
146da2e3ebdSchin if(!hardseek && type < SEEK_END && !(f->flags&SF_APPENDWR) )
147da2e3ebdSchin { s = f->here + (f->next - f->data);
148da2e3ebdSchin r = p + (type == SEEK_SET ? 0 : s);
149da2e3ebdSchin if(r == s)
150da2e3ebdSchin { p = r;
151da2e3ebdSchin goto done;
152da2e3ebdSchin }
153da2e3ebdSchin }
154da2e3ebdSchin
155da2e3ebdSchin if(f->next > f->data && SFSYNC(f) < 0)
156da2e3ebdSchin { p = -1;
157da2e3ebdSchin goto done;
158da2e3ebdSchin }
159da2e3ebdSchin }
160da2e3ebdSchin
161da2e3ebdSchin if(type == SEEK_END || (f->mode&SF_WRITE) )
162da2e3ebdSchin { if((hardseek&SF_PUBLIC) || type == SEEK_END)
163da2e3ebdSchin p = SFSK(f, p, type, f->disc);
164da2e3ebdSchin else
165da2e3ebdSchin { r = p + (type == SEEK_CUR ? f->here : 0);
166da2e3ebdSchin p = (hardseek || r != f->here) ? SFSK(f,r,SEEK_SET,f->disc) : r;
167da2e3ebdSchin }
168da2e3ebdSchin if(p >= 0)
169da2e3ebdSchin newpos(f,p);
170da2e3ebdSchin
171da2e3ebdSchin goto done;
172da2e3ebdSchin }
173da2e3ebdSchin
174da2e3ebdSchin /* if get here, must be a read stream */
175da2e3ebdSchin s = f->here - (f->endb - f->next);
176da2e3ebdSchin r = p + (type == SEEK_CUR ? s : 0);
177da2e3ebdSchin if(r <= f->here && r >= (f->here - (f->endb-f->data)) )
178da2e3ebdSchin { if((hardseek || (type == SEEK_CUR && p == 0)) )
179da2e3ebdSchin { if((s = SFSK(f, (Sfoff_t)0, SEEK_CUR, f->disc)) == f->here ||
180da2e3ebdSchin (s >= 0 && !(hardseek&SF_PUBLIC) &&
181da2e3ebdSchin (s = SFSK(f, f->here, SEEK_SET, f->disc)) == f->here) )
182da2e3ebdSchin goto near_done;
183da2e3ebdSchin else if(s < 0)
184da2e3ebdSchin { p = -1;
185da2e3ebdSchin goto done;
186da2e3ebdSchin }
187da2e3ebdSchin else
188da2e3ebdSchin { newpos(f,s);
189da2e3ebdSchin hardseek = 0;
190da2e3ebdSchin }
191da2e3ebdSchin }
192da2e3ebdSchin else
193da2e3ebdSchin { near_done:
194da2e3ebdSchin f->next = f->endb - (f->here - r);
195da2e3ebdSchin p = r;
196da2e3ebdSchin goto done;
197da2e3ebdSchin }
198da2e3ebdSchin }
199da2e3ebdSchin
200da2e3ebdSchin /* desired position */
201da2e3ebdSchin if((p += type == SEEK_CUR ? s : 0) < 0)
202da2e3ebdSchin goto done;
203da2e3ebdSchin
204da2e3ebdSchin #ifdef MAP_TYPE
205da2e3ebdSchin if(f->bits&SF_MMAP)
206da2e3ebdSchin { /* if mmap is not great, stop mmaping if moving around too much */
207da2e3ebdSchin #if _mmap_worthy < 2
208da2e3ebdSchin if((f->next - f->data) < ((f->endb - f->data)/4) )
209da2e3ebdSchin { SFSETBUF(f,(Void_t*)f->tiny,(size_t)SF_UNBOUND);
210da2e3ebdSchin hardseek = 1; /* this forces a hard seek below */
211da2e3ebdSchin }
212da2e3ebdSchin else
213da2e3ebdSchin #endif
214da2e3ebdSchin { /* for mmap, f->here can be virtual except for hardseek */
215da2e3ebdSchin newpos(f,p);
216da2e3ebdSchin if(!hardseek)
217da2e3ebdSchin goto done;
218da2e3ebdSchin }
219da2e3ebdSchin }
220da2e3ebdSchin #endif
221da2e3ebdSchin
222da2e3ebdSchin if(f->endb > f->next)
223da2e3ebdSchin { /* reduce wastage in future buffer fillings */
224da2e3ebdSchin f->iosz = (f->next - f->data) + (f->endb - f->next)/2;
225da2e3ebdSchin f->iosz = ((f->iosz + f->blksz-1)/f->blksz)*f->blksz;
226da2e3ebdSchin }
227da2e3ebdSchin if(f->iosz >= f->size)
228da2e3ebdSchin f->iosz = 0;
229da2e3ebdSchin
230da2e3ebdSchin /* buffer is now considered empty */
231da2e3ebdSchin f->next = f->endr = f->endb = f->data;
232da2e3ebdSchin
233da2e3ebdSchin /* small backseeks often come in bunches, so seek back as far as possible */
234da2e3ebdSchin if(p < f->lpos && f->size > f->blksz && (p + f->blksz) > s)
235da2e3ebdSchin { if((r = s - f->size) < 0)
236da2e3ebdSchin r = 0;
237da2e3ebdSchin }
238da2e3ebdSchin /* try to align buffer to block boundary to enhance I/O speed */
239da2e3ebdSchin else if(f->blksz > 0 && f->size >= 2*f->blksz)
240da2e3ebdSchin r = p - (p%f->blksz);
241da2e3ebdSchin else
242da2e3ebdSchin { r = p;
243da2e3ebdSchin
244da2e3ebdSchin /* seeking around and wasting data, be conservative */
245da2e3ebdSchin if(f->iosz > 0 && (p > f->lpos || p < f->lpos-f->size) )
246da2e3ebdSchin f->bits |= SF_JUSTSEEK;
247da2e3ebdSchin }
248da2e3ebdSchin
249da2e3ebdSchin if((hardseek || r != f->here) && (f->here = SFSK(f,r,SEEK_SET,f->disc)) != r)
250da2e3ebdSchin { if(r < p) /* now try to just get to p */
251da2e3ebdSchin f->here = SFSK(f,p,SEEK_SET,f->disc);
252da2e3ebdSchin if(f->here != p)
253da2e3ebdSchin p = -1;
254da2e3ebdSchin goto done;
255da2e3ebdSchin }
256da2e3ebdSchin
257da2e3ebdSchin if(r < p) /* read to cover p */
258da2e3ebdSchin { (void)SFRD(f, f->data, f->size, f->disc);
259da2e3ebdSchin if(p <= f->here && p >= (f->here - (f->endb - f->data)) )
260da2e3ebdSchin f->next = f->endb - (size_t)(f->here-p);
261da2e3ebdSchin else /* recover from read failure by just seeking to p */
262da2e3ebdSchin { f->next = f->endb = f->data;
263da2e3ebdSchin if((f->here = SFSK(f,p,SEEK_SET,f->disc)) != p)
264da2e3ebdSchin p = -1;
265da2e3ebdSchin }
266da2e3ebdSchin }
267da2e3ebdSchin
268da2e3ebdSchin done :
269da2e3ebdSchin if(f->here < 0) /* hasn't been the best of time */
270da2e3ebdSchin { f->extent = -1;
271da2e3ebdSchin f->here = 0;
272da2e3ebdSchin }
273da2e3ebdSchin
274da2e3ebdSchin f->lpos = p;
275da2e3ebdSchin
276da2e3ebdSchin SFOPEN(f,local);
277da2e3ebdSchin
278da2e3ebdSchin if(mustsync)
279da2e3ebdSchin sfsync(f);
280da2e3ebdSchin SFMTXRETURN(f, p);
281da2e3ebdSchin }
282