xref: /titanic_44/usr/src/lib/libast/common/sfio/sfsk.c (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
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 /*	Seek function that knows discipline
25da2e3ebdSchin **
26da2e3ebdSchin **	Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin #if __STD_C
sfsk(Sfio_t * f,Sfoff_t addr,int type,Sfdisc_t * disc)297c2fbfb3SApril Chin Sfoff_t sfsk(Sfio_t* f, Sfoff_t addr, int type, Sfdisc_t* disc)
30da2e3ebdSchin #else
31da2e3ebdSchin Sfoff_t sfsk(f,addr,type,disc)
327c2fbfb3SApril Chin Sfio_t*		f;
33da2e3ebdSchin Sfoff_t		addr;
347c2fbfb3SApril Chin int		type;
35da2e3ebdSchin Sfdisc_t*	disc;
36da2e3ebdSchin #endif
37da2e3ebdSchin {
38da2e3ebdSchin 	Sfoff_t		p;
39da2e3ebdSchin 	reg Sfdisc_t*	dc;
40da2e3ebdSchin 	reg ssize_t	s;
41da2e3ebdSchin 	reg int		local, mode;
427c2fbfb3SApril Chin 	SFMTXDECL(f);
43da2e3ebdSchin 
447c2fbfb3SApril Chin 	SFMTXENTER(f, (Sfoff_t)(-1));
45da2e3ebdSchin 
46da2e3ebdSchin 	GETLOCAL(f,local);
47da2e3ebdSchin 	if(!local && !(f->bits&SF_DCDOWN))
48da2e3ebdSchin 	{	if((mode = f->mode&SF_RDWR) != (int)f->mode && _sfmode(f,mode,0) < 0)
49da2e3ebdSchin 			SFMTXRETURN(f, (Sfoff_t)(-1));
50da2e3ebdSchin 		if(SFSYNC(f) < 0)
51da2e3ebdSchin 			SFMTXRETURN(f, (Sfoff_t)(-1));
52da2e3ebdSchin #ifdef MAP_TYPE
53da2e3ebdSchin 		if(f->mode == SF_READ && (f->bits&SF_MMAP) && f->data)
54da2e3ebdSchin 		{	SFMUNMAP(f, f->data, f->endb-f->data);
55da2e3ebdSchin 			f->data = NIL(uchar*);
56da2e3ebdSchin 		}
57da2e3ebdSchin #endif
58da2e3ebdSchin 		f->next = f->endb = f->endr = f->endw = f->data;
59da2e3ebdSchin 	}
60da2e3ebdSchin 
61da2e3ebdSchin 	if((type &= (SEEK_SET|SEEK_CUR|SEEK_END)) > SEEK_END)
62da2e3ebdSchin 		SFMTXRETURN(f, (Sfoff_t)(-1));
63da2e3ebdSchin 
64da2e3ebdSchin 	for(;;)
65da2e3ebdSchin 	{	dc = disc;
66da2e3ebdSchin 		if(f->flags&SF_STRING)
67da2e3ebdSchin 		{	SFSTRSIZE(f);
68da2e3ebdSchin 			if(type == SEEK_SET)
69da2e3ebdSchin 				s = (ssize_t)addr;
70da2e3ebdSchin 			else if(type == SEEK_CUR)
71da2e3ebdSchin 				s = (ssize_t)(addr + f->here);
72da2e3ebdSchin 			else	s = (ssize_t)(addr + f->extent);
73da2e3ebdSchin 		}
74da2e3ebdSchin 		else
75da2e3ebdSchin 		{	SFDISC(f,dc,seekf);
76da2e3ebdSchin 			if(dc && dc->seekf)
77da2e3ebdSchin 			{	SFDCSK(f,addr,type,dc,p);
78da2e3ebdSchin 			}
79da2e3ebdSchin 			else
80da2e3ebdSchin 			{	p = syslseekf(f->file,(sfoff_t)addr,type);
81da2e3ebdSchin 			}
82da2e3ebdSchin 			if(p >= 0)
83da2e3ebdSchin 				SFMTXRETURN(f,p);
84da2e3ebdSchin 			s = -1;
85da2e3ebdSchin 		}
86da2e3ebdSchin 
87da2e3ebdSchin 		if(local)
88da2e3ebdSchin 			SETLOCAL(f);
89da2e3ebdSchin 		switch(_sfexcept(f,SF_SEEK,s,dc))
90da2e3ebdSchin 		{
91da2e3ebdSchin 		case SF_EDISC:
92da2e3ebdSchin 		case SF_ECONT:
93da2e3ebdSchin 			if(f->flags&SF_STRING)
94da2e3ebdSchin 				SFMTXRETURN(f, (Sfoff_t)s);
95da2e3ebdSchin 			goto do_continue;
96da2e3ebdSchin 		default:
97da2e3ebdSchin 			SFMTXRETURN(f, (Sfoff_t)(-1));
98da2e3ebdSchin 		}
99da2e3ebdSchin 
100da2e3ebdSchin 	do_continue:
101da2e3ebdSchin 		for(dc = f->disc; dc; dc = dc->disc)
102da2e3ebdSchin 			if(dc == disc)
103da2e3ebdSchin 				break;
104da2e3ebdSchin 		disc = dc;
105da2e3ebdSchin 	}
106da2e3ebdSchin }
107