xref: /titanic_51/usr/src/lib/libast/common/sfio/sffilbuf.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 /*	Fill the buffer of a stream with data.
25da2e3ebdSchin **	If n < 0, sffilbuf() attempts to fill the buffer if it's empty.
26da2e3ebdSchin **	If n == 0, if the buffer is not empty, just return the first byte;
27da2e3ebdSchin **		otherwise fill the buffer and return the first byte.
28da2e3ebdSchin **	If n > 0, even if the buffer is not empty, try a read to get as
29da2e3ebdSchin **		close to n as possible. n is reset to -1 if stack pops.
30da2e3ebdSchin **
31da2e3ebdSchin **	Written by Kiem-Phong Vo
32da2e3ebdSchin */
33da2e3ebdSchin 
34da2e3ebdSchin #if __STD_C
_sffilbuf(Sfio_t * f,reg int n)35da2e3ebdSchin int _sffilbuf(Sfio_t* f, reg int n)
36da2e3ebdSchin #else
37da2e3ebdSchin int _sffilbuf(f,n)
38da2e3ebdSchin Sfio_t*	f;	/* fill the read buffer of this stream */
39da2e3ebdSchin reg int	n;	/* see above */
40da2e3ebdSchin #endif
41da2e3ebdSchin {
42da2e3ebdSchin 	reg ssize_t	r;
43da2e3ebdSchin 	reg int		first, local, rcrv, rc, justseek;
447c2fbfb3SApril Chin 	SFMTXDECL(f);
45da2e3ebdSchin 
467c2fbfb3SApril Chin 	SFMTXENTER(f,-1);
47da2e3ebdSchin 
48da2e3ebdSchin 	GETLOCAL(f,local);
49da2e3ebdSchin 
50da2e3ebdSchin 	/* any peek data must be preserved across stacked streams */
51da2e3ebdSchin 	rcrv = f->mode&(SF_RC|SF_RV|SF_LOCK);
52da2e3ebdSchin 	rc = f->getr;
53da2e3ebdSchin 
54da2e3ebdSchin 	justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;
55da2e3ebdSchin 
56da2e3ebdSchin 	for(first = 1;; first = 0, (f->mode &= ~SF_LOCK) )
57da2e3ebdSchin 	{	/* check mode */
58da2e3ebdSchin 		if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)
59da2e3ebdSchin 			SFMTXRETURN(f,-1);
60da2e3ebdSchin 		SFLOCK(f,local);
61da2e3ebdSchin 
62da2e3ebdSchin 		/* current extent of available data */
63da2e3ebdSchin 		if((r = f->endb-f->next) > 0)
64da2e3ebdSchin 		{	/* on first iteration, n is amount beyond current buffer;
65da2e3ebdSchin 			   afterward, n is the exact amount requested */
66da2e3ebdSchin 			if((first && n <= 0) || (!first && n <= r) ||
67da2e3ebdSchin 			   (f->flags&SF_STRING))
68da2e3ebdSchin 				break;
69da2e3ebdSchin 
70da2e3ebdSchin 			/* try shifting left to make room for new data */
71da2e3ebdSchin 			if(!(f->bits&SF_MMAP) && f->next > f->data &&
72da2e3ebdSchin 			   n > (f->size - (f->endb-f->data)) )
73da2e3ebdSchin 			{	ssize_t	s = r;
74da2e3ebdSchin 
75da2e3ebdSchin 				/* try to maintain block alignment */
76da2e3ebdSchin 				if(f->blksz > 0 && (f->here%f->blksz) == 0 )
77da2e3ebdSchin 				{	s = ((r + f->blksz-1)/f->blksz)*f->blksz;
78da2e3ebdSchin 					if(s+n > f->size)
79da2e3ebdSchin 						s = r;
80da2e3ebdSchin 				}
81da2e3ebdSchin 
82da2e3ebdSchin 				memcpy(f->data, f->endb-s, s);
83da2e3ebdSchin 				f->next = f->data + (s-r);
84da2e3ebdSchin 				f->endb = f->data + s;
85da2e3ebdSchin 			}
86da2e3ebdSchin 		}
87da2e3ebdSchin 		else if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )
88da2e3ebdSchin 			f->next = f->endb = f->endr = f->data;
89da2e3ebdSchin 
90da2e3ebdSchin 		if(f->bits&SF_MMAP)
91da2e3ebdSchin 			r = n > 0 ? n : f->size;
92da2e3ebdSchin 		else if(!(f->flags&SF_STRING) )
93da2e3ebdSchin 		{	r = f->size - (f->endb - f->data); /* available buffer */
94da2e3ebdSchin 			if(n > 0)
95da2e3ebdSchin 			{	if(r > n && f->extent < 0 && (f->flags&SF_SHARE) )
96da2e3ebdSchin 					r = n;	/* read only as much as requested */
97da2e3ebdSchin 				else if(justseek && n <= f->iosz && f->iosz <= f->size)
98da2e3ebdSchin 					r = f->iosz;	/* limit buffer filling */
99da2e3ebdSchin 			}
100da2e3ebdSchin 		}
101da2e3ebdSchin 
102da2e3ebdSchin 		/* SFRD takes care of discipline read and stack popping */
103da2e3ebdSchin 		f->mode |= rcrv;
104da2e3ebdSchin 		f->getr = rc;
105da2e3ebdSchin 		if((r = SFRD(f,f->endb,r,f->disc)) >= 0)
106da2e3ebdSchin 		{	r = f->endb - f->next;
107da2e3ebdSchin 			break;
108da2e3ebdSchin 		}
109da2e3ebdSchin 	}
110da2e3ebdSchin 
111da2e3ebdSchin 	SFOPEN(f,local);
112da2e3ebdSchin 
113da2e3ebdSchin 	rcrv = (n == 0) ? (r > 0 ? (int)(*f->next++) : EOF) : (int)r;
114da2e3ebdSchin 
115da2e3ebdSchin 	SFMTXRETURN(f,rcrv);
116da2e3ebdSchin }
117