xref: /titanic_51/usr/src/lib/libast/common/sfio/sfread.c (revision d42c7aec1963a7ded6694ac33a5bd96422fc8ca7)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
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(Sfio_t* f, Void_t* buf, size_t n)
31 #else
32 ssize_t sfread(f,buf,n)
33 Sfio_t*		f;	/* read from this stream. 	*/
34 Void_t*		buf;	/* buffer to read into		*/
35 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 	SFMTXDECL(f);
42 
43 	SFMTXENTER(f, (ssize_t)(-1));
44 
45 	GETLOCAL(f,local);
46 	justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;
47 
48 	if(!buf)
49 		SFMTXRETURN(f, (ssize_t)(n == 0 ? 0 : -1) );
50 
51 	/* release peek lock */
52 	if(f->mode&SF_PEEK)
53 	{	if(!(f->mode&SF_READ) )
54 			SFMTXRETURN(f, (ssize_t)(-1));
55 
56 		if(f->mode&SF_GETR)
57 		{	if(((uchar*)buf + f->val) != f->next &&
58 			   (!f->rsrv || f->rsrv->data != (uchar*)buf) )
59 				SFMTXRETURN(f, (ssize_t)(-1));
60 			f->mode &= ~SF_PEEK;
61 			SFMTXRETURN(f, 0);
62 		}
63 		else
64 		{	if((uchar*)buf != f->next)
65 				SFMTXRETURN(f, (ssize_t)(-1));
66 			f->mode &= ~SF_PEEK;
67 			if(f->mode&SF_PKRD)
68 			{	/* actually read the data now */
69 				f->mode &= ~SF_PKRD;
70 				if(n > 0)
71 					n = (r = sysreadf(f->file,f->data,n)) < 0 ? 0 : r;
72 				f->endb = f->data+n;
73 				f->here += n;
74 			}
75 			f->next += n;
76 			f->endr = f->endb;
77 			SFMTXRETURN(f, n);
78 		}
79 	}
80 
81 	s = begs = (uchar*)buf;
82 	for(;; f->mode &= ~SF_LOCK)
83 	{	/* check stream mode */
84 		if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)
85 		{	n = s > begs ? s-begs : (size_t)(-1);
86 			SFMTXRETURN(f, (ssize_t)n);
87 		}
88 
89 		SFLOCK(f,local);
90 
91 		if((r = f->endb - f->next) > 0) /* has buffered data */
92 		{	if(r > (ssize_t)n)
93 				r = (ssize_t)n;
94 			if(s != f->next)
95 				memcpy(s, f->next, r);
96 			f->next += r;
97 			s += r;
98 			n -= r;
99 		}
100 
101 		if(n <= 0)	/* all done */
102 			break;
103 
104 		if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )
105 		{	f->next = f->endb = f->data;
106 
107 			/* exact IO is desirable for these cases */
108 			if(SFDIRECT(f,n) ||
109 			   ((f->flags&SF_SHARE) && f->extent < 0) )
110 				r = (ssize_t)n;
111 			else if(justseek && n <= f->iosz && f->iosz <= f->size)
112 				r = f->iosz;	/* limit buffering */
113 			else	r = f->size;	/* full buffering */
114 
115 			/* if read almost full size, then just do it direct */
116 			if(r > (ssize_t)n && (r - r/8) <= (ssize_t)n)
117 				r = (ssize_t)n;
118 
119 			/* read directly to user's buffer */
120 			if(r == (ssize_t)n && (r = SFRD(f,s,r,f->disc)) >= 0)
121 			{	s += r;
122 				n -= r;
123 				if(r == 0 || n == 0) /* eof or eob */
124 					break;
125 			}
126 			else	goto do_filbuf;
127 		}
128 		else
129 		{ do_filbuf:
130 			if(justseek)
131 				f->bits |= SF_JUSTSEEK;
132 			if(SFFILBUF(f,-1) <= 0)
133 				break;
134 		}
135 	}
136 
137 	SFOPEN(f,local);
138 	r = s-begs;
139 	SFMTXRETURN(f, r);
140 }
141