xref: /titanic_44/usr/src/lib/libast/common/sfio/sfread.c (revision 0bb073995ac5a95bd35f2dd790df1ea3d8c2d507)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                 Glenn Fowler <gsf@research.att.com>                  *
18 *                  David Korn <dgk@research.att.com>                   *
19 *                   Phong Vo <kpv@research.att.com>                    *
20 *                                                                      *
21 ***********************************************************************/
22 #include	"sfhdr.h"
23 
24 /*	Read n bytes from a stream into a buffer
25 **
26 **	Written by Kiem-Phong Vo.
27 */
28 
29 #if __STD_C
30 ssize_t sfread(reg Sfio_t* f, Void_t* buf, reg size_t n)
31 #else
32 ssize_t sfread(f,buf,n)
33 reg Sfio_t*	f;	/* read from this stream. 	*/
34 Void_t*		buf;	/* buffer to read into		*/
35 reg size_t	n;	/* number of bytes to be read. 	*/
36 #endif
37 {
38 	reg uchar	*s, *begs;
39 	reg ssize_t	r;
40 	reg int		local, justseek;
41 
42 	SFMTXSTART(f, (ssize_t)(-1));
43 
44 	GETLOCAL(f,local);
45 	justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;
46 
47 	if(!buf)
48 		SFMTXRETURN(f, (ssize_t)(n == 0 ? 0 : -1) );
49 
50 	/* release peek lock */
51 	if(f->mode&SF_PEEK)
52 	{	if(!(f->mode&SF_READ) )
53 			SFMTXRETURN(f, (ssize_t)(-1));
54 
55 		if(f->mode&SF_GETR)
56 		{	if(((uchar*)buf + f->val) != f->next &&
57 			   (!f->rsrv || f->rsrv->data != (uchar*)buf) )
58 				SFMTXRETURN(f, (ssize_t)(-1));
59 			f->mode &= ~SF_PEEK;
60 			SFMTXRETURN(f, 0);
61 		}
62 		else
63 		{	if((uchar*)buf != f->next)
64 				SFMTXRETURN(f, (ssize_t)(-1));
65 			f->mode &= ~SF_PEEK;
66 			if(f->mode&SF_PKRD)
67 			{	/* actually read the data now */
68 				f->mode &= ~SF_PKRD;
69 				if(n > 0)
70 					n = (r = sysreadf(f->file,f->data,n)) < 0 ? 0 : r;
71 				f->endb = f->data+n;
72 				f->here += n;
73 			}
74 			f->next += n;
75 			f->endr = f->endb;
76 			SFMTXRETURN(f, n);
77 		}
78 	}
79 
80 	s = begs = (uchar*)buf;
81 	for(;; f->mode &= ~SF_LOCK)
82 	{	/* check stream mode */
83 		if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)
84 		{	n = s > begs ? s-begs : (size_t)(-1);
85 			SFMTXRETURN(f, (ssize_t)n);
86 		}
87 
88 		SFLOCK(f,local);
89 
90 		if((r = f->endb - f->next) > 0) /* has buffered data */
91 		{	if(r > (ssize_t)n)
92 				r = (ssize_t)n;
93 			if(s != f->next)
94 				memcpy(s, f->next, r);
95 			f->next += r;
96 			s += r;
97 			n -= r;
98 		}
99 
100 		if(n <= 0)	/* all done */
101 			break;
102 
103 		if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )
104 		{	f->next = f->endb = f->data;
105 
106 			/* exact IO is desirable for these cases */
107 			if(SFDIRECT(f,n) ||
108 			   ((f->flags&SF_SHARE) && f->extent < 0) )
109 				r = (ssize_t)n;
110 			else if(justseek && n <= f->iosz && f->iosz <= f->size)
111 				r = f->iosz;	/* limit buffering */
112 			else	r = f->size;	/* full buffering */
113 
114 			/* if read almost full size, then just do it direct */
115 			if(r > (ssize_t)n && (r - r/8) <= (ssize_t)n)
116 				r = (ssize_t)n;
117 
118 			/* read directly to user's buffer */
119 			if(r == (ssize_t)n && (r = SFRD(f,s,r,f->disc)) >= 0)
120 			{	s += r;
121 				n -= r;
122 				if(r == 0 || n == 0) /* eof or eob */
123 					break;
124 			}
125 			else	goto do_filbuf;
126 		}
127 		else
128 		{ do_filbuf:
129 			if(justseek)
130 				f->bits |= SF_JUSTSEEK;
131 			if(SFFILBUF(f,-1) <= 0)
132 				break;
133 		}
134 	}
135 
136 	SFOPEN(f,local);
137 	r = s-begs;
138 	SFMTXRETURN(f, r);
139 }
140