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#if defined(__STDPP__directive) && defined(__STDPP__hide)
23da2e3ebchin__STDPP__directive pragma pp:hide getpagesize
24da2e3ebchin#else
25da2e3ebchin#define getpagesize	______getpagesize
26da2e3ebchin#endif
27da2e3ebchin
28da2e3ebchin#include	"sfhdr.h"
29da2e3ebchin
30da2e3ebchin#if defined(__STDPP__directive) && defined(__STDPP__hide)
31da2e3ebchin__STDPP__directive pragma pp:nohide getpagesize
32da2e3ebchin#else
33da2e3ebchin#undef	getpagesize
34da2e3ebchin#endif
35da2e3ebchin
36da2e3ebchin#if _lib_getpagesize
37da2e3ebchin_BEGIN_EXTERNS_
38da2e3ebchinextern int	getpagesize _ARG_((void));
39da2e3ebchin_END_EXTERNS_
40da2e3ebchin#endif
41da2e3ebchin
42da2e3ebchin/*	Set a (new) buffer for a stream.
43da2e3ebchin**	If size < 0, it is assigned a suitable value depending on the
44da2e3ebchin**	kind of stream. The actual buffer size allocated is dependent
45da2e3ebchin**	on how much memory is available.
46da2e3ebchin**
47da2e3ebchin**	Written by Kiem-Phong Vo.
48da2e3ebchin*/
49da2e3ebchin
50da2e3ebchin#if !_sys_stat
51da2e3ebchinstruct stat
52da2e3ebchin{	int	st_mode;
53da2e3ebchin	int	st_size;
54da2e3ebchin};
55da2e3ebchin#undef sysfstatf
56da2e3ebchin#define sysfstatf(fd,st)	(-1)
57da2e3ebchin#endif /*_sys_stat*/
58da2e3ebchin
59da2e3ebchinstatic int setlinemode()
60da2e3ebchin{	char*			astsfio;
61da2e3ebchin	char*			endw;
62da2e3ebchin
63da2e3ebchin	static int		modes = -1;
64da2e3ebchin	static const char	sf_line[] = "SF_LINE";
65da2e3ebchin	static const char	sf_wcwidth[] = "SF_WCWIDTH";
66da2e3ebchin
67da2e3ebchin#define ISSEPAR(c)	((c) == ',' || (c) == ' ' || (c) == '\t')
68da2e3ebchin	if (modes < 0)
69da2e3ebchin	{	modes = 0;
70da2e3ebchin		if(astsfio = getenv("_AST_SFIO_OPTIONS"))
71da2e3ebchin		{	for(; *astsfio != 0; astsfio = endw)
72da2e3ebchin			{	while(ISSEPAR(*astsfio) )
73da2e3ebchin					*astsfio++;
74da2e3ebchin				for(endw = astsfio; *endw && !ISSEPAR(*endw); ++endw)
75da2e3ebchin					;
76da2e3ebchin				if((endw-astsfio) == (sizeof(sf_line)-1) &&
77da2e3ebchin				   strncmp(astsfio,sf_line,endw-astsfio) == 0)
78da2e3ebchin				{	if ((modes |= SF_LINE) == (SF_LINE|SF_WCWIDTH))
79da2e3ebchin						break;
80da2e3ebchin				}
81da2e3ebchin				else if((endw-astsfio) == (sizeof(sf_wcwidth)-1) &&
82da2e3ebchin				   strncmp(astsfio,sf_wcwidth,endw-astsfio) == 0)
83da2e3ebchin				{	if ((modes |= SF_WCWIDTH) == (SF_LINE|SF_WCWIDTH))
84da2e3ebchin						break;
85da2e3ebchin				}
86da2e3ebchin			}
87da2e3ebchin		}
88da2e3ebchin	}
89da2e3ebchin	return modes;
90da2e3ebchin}
91da2e3ebchin
92da2e3ebchin#if __STD_C
937c2fbfbApril ChinVoid_t* sfsetbuf(Sfio_t* f, Void_t* buf, size_t size)
94da2e3ebchin#else
95da2e3ebchinVoid_t* sfsetbuf(f,buf,size)
967c2fbfbApril ChinSfio_t*	f;	/* stream to be buffered */
977c2fbfbApril ChinVoid_t*	buf;	/* new buffer */
987c2fbfbApril Chinsize_t	size;	/* buffer size, -1 for default size */
99da2e3ebchin#endif
100da2e3ebchin{
101da2e3ebchin	int		sf_malloc, oflags, init, okmmap, local;
102da2e3ebchin	ssize_t		bufsize, blksz;
103da2e3ebchin	Sfdisc_t*	disc;
104da2e3ebchin	sfstat_t	st;
105da2e3ebchin	uchar*		obuf = NIL(uchar*);
106da2e3ebchin	ssize_t		osize = 0;
1077c2fbfbApril Chin	SFMTXDECL(f);
108da2e3ebchin
109da2e3ebchin	SFONCE();
110da2e3ebchin
1117c2fbfbApril Chin	SFMTXENTER(f,NIL(Void_t*));
112da2e3ebchin
113da2e3ebchin	GETLOCAL(f,local);
114da2e3ebchin
115da2e3ebchin	if(size == 0 && buf)
116da2e3ebchin	{	/* special case to get buffer info */
117da2e3ebchin		_Sfi = f->val = (f->bits&SF_MMAP) ? (f->endb-f->data) : f->size;
118da2e3ebchin		SFMTXRETURN(f, (Void_t*)f->data);
119da2e3ebchin	}
120da2e3ebchin
121da2e3ebchin	/* cleanup actions already done, don't allow write buffering any more */
122da2e3ebchin	if(_Sfexiting && !(f->flags&SF_STRING) && (f->mode&SF_WRITE))
123da2e3ebchin	{	buf = NIL(Void_t*);
124da2e3ebchin		size = 0;
125da2e3ebchin	}
126da2e3ebchin
127da2e3ebchin	if((init = f->mode&SF_INIT) )
128da2e3ebchin	{	if(!f->pool && _sfsetpool(f) < 0)
129da2e3ebchin			SFMTXRETURN(f, NIL(Void_t*));
130da2e3ebchin	}
131da2e3ebchin	else if((f->mode&SF_RDWR) != SFMODE(f,local) && _sfmode(f,0,local) < 0)
132da2e3ebchin		SFMTXRETURN(f, NIL(Void_t*));
133da2e3ebchin
134da2e3ebchin	if(init)
135da2e3ebchin		f->mode = (f->mode&SF_RDWR)|SF_LOCK;
136da2e3ebchin	else
137da2e3ebchin	{	int	rv;
138da2e3ebchin
139da2e3ebchin		/* make sure there is no hidden read data */
140da2e3ebchin		if(f->proc && (f->flags&SF_READ) && (f->mode&SF_WRITE) &&
141da2e3ebchin		   _sfmode(f,SF_READ,local) < 0)
142da2e3ebchin			SFMTXRETURN(f, NIL(Void_t*));
143da2e3ebchin
144da2e3ebchin		/* synchronize first */
145da2e3ebchin		SFLOCK(f,local); rv = SFSYNC(f); SFOPEN(f,local);
146da2e3ebchin		if(rv < 0)
147da2e3ebchin			SFMTXRETURN(f, NIL(Void_t*));
148da2e3ebchin
149da2e3ebchin		/* turn off the SF_SYNCED bit because buffer is changing */
150da2e3ebchin		f->mode &= ~SF_SYNCED;
151da2e3ebchin	}
152da2e3ebchin
153da2e3ebchin	SFLOCK(f,local);
154da2e3ebchin
155da2e3ebchin	if((Sfio_t*)buf != f)
156da2e3ebchin		blksz = -1;
157da2e3ebchin	else /* setting alignment size only */
158da2e3ebchin	{	blksz = (ssize_t)size;
159da2e3ebchin
160da2e3ebchin		if(!init) /* stream already initialized */
161da2e3ebchin		{	obuf = f->data;
162da2e3ebchin			osize = f->size;
163da2e3ebchin			goto done;
164da2e3ebchin		}
165da2e3ebchin		else /* initialize stream as if in the default case */
166da2e3ebchin		{	buf = NIL(Void_t*);
167da2e3ebchin			size = (size_t)SF_UNBOUND;
168da2e3ebchin		}
169da2e3ebchin	}
170da2e3ebchin
171da2e3ebchin	bufsize = 0;
172da2e3ebchin	oflags = f->flags;
173da2e3ebchin
174da2e3ebchin	/* see if memory mapping is possible (see sfwrite for SF_BOTH) */
175da2e3ebchin	okmmap = (buf || (f->flags&SF_STRING) || (f->flags&SF_RDWR) == SF_RDWR) ? 0 : 1;
176da2e3ebchin
177da2e3ebchin	/* save old buffer info */
178da2e3ebchin#ifdef MAP_TYPE
179da2e3ebchin	if(f->bits&SF_MMAP)
180da2e3ebchin	{	if(f->data)
181da2e3ebchin		{	SFMUNMAP(f,f->data,f->endb-f->data);
182da2e3ebchin			f->data = NIL(uchar*);
183da2e3ebchin		}
184da2e3ebchin	} else
185da2e3ebchin#endif
186da2e3ebchin	if(f->data == f->tiny)
187da2e3ebchin	{	f->data = NIL(uchar*);
188da2e3ebchin		f->size = 0;
189da2e3ebchin	}
190da2e3ebchin	obuf  = f->data;
191da2e3ebchin	osize = f->size;
192da2e3ebchin
193da2e3ebchin	f->flags &= ~SF_MALLOC;
194da2e3ebchin	f->bits  &= ~SF_MMAP;
195da2e3ebchin
196da2e3ebchin	/* pure read/string streams must have a valid string */
197da2e3ebchin	if((f->flags&(SF_RDWR|SF_STRING)) == SF_RDSTR &&
198da2e3ebchin	   (size == (size_t)SF_UNBOUND || !buf))
199da2e3ebchin		size = 0;
200da2e3ebchin
201da2e3ebchin	/* set disc to the first discipline with a seekf */
202da2e3ebchin	for(disc = f->disc; disc; disc = disc->disc)
203da2e3ebchin		if(disc->seekf)
204da2e3ebchin			break;
205da2e3ebchin
206da2e3ebchin	if((init || local) && !(f->flags&SF_STRING))
207da2e3ebchin	{	/* ASSERT(f->file >= 0) */
208da2e3ebchin		st.st_mode = 0;
209da2e3ebchin
210da2e3ebchin		/* if has discipline, set size by discipline if possible */
211da2e3ebchin		if(!_sys_stat || disc)
212da2e3ebchin		{	if((f->here = SFSK(f,(Sfoff_t)0,SEEK_CUR,disc)) < 0)
213da2e3ebchin				goto unseekable;
214da2e3ebchin			else
215da2e3ebchin			{	Sfoff_t	e;
216da2e3ebchin				if((e = SFSK(f,(Sfoff_t)0,SEEK_END,disc)) >= 0)
217da2e3ebchin					f->extent = e > f->here ? e : f->here;
218da2e3ebchin				(void)SFSK(f,f->here,SEEK_SET,disc);
219da2e3ebchin				goto setbuf;
220da2e3ebchin			}
221da2e3ebchin		}
222da2e3ebchin
223da2e3ebchin		/* get file descriptor status */
224da2e3ebchin		if(sysfstatf((int)f->file,&st) < 0)
225da2e3ebchin			f->here = -1;
226da2e3ebchin		else
227da2e3ebchin		{
228da2e3ebchin#if _sys_stat && _stat_blksize	/* preferred io block size */
229da2e3ebchin			f->blksz = (size_t)st.st_blksize;
230da2e3ebchin#endif
231da2e3ebchin			bufsize = 64 * 1024;
232da2e3ebchin			if(S_ISDIR(st.st_mode) || (Sfoff_t)st.st_size < (Sfoff_t)SF_GRAIN)
233da2e3ebchin				okmmap = 0;
234da2e3ebchin			if(S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))
235da2e3ebchin				f->here = SFSK(f,(Sfoff_t)0,SEEK_CUR,f->disc);
236da2e3ebchin			else	f->here = -1;
237da2e3ebchin
238da2e3ebchin#if O_TEXT /* no memory mapping with O_TEXT because read()/write() alter data stream */
239da2e3ebchin			if(okmmap && f->here >= 0 &&
240da2e3ebchin			   (sysfcntlf((int)f->file,F_GETFL,0) & O_TEXT) )
241da2e3ebchin				okmmap = 0;
242da2e3ebchin#endif
243da2e3ebchin		}
244da2e3ebchin
245da2e3ebchin		if(init)
246da2e3ebchin			f->flags |= setlinemode();
247da2e3ebchin
248da2e3ebchin		if(f->here >= 0)
249da2e3ebchin		{	f->extent = (Sfoff_t)st.st_size;
250da2e3ebchin
251da2e3ebchin			/* seekable std-devices are share-public by default */
252da2e3ebchin			if(f == sfstdin || f == sfstdout || f == sfstderr)
253da2e3ebchin				f->flags |= SF_SHARE|SF_PUBLIC;
254da2e3ebchin		}
255da2e3ebchin		else
256da2e3ebchin		{
257da2e3ebchin		unseekable:
258da2e3ebchin			f->extent = -1;
259da2e3ebchin			f->here = 0;
260da2e3ebchin
261da2e3ebchin			if(init)
262da2e3ebchin			{	if(S_ISCHR(st.st_mode) )
263da2e3ebchin				{	int oerrno = errno;
264da2e3ebchin
265da2e3ebchin					bufsize = SF_GRAIN;
266da2e3ebchin
267da2e3ebchin					/* set line mode for terminals */
268da2e3ebchin					if(!(f->flags&(SF_LINE|SF_WCWIDTH)) && isatty(f->file))
269da2e3ebchin						f->flags |= SF_LINE|SF_WCWIDTH;
270da2e3ebchin#if _sys_stat
271da2e3ebchin					else	/* special case /dev/null */
272da2e3ebchin					{	reg int	dev, ino;
273da2e3ebchin						dev = (int)st.st_dev;
274da2e3ebchin						ino = (int)st.st_ino;
275da2e3ebchin						if(sysstatf(DEVNULL,&st) >= 0 &&
276da2e3ebchin						   dev == (int)st.st_dev &&
277da2e3ebchin						   ino == (int)st.st_ino)
278da2e3ebchin							SFSETNULL(f);
279da2e3ebchin					}
280da2e3ebchin#endif
281da2e3ebchin					errno = oerrno;
282da2e3ebchin				}
283da2e3ebchin
284da2e3ebchin				/* initialize side buffer for r+w unseekable streams */
285da2e3ebchin				if(!f->proc && (f->bits&SF_BOTH) )
286da2e3ebchin					(void)_sfpopen(f,-1,-1,1);
287da2e3ebchin			}
288da2e3ebchin		}
289da2e3ebchin
290da2e3ebchin		/* set page size, this is also the desired default buffer size */
291da2e3ebchin		if(_Sfpage <= 0)
292da2e3ebchin		{
293da2e3ebchin#if _lib_getpagesize
294da2e3ebchin			if((_Sfpage = (size_t)getpagesize()) <= 0)
295da2e3ebchin#endif
296da2e3ebchin				_Sfpage = SF_PAGE;
297da2e3ebchin		}
298da2e3ebchin	}
299da2e3ebchin
300da2e3ebchin#ifdef MAP_TYPE
301da2e3ebchin	if(okmmap && size && (f->mode&SF_READ) && f->extent >= 0 )
302da2e3ebchin	{	/* see if we can try memory mapping */
303da2e3ebchin		if(!disc)
304da2e3ebchin			for(disc = f->disc; disc; disc = disc->disc)
305da2e3ebchin				if(disc->readf)
306da2e3ebchin					break;
307da2e3ebchin		if(!disc)
308da2e3ebchin		{	f->bits |= SF_MMAP;
309da2e3ebchin			if(size == (size_t)SF_UNBOUND)
310da2e3ebchin			{	if(bufsize > _Sfpage)
311da2e3ebchin					size = bufsize * SF_NMAP;
312da2e3ebchin				else	size = _Sfpage * SF_NMAP;
313da2e3ebchin				if(size > 256*1024)
314da2e3ebchin					size = 256*1024;
315da2e3ebchin			}
316da2e3ebchin		}
317da2e3ebchin	}
318da2e3ebchin#endif
319da2e3ebchin
320da2e3ebchin	/* get buffer space */
321da2e3ebchinsetbuf:
322da2e3ebchin	if(size == (size_t)SF_UNBOUND)
323da2e3ebchin	{	/* define a default size suitable for block transfer */
324da2e3ebchin		if(init && osize > 0)
325da2e3ebchin			size = osize;
326da2e3ebchin		else if(f == sfstderr && (f->mode&SF_WRITE))
327da2e3ebchin			size = 0;
328da2e3ebchin		else if(f->flags&SF_STRING )
329da2e3ebchin			size = SF_GRAIN;
330da2e3ebchin		else if((f->flags&SF_READ) && !(f->bits&SF_BOTH) &&
331da2e3ebchin			f->extent > 0 && f->extent < (Sfoff_t)_Sfpage )
332da2e3ebchin			size = (((size_t)f->extent + SF_GRAIN-1)/SF_GRAIN)*SF_GRAIN;
333da2e3ebchin		else if((ssize_t)(size = _Sfpage) < bufsize)
334da2e3ebchin			size = bufsize;
335da2e3ebchin
336da2e3ebchin		buf = NIL(Void_t*);
337da2e3ebchin	}
338da2e3ebchin
339da2e3ebchin	sf_malloc = 0;
340da2e3ebchin	if(size > 0 && !buf && !(f->bits&SF_MMAP))
341da2e3ebchin	{	/* try to allocate a buffer */
342da2e3ebchin		if(obuf && size == (size_t)osize && init)
343da2e3ebchin		{	buf = (Void_t*)obuf;
344da2e3ebchin			obuf = NIL(uchar*);
345da2e3ebchin			sf_malloc = (oflags&SF_MALLOC);
346da2e3ebchin		}
347da2e3ebchin		if(!buf)
348da2e3ebchin		{	/* do allocation */
349da2e3ebchin			while(!buf && size > 0)
350da2e3ebchin			{	if((buf = (Void_t*)malloc(size)) )
351da2e3ebchin					break;
352da2e3ebchin				else	size /= 2;
353da2e3ebchin			}
354da2e3ebchin			if(size > 0)
355da2e3ebchin				sf_malloc = SF_MALLOC;
356da2e3ebchin		}
357da2e3ebchin	}
358da2e3ebchin
359da2e3ebchin	if(size == 0 && !(f->flags&SF_STRING) && !(f->bits&SF_MMAP) && (f->mode&SF_READ))
360da2e3ebchin	{	/* use the internal buffer */
361da2e3ebchin		size = sizeof(f->tiny);
362da2e3ebchin		buf = (Void_t*)f->tiny;
363da2e3ebchin	}
364da2e3ebchin
365da2e3ebchin	/* set up new buffer */
366da2e3ebchin	f->size = size;
367da2e3ebchin	f->next = f->data = f->endr = f->endw = (uchar*)buf;
368da2e3ebchin	f->endb = (f->mode&SF_READ) ? f->data : f->data+size;
369da2e3ebchin	if(f->flags&SF_STRING)
370da2e3ebchin	{	/* these fields are used to test actual size - see sfseek() */
371da2e3ebchin		f->extent = (!sf_malloc &&
372da2e3ebchin			     ((f->flags&SF_READ) || (f->bits&SF_BOTH)) ) ? size : 0;
373da2e3ebchin		f->here = 0;
374da2e3ebchin
375da2e3ebchin		/* read+string stream should have all data available */
376da2e3ebchin		if((f->mode&SF_READ) && !sf_malloc)
377da2e3ebchin			f->endb = f->data+size;
378da2e3ebchin	}
379da2e3ebchin
380da2e3ebchin	f->flags = (f->flags & ~SF_MALLOC)|sf_malloc;
381da2e3ebchin
382da2e3ebchin	if(obuf && obuf != f->data && osize > 0 && (oflags&SF_MALLOC))
383da2e3ebchin	{	free((Void_t*)obuf);
384da2e3ebchin		obuf = NIL(uchar*);
385da2e3ebchin	}
386da2e3ebchin
387da2e3ebchindone:
388da2e3ebchin	_Sfi = f->val = obuf ? osize : 0;
389da2e3ebchin
390da2e3ebchin	/* blksz is used for aligning disk block boundary while reading data to
391da2e3ebchin	** optimize data transfer from disk (eg, via direct I/O). blksz can be
392da2e3ebchin	** at most f->size/2 so that data movement in buffer can be optimized.
393da2e3ebchin	** blksz should also be a power-of-2 for optimal disk seeks.
394da2e3ebchin	*/
395da2e3ebchin	if(blksz <= 0 || (blksz & (blksz-1)) != 0 )
396da2e3ebchin		blksz = SF_GRAIN;
397da2e3ebchin	while(blksz > f->size/2)
398da2e3ebchin		blksz /= 2;
399da2e3ebchin	f->blksz = blksz;
400da2e3ebchin
401da2e3ebchin	SFOPEN(f,local);
402da2e3ebchin
403da2e3ebchin	SFMTXRETURN(f, (Void_t*)obuf);
404da2e3ebchin}
405