14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #include "sfhdr.h"
234887Schin
244887Schin /* Fill the buffer of a stream with data.
254887Schin ** If n < 0, sffilbuf() attempts to fill the buffer if it's empty.
264887Schin ** If n == 0, if the buffer is not empty, just return the first byte;
274887Schin ** otherwise fill the buffer and return the first byte.
284887Schin ** If n > 0, even if the buffer is not empty, try a read to get as
294887Schin ** close to n as possible. n is reset to -1 if stack pops.
304887Schin **
314887Schin ** Written by Kiem-Phong Vo
324887Schin */
334887Schin
344887Schin #if __STD_C
_sffilbuf(Sfio_t * f,reg int n)354887Schin int _sffilbuf(Sfio_t* f, reg int n)
364887Schin #else
374887Schin int _sffilbuf(f,n)
384887Schin Sfio_t* f; /* fill the read buffer of this stream */
394887Schin reg int n; /* see above */
404887Schin #endif
414887Schin {
424887Schin reg ssize_t r;
434887Schin reg int first, local, rcrv, rc, justseek;
448462SApril.Chin@Sun.COM SFMTXDECL(f);
454887Schin
468462SApril.Chin@Sun.COM SFMTXENTER(f,-1);
474887Schin
484887Schin GETLOCAL(f,local);
494887Schin
504887Schin /* any peek data must be preserved across stacked streams */
514887Schin rcrv = f->mode&(SF_RC|SF_RV|SF_LOCK);
524887Schin rc = f->getr;
534887Schin
544887Schin justseek = f->bits&SF_JUSTSEEK; f->bits &= ~SF_JUSTSEEK;
554887Schin
564887Schin for(first = 1;; first = 0, (f->mode &= ~SF_LOCK) )
574887Schin { /* check mode */
584887Schin if(SFMODE(f,local) != SF_READ && _sfmode(f,SF_READ,local) < 0)
594887Schin SFMTXRETURN(f,-1);
604887Schin SFLOCK(f,local);
614887Schin
624887Schin /* current extent of available data */
634887Schin if((r = f->endb-f->next) > 0)
644887Schin { /* on first iteration, n is amount beyond current buffer;
654887Schin afterward, n is the exact amount requested */
664887Schin if((first && n <= 0) || (!first && n <= r) ||
674887Schin (f->flags&SF_STRING))
684887Schin break;
694887Schin
704887Schin /* try shifting left to make room for new data */
714887Schin if(!(f->bits&SF_MMAP) && f->next > f->data &&
724887Schin n > (f->size - (f->endb-f->data)) )
734887Schin { ssize_t s = r;
744887Schin
754887Schin /* try to maintain block alignment */
764887Schin if(f->blksz > 0 && (f->here%f->blksz) == 0 )
774887Schin { s = ((r + f->blksz-1)/f->blksz)*f->blksz;
784887Schin if(s+n > f->size)
794887Schin s = r;
804887Schin }
814887Schin
824887Schin memcpy(f->data, f->endb-s, s);
834887Schin f->next = f->data + (s-r);
844887Schin f->endb = f->data + s;
854887Schin }
864887Schin }
874887Schin else if(!(f->flags&SF_STRING) && !(f->bits&SF_MMAP) )
884887Schin f->next = f->endb = f->endr = f->data;
894887Schin
904887Schin if(f->bits&SF_MMAP)
914887Schin r = n > 0 ? n : f->size;
924887Schin else if(!(f->flags&SF_STRING) )
934887Schin { r = f->size - (f->endb - f->data); /* available buffer */
944887Schin if(n > 0)
954887Schin { if(r > n && f->extent < 0 && (f->flags&SF_SHARE) )
964887Schin r = n; /* read only as much as requested */
974887Schin else if(justseek && n <= f->iosz && f->iosz <= f->size)
984887Schin r = f->iosz; /* limit buffer filling */
994887Schin }
1004887Schin }
1014887Schin
1024887Schin /* SFRD takes care of discipline read and stack popping */
1034887Schin f->mode |= rcrv;
1044887Schin f->getr = rc;
1054887Schin if((r = SFRD(f,f->endb,r,f->disc)) >= 0)
1064887Schin { r = f->endb - f->next;
1074887Schin break;
1084887Schin }
1094887Schin }
1104887Schin
1114887Schin SFOPEN(f,local);
1124887Schin
1134887Schin rcrv = (n == 0) ? (r > 0 ? (int)(*f->next++) : EOF) : (int)r;
1144887Schin
1154887Schin SFMTXRETURN(f,rcrv);
1164887Schin }
117