xref: /titanic_44/usr/src/lib/libast/common/sfio/sfread.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 /*	Read n bytes from a stream into a buffer
25da2e3ebdSchin **
26da2e3ebdSchin **	Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin 
29da2e3ebdSchin #if __STD_C
sfread(Sfio_t * f,Void_t * buf,size_t n)307c2fbfb3SApril Chin ssize_t sfread(Sfio_t* f, Void_t* buf, size_t n)
31da2e3ebdSchin #else
32da2e3ebdSchin ssize_t sfread(f,buf,n)
337c2fbfb3SApril Chin Sfio_t*		f;	/* read from this stream. 	*/
34da2e3ebdSchin Void_t*		buf;	/* buffer to read into		*/
357c2fbfb3SApril Chin size_t		n;	/* number of bytes to be read. 	*/
36da2e3ebdSchin #endif
37da2e3ebdSchin {
38da2e3ebdSchin 	reg uchar	*s, *begs;
39da2e3ebdSchin 	reg ssize_t	r;
40da2e3ebdSchin 	reg int		local, justseek;
417c2fbfb3SApril Chin 	SFMTXDECL(f);
42da2e3ebdSchin 
437c2fbfb3SApril Chin 	SFMTXENTER(f, (ssize_t)(-1));
44da2e3ebdSchin 
45da2e3ebdSchin 	GETLOCAL(f,local);
46da2e3ebdSchin 	justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;
47da2e3ebdSchin 
48da2e3ebdSchin 	if(!buf)
49da2e3ebdSchin 		SFMTXRETURN(f, (ssize_t)(n == 0 ? 0 : -1) );
50da2e3ebdSchin 
51da2e3ebdSchin 	/* release peek lock */
52da2e3ebdSchin 	if(f->mode&SF_PEEK)
53da2e3ebdSchin 	{	if(!(f->mode&SF_READ) )
54da2e3ebdSchin 			SFMTXRETURN(f, (ssize_t)(-1));
55da2e3ebdSchin 
56da2e3ebdSchin 		if(f->mode&SF_GETR)
57da2e3ebdSchin 		{	if(((uchar*)buf + f->val) != f->next &&
58da2e3ebdSchin 			   (!f->rsrv || f->rsrv->data != (uchar*)buf) )
59da2e3ebdSchin 				SFMTXRETURN(f, (ssize_t)(-1));
60da2e3ebdSchin 			f->mode &= ~SF_PEEK;
61da2e3ebdSchin 			SFMTXRETURN(f, 0);
62da2e3ebdSchin 		}
63da2e3ebdSchin 		else
64da2e3ebdSchin 		{	if((uchar*)buf != f->next)
65da2e3ebdSchin 				SFMTXRETURN(f, (ssize_t)(-1));
66da2e3ebdSchin 			f->mode &= ~SF_PEEK;
67da2e3ebdSchin 			if(f->mode&SF_PKRD)
68da2e3ebdSchin 			{	/* actually read the data now */
69da2e3ebdSchin 				f->mode &= ~SF_PKRD;
70da2e3ebdSchin 				if(n > 0)
71da2e3ebdSchin 					n = (r = sysreadf(f->file,f->data,n)) < 0 ? 0 : r;
72da2e3ebdSchin 				f->endb = f->data+n;
73da2e3ebdSchin 				f->here += n;
74da2e3ebdSchin 			}
75da2e3ebdSchin 			f->next += n;
76da2e3ebdSchin 			f->endr = f->endb;
77da2e3ebdSchin 			SFMTXRETURN(f, n);
78da2e3ebdSchin 		}
79da2e3ebdSchin 	}
80da2e3ebdSchin 
81da2e3ebdSchin 	s = begs = (uchar*)buf;
82da2e3ebdSchin 	for(;; f->mode &= ~SF_LOCK)
83da2e3ebdSchin 	{	/* check stream mode */
84da2e3ebdSchin 		if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)
85da2e3ebdSchin 		{	n = s > begs ? s-begs : (size_t)(-1);
86da2e3ebdSchin 			SFMTXRETURN(f, (ssize_t)n);
87da2e3ebdSchin 		}
88da2e3ebdSchin 
89da2e3ebdSchin 		SFLOCK(f,local);
90da2e3ebdSchin 
91da2e3ebdSchin 		if((r = f->endb - f->next) > 0) /* has buffered data */
92da2e3ebdSchin 		{	if(r > (ssize_t)n)
93da2e3ebdSchin 				r = (ssize_t)n;
94da2e3ebdSchin 			if(s != f->next)
95da2e3ebdSchin 				memcpy(s, f->next, r);
96da2e3ebdSchin 			f->next += r;
97da2e3ebdSchin 			s += r;
98da2e3ebdSchin 			n -= r;
99da2e3ebdSchin 		}
100da2e3ebdSchin 
101da2e3ebdSchin 		if(n <= 0)	/* all done */
102da2e3ebdSchin 			break;
103da2e3ebdSchin 
104da2e3ebdSchin 		if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )
105da2e3ebdSchin 		{	f->next = f->endb = f->data;
106da2e3ebdSchin 
107da2e3ebdSchin 			/* exact IO is desirable for these cases */
108da2e3ebdSchin 			if(SFDIRECT(f,n) ||
109da2e3ebdSchin 			   ((f->flags&SF_SHARE) && f->extent < 0) )
110da2e3ebdSchin 				r = (ssize_t)n;
111da2e3ebdSchin 			else if(justseek && n <= f->iosz && f->iosz <= f->size)
112da2e3ebdSchin 				r = f->iosz;	/* limit buffering */
113da2e3ebdSchin 			else	r = f->size;	/* full buffering */
114da2e3ebdSchin 
115da2e3ebdSchin 			/* if read almost full size, then just do it direct */
116da2e3ebdSchin 			if(r > (ssize_t)n && (r - r/8) <= (ssize_t)n)
117da2e3ebdSchin 				r = (ssize_t)n;
118da2e3ebdSchin 
119da2e3ebdSchin 			/* read directly to user's buffer */
120da2e3ebdSchin 			if(r == (ssize_t)n && (r = SFRD(f,s,r,f->disc)) >= 0)
121da2e3ebdSchin 			{	s += r;
122da2e3ebdSchin 				n -= r;
123da2e3ebdSchin 				if(r == 0 || n == 0) /* eof or eob */
124da2e3ebdSchin 					break;
125da2e3ebdSchin 			}
126da2e3ebdSchin 			else	goto do_filbuf;
127da2e3ebdSchin 		}
128da2e3ebdSchin 		else
129da2e3ebdSchin 		{ do_filbuf:
130da2e3ebdSchin 			if(justseek)
131da2e3ebdSchin 				f->bits |= SF_JUSTSEEK;
132da2e3ebdSchin 			if(SFFILBUF(f,-1) <= 0)
133da2e3ebdSchin 				break;
134da2e3ebdSchin 		}
135da2e3ebdSchin 	}
136da2e3ebdSchin 
137da2e3ebdSchin 	SFOPEN(f,local);
138da2e3ebdSchin 	r = s-begs;
139da2e3ebdSchin 	SFMTXRETURN(f, r);
140da2e3ebdSchin }
141