xref: /titanic_44/usr/src/lib/libast/common/sfio/sfpeek.c (revision db8b037b5616a366b7dfdc01ef9552f02f9adfdd)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2010 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 /*	Safe access to the internal stream buffer.
25 **	This function is obsolete. sfreserve() should be used.
26 **
27 **	Written by Kiem-Phong Vo (06/27/90).
28 */
29 
30 #if _BLD_sfio && defined(__EXPORT__)
31 #define extern	__EXPORT__
32 #endif
33 
34 #if __STD_C
35 extern ssize_t sfpeek(reg Sfio_t* f, Void_t** bp, reg size_t size)
36 #else
37 extern ssize_t sfpeek(f,bp,size)
38 reg Sfio_t*	f;	/* file to peek */
39 Void_t**	bp;	/* start of data area */
40 reg size_t	size;	/* size of peek */
41 #endif
42 {	reg ssize_t	n, sz;
43 	reg int		mode;
44 
45 	/* query for the extent of the remainder of the buffer */
46 	if((sz = size) == 0 || !bp)
47 	{	if(f->mode&SF_INIT)
48 			(void)_sfmode(f,0,0);
49 
50 		if((f->flags&SF_RDWRSTR) == SF_RDWRSTR)
51 		{	SFSTRSIZE(f);
52 			n = (f->data+f->here) - f->next;
53 		}
54 		else	n = f->endb - f->next;
55 
56 		if(!bp)
57 			return n;
58 		else if(n > 0)	/* size == 0 */
59 		{	*bp = (Void_t*)f->next;
60 			return 0;
61 		}
62 		/* else fall down and fill buffer */
63 	}
64 
65 	if(!(mode = f->flags&SF_READ) )
66 		mode = SF_WRITE;
67 	if((int)f->mode != mode && _sfmode(f,mode,0) < 0)
68 		return -1;
69 
70 	*bp = sfreserve(f, sz <= 0 ? 0 : sz > f->size ? f->size : sz, 0);
71 
72 	if(*bp && sz >= 0)
73 		return sz;
74 
75 	if((n = sfvalue(f)) > 0)
76 	{	*bp = (Void_t*)f->next;
77 		if(sz < 0)
78 		{	f->mode |= SF_PEEK;
79 			f->endr = f->endw = f->data;
80 		}
81 		else
82 		{	if(sz > n)
83 				sz = n;
84 			f->next += sz;
85 		}
86 	}
87 
88 	return (sz >= 0 && n >= sz) ? sz : n;
89 }
90