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#ifndef _SFHDR_H
23da2e3ebchin#define _SFHDR_H	1
24da2e3ebchin#if !defined(_BLD_sfio) && !defined(_BLD_stdio)
25da2e3ebchin#define _BLD_sfio	1
26da2e3ebchin#endif
27da2e3ebchin
28da2e3ebchin/*	Internal definitions for sfio.
29da2e3ebchin**	Written by Kiem-Phong Vo
30da2e3ebchin*/
31da2e3ebchin
32da2e3ebchin#define _next		next
33da2e3ebchin#define _endw		endw
34da2e3ebchin#define _endr		endr
35da2e3ebchin#define _endb		endb
36da2e3ebchin#define _push		push
37da2e3ebchin#define _flags		flags
38da2e3ebchin#define _file		file
39da2e3ebchin#define _data		data
40da2e3ebchin#define _size		size
41da2e3ebchin#define _val		val
42da2e3ebchin
43da2e3ebchin#include	"FEATURE/sfio"
44da2e3ebchin#include	"FEATURE/mmap"
45da2e3ebchin
46da2e3ebchin/* define va_list, etc. before including sfio_t.h (sfio.h) */
47da2e3ebchin#if !_PACKAGE_ast
48da2e3ebchin
49da2e3ebchin/* some systems don't know large files */
50da2e3ebchin#if defined(_NO_LARGEFILE64_SOURCE) || _mips == 2 /* || __hppa */
51da2e3ebchin#undef _NO_LARGEFILE64_SOURCE
52da2e3ebchin#define _NO_LARGEFILE64_SOURCE	1
53da2e3ebchin#undef	_LARGEFILE64_SOURCE
54da2e3ebchin#undef	_LARGEFILE_SOURCE
55da2e3ebchin#endif
56da2e3ebchin
57da2e3ebchin#if !_NO_LARGEFILE64_SOURCE && _typ_off64_t && _lib_lseek64 && _lib_stat64
58da2e3ebchin#undef	_LARGEFILE64_SOURCE
59da2e3ebchin#undef	_LARGEFILE_SOURCE
60da2e3ebchin#undef	_FILE_OFFSET_BITS
61da2e3ebchin#define _LARGEFILE64_SOURCE	1	/* enabling the *64 stuff */
62da2e3ebchin#define _LARGEFILE_SOURCE	1
63da2e3ebchin#endif
64da2e3ebchin
65da2e3ebchin#if _hdr_stdarg
66da2e3ebchin#include	<stdarg.h>
67da2e3ebchin#else
68da2e3ebchin#include	<varargs.h>
69da2e3ebchin#endif
70da2e3ebchin#include	"FEATURE/common"
71da2e3ebchin#if !__STD_C
72da2e3ebchin#define const
73da2e3ebchin#endif
74da2e3ebchin#endif /* !_PACKAGE_ast */
75da2e3ebchin
76da2e3ebchin#include	"sfio_t.h"
77da2e3ebchin
78da2e3ebchin/* note that the macro vt_threaded has effect on vthread.h */
79da2e3ebchin#include	<vthread.h>
80da2e3ebchin
81da2e3ebchin/* file system info */
82da2e3ebchin#if _PACKAGE_ast
83da2e3ebchin
84da2e3ebchin#include	<ast.h>
85da2e3ebchin#include	<ast_time.h>
86da2e3ebchin#include	<ast_tty.h>
87da2e3ebchin#include	<ls.h>
88da2e3ebchin
89da2e3ebchin/* ast always provides multibyte handling */
90da2e3ebchin#undef _hdr_wchar
91da2e3ebchin#undef _lib_mbrtowc
92da2e3ebchin#undef _lib_wcrtomb
93da2e3ebchin#define _hdr_wchar	1
94da2e3ebchin#define _lib_mbrtowc	1
95da2e3ebchin#define _lib_wcrtomb	1
96da2e3ebchin
97da2e3ebchin#if _mem_st_blksize_stat
98da2e3ebchin#define _stat_blksize	1
99da2e3ebchin#endif
100da2e3ebchin
101da2e3ebchin#if _lib_localeconv && _hdr_locale
102da2e3ebchin#define _lib_locale	1
103da2e3ebchin#endif
104da2e3ebchin
105da2e3ebchin#define sfoff_t		off_t
106da2e3ebchin#define sfstat_t	struct stat
107da2e3ebchin#define sysclosef	close
108da2e3ebchin#define syscreatf	creat
109da2e3ebchin#define sysdupf		dup
110da2e3ebchin#define sysfcntlf	fcntl
111da2e3ebchin#define sysfstatf	fstat
112da2e3ebchin#define sysftruncatef	ftruncate
113da2e3ebchin#define syslseekf	lseek
114da2e3ebchin#define sysmmapf	mmap
115da2e3ebchin#define sysmunmapf	munmap
116da2e3ebchin#define sysopenf	open
117da2e3ebchin#define syspipef	pipe
118da2e3ebchin#define sysreadf	read
119da2e3ebchin#define sysremovef	remove
120da2e3ebchin#define sysstatf	stat
121da2e3ebchin#define syswritef	write
122da2e3ebchin
123da2e3ebchin#else /*!_PACKAGE_ast*/
124da2e3ebchin
125da2e3ebchin/* when building the binary compatibility package, a number of header files
126da2e3ebchin   are not needed and they may get in the way so we remove them here.
127da2e3ebchin*/
128da2e3ebchin#if _SFBINARY_H
129da2e3ebchin#undef  _hdr_time
130da2e3ebchin#undef  _sys_time
131da2e3ebchin#undef  _sys_stat
132da2e3ebchin#undef  _hdr_stat
133da2e3ebchin#undef  _hdr_filio
134da2e3ebchin#undef  _sys_filio
135da2e3ebchin#undef  _lib_poll
136da2e3ebchin#undef  _stream_peek
137da2e3ebchin#undef  _socket_peek
138da2e3ebchin#undef  _hdr_vfork
139da2e3ebchin#undef  _sys_vfork
140da2e3ebchin#undef  _lib_vfork
141da2e3ebchin#undef  _hdr_values
142da2e3ebchin#undef  _hdr_math
143da2e3ebchin#undef  _sys_mman
144da2e3ebchin#undef  _hdr_mman
145da2e3ebchin#undef  _sys_ioctl
146da2e3ebchin#endif
147da2e3ebchin
148da2e3ebchin#if _hdr_stdlib
149da2e3ebchin#include	<stdlib.h>
150da2e3ebchin#endif
151da2e3ebchin
152da2e3ebchin#if _hdr_string
153da2e3ebchin#include	<string.h>
154da2e3ebchin#endif
155da2e3ebchin
156da2e3ebchin#if _hdr_time
157da2e3ebchin#include	<time.h>
158da2e3ebchin#endif
159da2e3ebchin#if _sys_time
160da2e3ebchin#include	<sys/time.h>
161da2e3ebchin#endif
162da2e3ebchin
163da2e3ebchin#if _sys_stat
164da2e3ebchin#include	<sys/stat.h>
165da2e3ebchin#else
166da2e3ebchin#if _hdr_stat
167da2e3ebchin#include	<stat.h>
168da2e3ebchin#ifndef _sys_stat
169da2e3ebchin#define	_sys_stat	1
170da2e3ebchin#endif
171da2e3ebchin#endif
172da2e3ebchin#endif /*_sys_stat*/
173da2e3ebchin
174da2e3ebchin#ifndef _sys_stat
175da2e3ebchin#define _sys_stat	0
176da2e3ebchin#endif
177da2e3ebchin
178da2e3ebchin#include	<fcntl.h>
179da2e3ebchin
180da2e3ebchin#ifndef F_SETFD
181da2e3ebchin#ifndef FIOCLEX
182da2e3ebchin#if _hdr_filio
183da2e3ebchin#include	<filio.h>
184da2e3ebchin#else
185da2e3ebchin#if _sys_filio
186da2e3ebchin#include	<sys/filio.h>
187da2e3ebchin#endif /*_sys_filio*/
188da2e3ebchin#endif /*_hdr_filio*/
189da2e3ebchin#endif /*_FIOCLEX*/
190da2e3ebchin#endif /*F_SETFD*/
191da2e3ebchin
192da2e3ebchin#if _hdr_unistd
193da2e3ebchin#include	<unistd.h>
194da2e3ebchin#endif
195da2e3ebchin
196da2e3ebchin#if !_LARGEFILE64_SOURCE	/* turn off the *64 stuff */
197da2e3ebchin#undef	_typ_off64_t
198da2e3ebchin#undef	_typ_struct_stat64
199da2e3ebchin#undef	_lib_creat64
200da2e3ebchin#undef	_lib_open64
201da2e3ebchin#undef	_lib_close64
202da2e3ebchin#undef	_lib_stat64
203da2e3ebchin#undef	_lib_fstat64
204da2e3ebchin#undef	_lib_ftruncate64
205da2e3ebchin#undef	_lib_lseek64
206da2e3ebchin#undef	_lib_mmap64
207da2e3ebchin#undef	_lib_munmap64
208da2e3ebchin#endif /*!_LARGEFILE64_SOURCE */
209da2e3ebchin
210da2e3ebchin/* see if we can use memory mapping for io */
211da2e3ebchin#if _LARGEFILE64_SOURCE && !_lib_mmap64
212da2e3ebchin#undef _mmap_worthy
213da2e3ebchin#endif
214da2e3ebchin#if !_mmap_worthy
215da2e3ebchin#undef _hdr_mman
216da2e3ebchin#undef _sys_mman
217da2e3ebchin#endif
218da2e3ebchin#if _hdr_mman
219da2e3ebchin#include	<mman.h>
220da2e3ebchin#endif
221da2e3ebchin#if _sys_mman
222da2e3ebchin#include	<sys/mman.h>
223da2e3ebchin#endif
224da2e3ebchin
225da2e3ebchin/* standardize system calls and types dealing with files */
226da2e3ebchin#if _typ_off64_t
227da2e3ebchin#define sfoff_t		off64_t
228da2e3ebchin#else
229da2e3ebchin#define sfoff_t		off_t
230da2e3ebchin#endif
231da2e3ebchin#if _typ_struct_stat64
232da2e3ebchin#define sfstat_t	struct stat64
233da2e3ebchin#else
234da2e3ebchin#define sfstat_t	struct stat
235da2e3ebchin#endif
236da2e3ebchin#if _lib_lseek64
237da2e3ebchin#define syslseekf	lseek64
238da2e3ebchin#else
239da2e3ebchin#define syslseekf	lseek
240da2e3ebchin#endif
241da2e3ebchin#if _lib_stat64
242da2e3ebchin#define sysstatf	stat64
243da2e3ebchin#else
244da2e3ebchin#define sysstatf	stat
245da2e3ebchin#endif
246da2e3ebchin#if _lib_fstat64
247da2e3ebchin#define sysfstatf	fstat64
248da2e3ebchin#else
249da2e3ebchin#define sysfstatf	fstat
250da2e3ebchin#endif
251da2e3ebchin#if _lib_mmap64
252da2e3ebchin#define sysmmapf	mmap64
253da2e3ebchin#else
254da2e3ebchin#define sysmmapf	mmap
255da2e3ebchin#endif
256da2e3ebchin#if _lib_munmap64
257da2e3ebchin#define sysmunmapf	munmap64
258da2e3ebchin#else
259da2e3ebchin#define sysmunmapf	munmap
260da2e3ebchin#endif
261da2e3ebchin#if _lib_open64
262da2e3ebchin#define sysopenf	open64
263da2e3ebchin#else
264da2e3ebchin#define sysopenf	open
265da2e3ebchin#endif
266da2e3ebchin#if _lib_creat64
267da2e3ebchin#define syscreatf	creat64
268da2e3ebchin#else
269da2e3ebchin#define syscreatf	creat
270da2e3ebchin#endif
271da2e3ebchin#if _lib_close64
272da2e3ebchin#define sysclosef	close64
273da2e3ebchin#else
274da2e3ebchin#define sysclosef	close
275da2e3ebchin#endif
276da2e3ebchin#if _lib_ftruncate64
277da2e3ebchin#undef _lib_ftruncate
278da2e3ebchin#define _lib_ftruncate	1
279da2e3ebchin#define sysftruncatef	ftruncate64
280da2e3ebchin#endif
281da2e3ebchin#if !_lib_ftruncate64 && _lib_ftruncate
282da2e3ebchin#define sysftruncatef	ftruncate
283da2e3ebchin#endif
284da2e3ebchin#if _lib_remove
285da2e3ebchin#define sysremovef	remove
286da2e3ebchin#else
287da2e3ebchin#define sysremovef	unlink
288da2e3ebchin#endif
289da2e3ebchin
290da2e3ebchin#define sysreadf	read
291da2e3ebchin#define syswritef	write
292da2e3ebchin#define syspipef	pipe
293da2e3ebchin#define sysdupf		dup
294da2e3ebchin#define sysfcntlf	fcntl
295da2e3ebchin
296da2e3ebchin#endif /*_PACKAGE_ast*/
297da2e3ebchin
298da2e3ebchin#if !_mmap_worthy
299da2e3ebchin#undef MAP_TYPE
300da2e3ebchin#endif
301da2e3ebchin
302da2e3ebchin#include	"FEATURE/float"
303da2e3ebchin
304da2e3ebchin#include	<errno.h>
305da2e3ebchin#include	<ctype.h>
306da2e3ebchin
307da2e3ebchin/* deal with multi-byte character and string conversions */
308da2e3ebchin#if _PACKAGE_ast
309da2e3ebchin
310da2e3ebchin#include	<wchar.h>
311da2e3ebchin
312da2e3ebchin#define _has_multibyte		1
313da2e3ebchin
314da2e3ebchin#define SFMBMAX			mbmax()
315da2e3ebchin#define SFMBCPY(to,fr)		memcpy((to), (fr), sizeof(mbstate_t))
316da2e3ebchin#define SFMBCLR(mb)		memset((mb), 0,  sizeof(mbstate_t))
317da2e3ebchin#define SFMBSET(lhs,v)		(lhs = (v))
318da2e3ebchin#define SFMBLEN(s,mb)		mbsize(s)
319da2e3ebchin#define SFMBDCL(ms)		mbstate_t ms;
320da2e3ebchin
321da2e3ebchin#else
322da2e3ebchin
323da2e3ebchin#if _hdr_wchar && _typ_mbstate_t && _lib_wcrtomb && _lib_mbrtowc
324da2e3ebchin#define _has_multibyte		1	/* Xopen-compliant	*/
325da2e3ebchin#include	<wchar.h>
326da2e3ebchin#define SFMBCPY(to,fr)		memcpy((to), (fr), sizeof(mbstate_t))
327da2e3ebchin#define SFMBCLR(mb)		memset((mb), 0,  sizeof(mbstate_t))
328da2e3ebchin#define SFMBSET(lhs,v)		(lhs = (v))
329da2e3ebchin#define SFMBDCL(mb)		mbstate_t mb;
330da2e3ebchin#define SFMBLEN(s,mb)		mbrtowc(NIL(wchar_t*), (s), SFMBMAX, (mb) )
331da2e3ebchin#endif /*_hdr_wchar && _typ_mbstate_t && _lib_wcrtomb && _lib_mbrtowc*/
332da2e3ebchin
333da2e3ebchin#if !_has_multibyte && _hdr_wchar && _lib_mbtowc && _lib_wctomb
334da2e3ebchin#define _has_multibyte		2	/* no shift states	*/
335da2e3ebchin#include	<wchar.h>
336da2e3ebchin#undef mbrtowc
337da2e3ebchin#define mbrtowc(wp,s,n,mb)	mbtowc(wp, s, n)
338da2e3ebchin#undef wcrtomb
339da2e3ebchin#define wcrtomb(s,wc,mb)	wctomb(s, wc)
340da2e3ebchin#define SFMBCPY(to,fr)
341da2e3ebchin#define SFMBCLR(mb)
342da2e3ebchin#define SFMBSET(lhs,v)
343da2e3ebchin#define SFMBDCL(mb)
344da2e3ebchin#define SFMBLEN(s,mb)		mbrtowc(NIL(wchar_t*), (s), SFMBMAX, (mb) )
345da2e3ebchin#endif /*!_has_multibyte && _hdr_wchar && _lib_mbtowc && _lib_wctomb*/
346da2e3ebchin
347da2e3ebchin#ifdef MB_CUR_MAX
348da2e3ebchin#define SFMBMAX			MB_CUR_MAX
349da2e3ebchin#else
350da2e3ebchin#define SFMBMAX			sizeof(Sflong_t)
351da2e3ebchin#endif
352da2e3ebchin
353da2e3ebchin#endif /* _PACKAGE_ast */
354da2e3ebchin
355da2e3ebchin#if !_has_multibyte
356da2e3ebchin#define _has_multibyte		0	/* no multibyte support	*/
357da2e3ebchin#define SFMBCPY(to,fr)
358da2e3ebchin#define SFMBCLR(mb)
359da2e3ebchin#define SFMBSET(lhs,v)
360da2e3ebchin#define SFMBLEN(s,mb)		(*(s) ? 1 : 0)
361da2e3ebchin#define SFMBDCL(mb)
362da2e3ebchin#endif /* _has_multibyte */
363da2e3ebchin
3647c2fbfbApril Chin/* dealing with streams that might be accessed concurrently */
365da2e3ebchin#if vt_threaded
366da2e3ebchin
3677c2fbfbApril Chin#define SFMTXdecl(ff,_mf_)	Sfio_t* _mf_ = (ff)
3687c2fbfbApril Chin#define SFMTXbegin(ff,_mf_,rv) \
3697c2fbfbApril Chin	{	if((ff)->_flags&SF_MTSAFE) \
3707c2fbfbApril Chin		{	(_mf_) = (ff); \
3717c2fbfbApril Chin			if(sfmutex((ff), SFMTX_LOCK) != 0) return(rv); \
3727c2fbfbApril Chin			if(_Sfnotify) \
3737c2fbfbApril Chin			{	(*_Sfnotify)((_mf_), SF_MTACCESS, (Void_t*)(&(ff)) ); \
3747c2fbfbApril Chin				if(!(ff)) (ff) = (_mf_); \
3757c2fbfbApril Chin			} \
3767c2fbfbApril Chin		} \
3777c2fbfbApril Chin	}
3787c2fbfbApril Chin#define SFMTXend(ff,_mf_) \
3797c2fbfbApril Chin	{	if((ff)->_flags&SF_MTSAFE) \
3807c2fbfbApril Chin		{	if(_Sfnotify) \
3817c2fbfbApril Chin				(*_Sfnotify)((_mf_), SF_MTACCESS, NIL(Void_t*) ); \
3827c2fbfbApril Chin			sfmutex((ff), SFMTX_UNLOCK); \
3837c2fbfbApril Chin			(ff) = (_mf_); \
3847c2fbfbApril Chin		} \
3857c2fbfbApril Chin	}
3867c2fbfbApril Chin
3877c2fbfbApril Chin#define SFONCE()		(_Sfdone ? 0 : vtonce(_Sfonce,_Sfoncef))
3887c2fbfbApril Chin
3897c2fbfbApril Chin#define SFMTXLOCK(f)		(((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_LOCK) : 0)
3907c2fbfbApril Chin#define SFMTXUNLOCK(f)		(((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_UNLOCK) : 0)
391da2e3ebchin
3927c2fbfbApril Chin#define SFMTXDECL(ff)		SFMTXdecl((ff), _mtxf1_)
3937c2fbfbApril Chin#define SFMTXBEGIN(ff,v) 	{ SFMTXbegin((ff), _mtxf1_, (v) ); }
3947c2fbfbApril Chin#define SFMTXEND(ff)		{ SFMTXend(ff, _mtxf1_); }
3957c2fbfbApril Chin#define SFMTXENTER(ff,v) 	{ if(!(ff)) return(v); SFMTXBEGIN((ff), (v)); }
3967c2fbfbApril Chin#define SFMTXRETURN(ff,v)	{ SFMTXEND(ff); return(v); }
3977c2fbfbApril Chin
3987c2fbfbApril Chin#define SFMTXDECL2(ff)		SFMTXdecl((ff), _mtxf2_)
3997c2fbfbApril Chin#define SFMTXBEGIN2(ff,v) 	{ SFMTXbegin((ff), _mtxf2_, (v) ); }
4007c2fbfbApril Chin#define SFMTXEND2(ff)		{ SFMTXend((ff), _mtxf2_); }
401da2e3ebchin
402da2e3ebchin#define POOLMTXLOCK(p)		( vtmtxlock(&(p)->mutex) )
403da2e3ebchin#define POOLMTXUNLOCK(p)	( vtmtxunlock(&(p)->mutex) )
4047c2fbfbApril Chin#define POOLMTXENTER(p)		{ POOLMTXLOCK(p); }
4057c2fbfbApril Chin#define POOLMTXRETURN(p,rv)	{ POOLMTXUNLOCK(p); return(rv); }
406da2e3ebchin
407da2e3ebchin#else /*!vt_threaded*/
408da2e3ebchin
409da2e3ebchin#undef SF_MTSAFE /* no need to worry about thread-safety */
410da2e3ebchin#define SF_MTSAFE		0
411da2e3ebchin
412da2e3ebchin#define SFONCE()		/*(0)*/
413da2e3ebchin
414da2e3ebchin#define SFMTXLOCK(f)		/*(0)*/
415da2e3ebchin#define SFMTXUNLOCK(f)		/*(0)*/
4167c2fbfbApril Chin
4177c2fbfbApril Chin#define	SFMTXDECL(ff)		/*(0)*/
4187c2fbfbApril Chin#define SFMTXBEGIN(ff,v) 	/*(0)*/
4197c2fbfbApril Chin#define SFMTXEND(ff)		/*(0)*/
4207c2fbfbApril Chin#define SFMTXENTER(ff,v)	{ if(!(ff)) return(v); }
4217c2fbfbApril Chin#define SFMTXRETURN(ff,v)	{ return(v); }
4227c2fbfbApril Chin
4237c2fbfbApril Chin#define SFMTXDECL2(ff)		/*(0)*/
4247c2fbfbApril Chin#define SFMTXBEGIN2(ff,v) 	/*(0)*/
4257c2fbfbApril Chin#define SFMTXEND2(ff)		/*(0)*/
426da2e3ebchin
427da2e3ebchin#define POOLMTXLOCK(p)
428da2e3ebchin#define POOLMTXUNLOCK(p)
4297c2fbfbApril Chin#define POOLMTXENTER(p)
430da2e3ebchin#define POOLMTXRETURN(p,v)	{ return(v); }
431da2e3ebchin
432da2e3ebchin#endif /*vt_threaded*/
433da2e3ebchin
434da2e3ebchin
435da2e3ebchin/* functions for polling readiness of streams */
436da2e3ebchin#if _lib_select
437da2e3ebchin#undef _lib_poll
438da2e3ebchin#else
439da2e3ebchin#if _lib_poll_fd_1 || _lib_poll_fd_2
440da2e3ebchin#define _lib_poll	1
441da2e3ebchin#endif
442da2e3ebchin#endif /*_lib_select_*/
443da2e3ebchin
444da2e3ebchin#if _lib_poll
445da2e3ebchin#include	<poll.h>
446da2e3ebchin
447da2e3ebchin#if _lib_poll_fd_1
448da2e3ebchin#define SFPOLL(pfd,n,tm)	poll((pfd),(ulong)(n),(tm))
449da2e3ebchin#else
450da2e3ebchin#define SFPOLL(pfd,n,tm)	poll((ulong)(n),(pfd),(tm))
451da2e3ebchin#endif
452da2e3ebchin#endif /*_lib_poll*/
453da2e3ebchin
454da2e3ebchin#if _stream_peek
455da2e3ebchin#include	<stropts.h>
456da2e3ebchin#endif
457da2e3ebchin
458da2e3ebchin#if _socket_peek
459da2e3ebchin#include	<sys/socket.h>
460da2e3ebchin#endif
461da2e3ebchin
462da2e3ebchin/* to test for executable access mode of a file */
463da2e3ebchin#ifndef X_OK
464da2e3ebchin#define X_OK	01
465da2e3ebchin#endif
466da2e3ebchin
467da2e3ebchin/* alternative process forking */
468da2e3ebchin#if _lib_vfork && !defined(fork) && !defined(sparc) && !defined(__sparc)
469da2e3ebchin#if _hdr_vfork
470da2e3ebchin#include	<vfork.h>
471da2e3ebchin#endif
472da2e3ebchin#if _sys_vfork
473da2e3ebchin#include	<sys/vfork.h>
474da2e3ebchin#endif
475da2e3ebchin#define fork	vfork
476da2e3ebchin#endif
477da2e3ebchin
478da2e3ebchin/* to get rid of pesky compiler warnings */
479da2e3ebchin#if __STD_C
480da2e3ebchin#define NOTUSED(x)	(void)(x)
481da2e3ebchin#else
482da2e3ebchin#define NOTUSED(x)	(&x,1)
483da2e3ebchin#endif
484da2e3ebchin
485da2e3ebchin/* Private flags in the "bits" field */
486da2e3ebchin#define SF_MMAP		00000001	/* in memory mapping mode		*/
487da2e3ebchin#define SF_BOTH		00000002	/* both read/write			*/
488da2e3ebchin#define SF_HOLE		00000004	/* a hole of zero's was created		*/
489da2e3ebchin#define SF_NULL		00000010	/* stream is /dev/null			*/
490da2e3ebchin#define SF_SEQUENTIAL	00000020	/* sequential access			*/
491da2e3ebchin#define SF_JUSTSEEK	00000040	/* just did a sfseek			*/
492da2e3ebchin#define SF_PRIVATE	00000100	/* private stream to Sfio, no mutex	*/
493da2e3ebchin#define SF_ENDING	00000200	/* no re-io on interrupts at closing	*/
494da2e3ebchin#define SF_WIDE		00000400	/* in wide mode - stdio only		*/
495da2e3ebchin
496da2e3ebchin/* "bits" flags that must be cleared in sfclrlock */
497da2e3ebchin#define SF_TMPBITS	00170000
498da2e3ebchin#define SF_DCDOWN	00010000	/* recurse down the discipline stack	*/
499da2e3ebchin
500da2e3ebchin#define SF_WCFORMAT	00020000	/* wchar_t formatting - stdio only	*/
501da2e3ebchin#if _has_multibyte
502da2e3ebchin#define SFWCSET(f)	((f)->bits |= SF_WCFORMAT)
503da2e3ebchin#define SFWCGET(f,v)	(((v) = (f)->bits & SF_WCFORMAT), ((f)->bits &= ~SF_WCFORMAT) )
504da2e3ebchin#else
505da2e3ebchin#define SFWCSET(f)
506da2e3ebchin#define SFWCGET(f,v)
507da2e3ebchin#endif
508da2e3ebchin
509da2e3ebchin#define SF_MVSIZE	00040000	/* f->size was reset in sfmove()	*/
510da2e3ebchin#define SFMVSET(f)	(((f)->size *= SF_NMAP), ((f)->bits |= SF_MVSIZE) )
511da2e3ebchin#define SFMVUNSET(f)	(!((f)->bits&SF_MVSIZE) ? 0 : \
512da2e3ebchin				(((f)->bits &= ~SF_MVSIZE), ((f)->size /= SF_NMAP)) )
513da2e3ebchin
514da2e3ebchin#define SFCLRBITS(f)	(SFMVUNSET(f), ((f)->bits &= ~SF_TMPBITS) )
515da2e3ebchin
516da2e3ebchin
517da2e3ebchin/* bits for the mode field, SF_INIT defined in sfio_t.h */
518da2e3ebchin#define SF_RC		00000010	/* peeking for a record			*/
519da2e3ebchin#define SF_RV		00000020	/* reserve without read	or most write	*/
520da2e3ebchin#define SF_LOCK		00000040	/* stream is locked for io op		*/
521da2e3ebchin#define SF_PUSH		00000100	/* stream has been pushed		*/
522da2e3ebchin#define SF_POOL		00000200	/* stream is in a pool but not current	*/
523da2e3ebchin#define SF_PEEK		00000400	/* there is a pending peek		*/
524da2e3ebchin#define SF_PKRD		00001000	/* did a peek read			*/
525da2e3ebchin#define SF_GETR		00002000	/* did a getr on this stream		*/
526da2e3ebchin#define SF_SYNCED	00004000	/* stream was synced			*/
527da2e3ebchin#define SF_STDIO	00010000	/* given up the buffer to stdio		*/
528da2e3ebchin#define SF_AVAIL	00020000	/* was closed, available for reuse	*/
529da2e3ebchin#define SF_LOCAL	00100000	/* sentinel for a local call		*/
530da2e3ebchin
531da2e3ebchin#ifdef DEBUG
532da2e3ebchin#define ASSERT(p)	((p) ? 0 : (abort(),0) )
533da2e3ebchin#else
534da2e3ebchin#define ASSERT(p)
535da2e3ebchin#endif
536da2e3ebchin
537da2e3ebchin/* short-hands */
538da2e3ebchin#define NIL(t)		((t)0)
539da2e3ebchin#define reg		register
540da2e3ebchin#ifndef uchar
541da2e3ebchin#define uchar		unsigned char
542da2e3ebchin#endif
543da2e3ebchin#ifndef ulong
544da2e3ebchin#define ulong		unsigned long
545da2e3ebchin#endif
546da2e3ebchin#ifndef uint
547da2e3ebchin#define uint		unsigned int
548da2e3ebchin#endif
549da2e3ebchin#ifndef ushort
550da2e3ebchin#define ushort		unsigned short
551da2e3ebchin#endif
552da2e3ebchin
553da2e3ebchin#define SECOND		1000	/* millisecond units */
554da2e3ebchin
555da2e3ebchin/* macros do determine stream types from sfstat_t data */
556da2e3ebchin#ifndef S_IFMT
557da2e3ebchin#define S_IFMT	0
558da2e3ebchin#endif
559da2e3ebchin#ifndef S_IFDIR
560