xref: /onnv-gate/usr/src/lib/libast/common/sfio/sfpeek.c (revision 12068:08a39a083754)
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 /*	Safe access to the internal stream buffer.
254887Schin **	This function is obsolete. sfreserve() should be used.
264887Schin **
274887Schin **	Written by Kiem-Phong Vo (06/27/90).
284887Schin */
294887Schin 
304887Schin #if _BLD_sfio && defined(__EXPORT__)
314887Schin #define extern	__EXPORT__
324887Schin #endif
334887Schin 
344887Schin #if __STD_C
sfpeek(reg Sfio_t * f,Void_t ** bp,reg size_t size)354887Schin extern ssize_t sfpeek(reg Sfio_t* f, Void_t** bp, reg size_t size)
364887Schin #else
374887Schin extern ssize_t sfpeek(f,bp,size)
384887Schin reg Sfio_t*	f;	/* file to peek */
394887Schin Void_t**	bp;	/* start of data area */
404887Schin reg size_t	size;	/* size of peek */
414887Schin #endif
424887Schin {	reg ssize_t	n, sz;
434887Schin 	reg int		mode;
444887Schin 
454887Schin 	/* query for the extent of the remainder of the buffer */
464887Schin 	if((sz = size) == 0 || !bp)
474887Schin 	{	if(f->mode&SF_INIT)
484887Schin 			(void)_sfmode(f,0,0);
494887Schin 
504887Schin 		if((f->flags&SF_RDWRSTR) == SF_RDWRSTR)
514887Schin 		{	SFSTRSIZE(f);
524887Schin 			n = (f->data+f->here) - f->next;
534887Schin 		}
544887Schin 		else	n = f->endb - f->next;
554887Schin 
564887Schin 		if(!bp)
574887Schin 			return n;
584887Schin 		else if(n > 0)	/* size == 0 */
594887Schin 		{	*bp = (Void_t*)f->next;
604887Schin 			return 0;
614887Schin 		}
624887Schin 		/* else fall down and fill buffer */
634887Schin 	}
644887Schin 
654887Schin 	if(!(mode = f->flags&SF_READ) )
664887Schin 		mode = SF_WRITE;
674887Schin 	if((int)f->mode != mode && _sfmode(f,mode,0) < 0)
684887Schin 		return -1;
694887Schin 
704887Schin 	*bp = sfreserve(f, sz <= 0 ? 0 : sz > f->size ? f->size : sz, 0);
714887Schin 
724887Schin 	if(*bp && sz >= 0)
734887Schin 		return sz;
744887Schin 
754887Schin 	if((n = sfvalue(f)) > 0)
764887Schin 	{	*bp = (Void_t*)f->next;
774887Schin 		if(sz < 0)
784887Schin 		{	f->mode |= SF_PEEK;
794887Schin 			f->endr = f->endw = f->data;
804887Schin 		}
814887Schin 		else
824887Schin 		{	if(sz > n)
834887Schin 				sz = n;
844887Schin 			f->next += sz;
854887Schin 		}
864887Schin 	}
874887Schin 
884887Schin 	return (sz >= 0 && n >= sz) ? sz : n;
894887Schin }
90