1da2e3ebchin/***********************************************************************
2da2e3ebchin*                                                                      *
3da2e3ebchin*               This software is part of the ast package               *
43e14f97Roger A. Faulkner*          Copyright (c) 1985-2010 AT&T Intellectual Property          *
5da2e3ebchin*                      and is licensed under the                       *
6da2e3ebchin*                  Common Public License, Version 1.0                  *
77c2fbfbApril Chin*                    by AT&T Intellectual Property                     *
8da2e3ebchin*                                                                      *
9da2e3ebchin*                A copy of the License is available at                 *
10da2e3ebchin*            http://www.opensource.org/licenses/cpl1.0.txt             *
11da2e3ebchin*         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12da2e3ebchin*                                                                      *
13da2e3ebchin*              Information and Software Systems Research               *
14da2e3ebchin*                            AT&T Research                             *
15da2e3ebchin*                           Florham Park NJ                            *
16da2e3ebchin*                                                                      *
17da2e3ebchin*                 Glenn Fowler <gsf@research.att.com>                  *
18da2e3ebchin*                  David Korn <dgk@research.att.com>                   *
19da2e3ebchin*                   Phong Vo <kpv@research.att.com>                    *
20da2e3ebchin*                                                                      *
21da2e3ebchin***********************************************************************/
22da2e3ebchin#include	"sfhdr.h"
23da2e3ebchin
24da2e3ebchin/*	Safe access to the internal stream buffer.
25da2e3ebchin**	This function is obsolete. sfreserve() should be used.
26da2e3ebchin**
27da2e3ebchin**	Written by Kiem-Phong Vo (06/27/90).
28da2e3ebchin*/
29da2e3ebchin
30da2e3ebchin#if _BLD_sfio && defined(__EXPORT__)
31da2e3ebchin#define extern	__EXPORT__
32da2e3ebchin#endif
33da2e3ebchin
34da2e3ebchin#if __STD_C
35da2e3ebchinextern ssize_t sfpeek(reg Sfio_t* f, Void_t** bp, reg size_t size)
36da2e3ebchin#else
37da2e3ebchinextern ssize_t sfpeek(f,bp,size)
38da2e3ebchinreg Sfio_t*	f;	/* file to peek */
39da2e3ebchinVoid_t**	bp;	/* start of data area */
40da2e3ebchinreg size_t	size;	/* size of peek */
41da2e3ebchin#endif
42da2e3ebchin{	reg ssize_t	n, sz;
43da2e3ebchin	reg int		mode;
44da2e3ebchin
45da2e3ebchin	/* query for the extent of the remainder of the buffer */
46da2e3ebchin	if((sz = size) == 0 || !bp)
47da2e3ebchin	{	if(f->mode&SF_INIT)
48da2e3ebchin			(void)_sfmode(f,0,0);
49da2e3ebchin
50da2e3ebchin		if((f->flags&SF_RDWRSTR) == SF_RDWRSTR)
51da2e3ebchin		{	SFSTRSIZE(f);
52da2e3ebchin			n = (f->data+f->here) - f->next;
53da2e3ebchin		}
54da2e3ebchin		else	n = f->endb - f->next;
55da2e3ebchin
56da2e3ebchin		if(!bp)
57da2e3ebchin			return n;
58da2e3ebchin		else if(n > 0)	/* size == 0 */
59da2e3ebchin		{	*bp = (Void_t*)f->next;
60da2e3ebchin			return 0;
61da2e3ebchin		}
62da2e3ebchin		/* else fall down and fill buffer */
63da2e3ebchin	}
64da2e3ebchin
65da2e3ebchin	if(!(mode = f->flags&SF_READ) )
66da2e3ebchin		mode = SF_WRITE;
67da2e3ebchin	if((int)f->mode != mode && _sfmode(f,mode,0) < 0)
68da2e3ebchin		return -1;
69da2e3ebchin
70da2e3ebchin	*bp = sfreserve(f, sz <= 0 ? 0 : sz > f->size ? f->size : sz, 0);
71da2e3ebchin
72da2e3ebchin	if(*bp && sz >= 0)
73da2e3ebchin		return sz;
74da2e3ebchin
75da2e3ebchin	if((n = sfvalue(f)) > 0)
76da2e3ebchin	{	*bp = (Void_t*)f->next;
77da2e3ebchin		if(sz < 0)
78da2e3ebchin		{	f->mode |= SF_PEEK;
79da2e3ebchin			f->endr = f->endw = f->data;
80da2e3ebchin		}
81da2e3ebchin		else
82da2e3ebchin		{	if(sz > n)
83da2e3ebchin				sz = n;
84da2e3ebchin			f->next += sz;
85da2e3ebchin		}
86da2e3ebchin	}
87da2e3ebchin
88da2e3ebchin	return (sz >= 0 && n >= sz) ? sz : n;
89da2e3ebchin}
90