xref: /titanic_50/usr/src/lib/libast/common/sfio/sfhdr.h (revision 1da7e59911a9f3f267c3ef294dc342767dee7952)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1985-2008 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 #ifndef _SFHDR_H
23 #define _SFHDR_H	1
24 #if !defined(_BLD_sfio) && !defined(_BLD_stdio)
25 #define _BLD_sfio	1
26 #endif
27 
28 /*	Internal definitions for sfio.
29 **	Written by Kiem-Phong Vo
30 */
31 
32 #define _next		next
33 #define _endw		endw
34 #define _endr		endr
35 #define _endb		endb
36 #define _push		push
37 #define _flags		flags
38 #define _file		file
39 #define _data		data
40 #define _size		size
41 #define _val		val
42 
43 #include	"FEATURE/sfio"
44 #include	"FEATURE/mmap"
45 
46 /* define va_list, etc. before including sfio_t.h (sfio.h) */
47 #if !_PACKAGE_ast
48 
49 /* some systems don't know large files */
50 #if defined(_NO_LARGEFILE64_SOURCE) || _mips == 2 /* || __hppa */
51 #undef _NO_LARGEFILE64_SOURCE
52 #define _NO_LARGEFILE64_SOURCE	1
53 #undef	_LARGEFILE64_SOURCE
54 #undef	_LARGEFILE_SOURCE
55 #endif
56 
57 #if !_NO_LARGEFILE64_SOURCE && _typ_off64_t && _lib_lseek64 && _lib_stat64
58 #undef	_LARGEFILE64_SOURCE
59 #undef	_LARGEFILE_SOURCE
60 #undef	_FILE_OFFSET_BITS
61 #define _LARGEFILE64_SOURCE	1	/* enabling the *64 stuff */
62 #define _LARGEFILE_SOURCE	1
63 #endif
64 
65 #if _hdr_stdarg
66 #include	<stdarg.h>
67 #else
68 #include	<varargs.h>
69 #endif
70 #include	"FEATURE/common"
71 #if !__STD_C
72 #define const
73 #endif
74 #endif /* !_PACKAGE_ast */
75 
76 #include	"sfio_t.h"
77 
78 /* note that the macro vt_threaded has effect on vthread.h */
79 #include	<vthread.h>
80 
81 /* file system info */
82 #if _PACKAGE_ast
83 
84 #include	<ast.h>
85 #include	<ast_time.h>
86 #include	<ast_tty.h>
87 #include	<ls.h>
88 
89 /* ast always provides multibyte handling */
90 #undef _hdr_wchar
91 #undef _lib_mbrtowc
92 #undef _lib_wcrtomb
93 #define _hdr_wchar	1
94 #define _lib_mbrtowc	1
95 #define _lib_wcrtomb	1
96 
97 #if _mem_st_blksize_stat
98 #define _stat_blksize	1
99 #endif
100 
101 #if _lib_localeconv && _hdr_locale
102 #define _lib_locale	1
103 #endif
104 
105 #define sfoff_t		off_t
106 #define sfstat_t	struct stat
107 #define sysclosef	close
108 #define syscreatf	creat
109 #define sysdupf		dup
110 #define sysfcntlf	fcntl
111 #define sysfstatf	fstat
112 #define sysftruncatef	ftruncate
113 #define syslseekf	lseek
114 #define sysmmapf	mmap
115 #define sysmunmapf	munmap
116 #define sysopenf	open
117 #define syspipef	pipe
118 #define sysreadf	read
119 #define sysremovef	remove
120 #define sysstatf	stat
121 #define syswritef	write
122 
123 #else /*!_PACKAGE_ast*/
124 
125 /* when building the binary compatibility package, a number of header files
126    are not needed and they may get in the way so we remove them here.
127 */
128 #if _SFBINARY_H
129 #undef  _hdr_time
130 #undef  _sys_time
131 #undef  _sys_stat
132 #undef  _hdr_stat
133 #undef  _hdr_filio
134 #undef  _sys_filio
135 #undef  _lib_poll
136 #undef  _stream_peek
137 #undef  _socket_peek
138 #undef  _hdr_vfork
139 #undef  _sys_vfork
140 #undef  _lib_vfork
141 #undef  _hdr_values
142 #undef  _hdr_math
143 #undef  _sys_mman
144 #undef  _hdr_mman
145 #undef  _sys_ioctl
146 #endif
147 
148 #if _hdr_stdlib
149 #include	<stdlib.h>
150 #endif
151 
152 #if _hdr_string
153 #include	<string.h>
154 #endif
155 
156 #if _hdr_time
157 #include	<time.h>
158 #endif
159 #if _sys_time
160 #include	<sys/time.h>
161 #endif
162 
163 #if _sys_stat
164 #include	<sys/stat.h>
165 #else
166 #if _hdr_stat
167 #include	<stat.h>
168 #ifndef _sys_stat
169 #define	_sys_stat	1
170 #endif
171 #endif
172 #endif /*_sys_stat*/
173 
174 #ifndef _sys_stat
175 #define _sys_stat	0
176 #endif
177 
178 #include	<fcntl.h>
179 
180 #ifndef F_SETFD
181 #ifndef FIOCLEX
182 #if _hdr_filio
183 #include	<filio.h>
184 #else
185 #if _sys_filio
186 #include	<sys/filio.h>
187 #endif /*_sys_filio*/
188 #endif /*_hdr_filio*/
189 #endif /*_FIOCLEX*/
190 #endif /*F_SETFD*/
191 
192 #if _hdr_unistd
193 #include	<unistd.h>
194 #endif
195 
196 #if !_LARGEFILE64_SOURCE	/* turn off the *64 stuff */
197 #undef	_typ_off64_t
198 #undef	_typ_struct_stat64
199 #undef	_lib_creat64
200 #undef	_lib_open64
201 #undef	_lib_close64
202 #undef	_lib_stat64
203 #undef	_lib_fstat64
204 #undef	_lib_ftruncate64
205 #undef	_lib_lseek64
206 #undef	_lib_mmap64
207 #undef	_lib_munmap64
208 #endif /*!_LARGEFILE64_SOURCE */
209 
210 /* see if we can use memory mapping for io */
211 #if _LARGEFILE64_SOURCE && !_lib_mmap64
212 #undef _mmap_worthy
213 #endif
214 #if !_mmap_worthy
215 #undef _hdr_mman
216 #undef _sys_mman
217 #endif
218 #if _hdr_mman
219 #include	<mman.h>
220 #endif
221 #if _sys_mman
222 #include	<sys/mman.h>
223 #endif
224 
225 /* standardize system calls and types dealing with files */
226 #if _typ_off64_t
227 #define sfoff_t		off64_t
228 #else
229 #define sfoff_t		off_t
230 #endif
231 #if _typ_struct_stat64
232 #define sfstat_t	struct stat64
233 #else
234 #define sfstat_t	struct stat
235 #endif
236 #if _lib_lseek64
237 #define syslseekf	lseek64
238 #else
239 #define syslseekf	lseek
240 #endif
241 #if _lib_stat64
242 #define sysstatf	stat64
243 #else
244 #define sysstatf	stat
245 #endif
246 #if _lib_fstat64
247 #define sysfstatf	fstat64
248 #else
249 #define sysfstatf	fstat
250 #endif
251 #if _lib_mmap64
252 #define sysmmapf	mmap64
253 #else
254 #define sysmmapf	mmap
255 #endif
256 #if _lib_munmap64
257 #define sysmunmapf	munmap64
258 #else
259 #define sysmunmapf	munmap
260 #endif
261 #if _lib_open64
262 #define sysopenf	open64
263 #else
264 #define sysopenf	open
265 #endif
266 #if _lib_creat64
267 #define syscreatf	creat64
268 #else
269 #define syscreatf	creat
270 #endif
271 #if _lib_close64
272 #define sysclosef	close64
273 #else
274 #define sysclosef	close
275 #endif
276 #if _lib_ftruncate64
277 #undef _lib_ftruncate
278 #define _lib_ftruncate	1
279 #define sysftruncatef	ftruncate64
280 #endif
281 #if !_lib_ftruncate64 && _lib_ftruncate
282 #define sysftruncatef	ftruncate
283 #endif
284 #if _lib_remove
285 #define sysremovef	remove
286 #else
287 #define sysremovef	unlink
288 #endif
289 
290 #define sysreadf	read
291 #define syswritef	write
292 #define syspipef	pipe
293 #define sysdupf		dup
294 #define sysfcntlf	fcntl
295 
296 #endif /*_PACKAGE_ast*/
297 
298 #if !_mmap_worthy
299 #undef MAP_TYPE
300 #endif
301 
302 #include	"FEATURE/float"
303 
304 #include	<errno.h>
305 #include	<ctype.h>
306 
307 /* deal with multi-byte character and string conversions */
308 #if _PACKAGE_ast
309 
310 #include	<wchar.h>
311 
312 #define _has_multibyte		1
313 
314 #define SFMBMAX			mbmax()
315 #define SFMBCPY(to,fr)		memcpy((to), (fr), sizeof(mbstate_t))
316 #define SFMBCLR(mb)		memset((mb), 0,  sizeof(mbstate_t))
317 #define SFMBSET(lhs,v)		(lhs = (v))
318 #define SFMBLEN(s,mb)		mbsize(s)
319 #define SFMBDCL(ms)		mbstate_t ms;
320 
321 #else
322 
323 #if _hdr_wchar && _typ_mbstate_t && _lib_wcrtomb && _lib_mbrtowc
324 #define _has_multibyte		1	/* Xopen-compliant	*/
325 #include	<wchar.h>
326 #define SFMBCPY(to,fr)		memcpy((to), (fr), sizeof(mbstate_t))
327 #define SFMBCLR(mb)		memset((mb), 0,  sizeof(mbstate_t))
328 #define SFMBSET(lhs,v)		(lhs = (v))
329 #define SFMBDCL(mb)		mbstate_t mb;
330 #define SFMBLEN(s,mb)		mbrtowc(NIL(wchar_t*), (s), SFMBMAX, (mb) )
331 #endif /*_hdr_wchar && _typ_mbstate_t && _lib_wcrtomb && _lib_mbrtowc*/
332 
333 #if !_has_multibyte && _hdr_wchar && _lib_mbtowc && _lib_wctomb
334 #define _has_multibyte		2	/* no shift states	*/
335 #include	<wchar.h>
336 #undef mbrtowc
337 #define mbrtowc(wp,s,n,mb)	mbtowc(wp, s, n)
338 #undef wcrtomb
339 #define wcrtomb(s,wc,mb)	wctomb(s, wc)
340 #define SFMBCPY(to,fr)
341 #define SFMBCLR(mb)
342 #define SFMBSET(lhs,v)
343 #define SFMBDCL(mb)
344 #define SFMBLEN(s,mb)		mbrtowc(NIL(wchar_t*), (s), SFMBMAX, (mb) )
345 #endif /*!_has_multibyte && _hdr_wchar && _lib_mbtowc && _lib_wctomb*/
346 
347 #ifdef MB_CUR_MAX
348 #define SFMBMAX			MB_CUR_MAX
349 #else
350 #define SFMBMAX			sizeof(Sflong_t)
351 #endif
352 
353 #endif /* _PACKAGE_ast */
354 
355 #if !_has_multibyte
356 #define _has_multibyte		0	/* no multibyte support	*/
357 #define SFMBCPY(to,fr)
358 #define SFMBCLR(mb)
359 #define SFMBSET(lhs,v)
360 #define SFMBLEN(s,mb)		(*(s) ? 1 : 0)
361 #define SFMBDCL(mb)
362 #endif /* _has_multibyte */
363 
364 /* dealing with streams that might be accessed concurrently */
365 #if vt_threaded
366 
367 #define SFMTXdecl(ff,_mf_)	Sfio_t* _mf_ = (ff)
368 #define SFMTXbegin(ff,_mf_,rv) \
369 	{	if((ff)->_flags&SF_MTSAFE) \
370 		{	(_mf_) = (ff); \
371 			if(sfmutex((ff), SFMTX_LOCK) != 0) return(rv); \
372 			if(_Sfnotify) \
373 			{	(*_Sfnotify)((_mf_), SF_MTACCESS, (Void_t*)(&(ff)) ); \
374 				if(!(ff)) (ff) = (_mf_); \
375 			} \
376 		} \
377 	}
378 #define SFMTXend(ff,_mf_) \
379 	{	if((ff)->_flags&SF_MTSAFE) \
380 		{	if(_Sfnotify) \
381 				(*_Sfnotify)((_mf_), SF_MTACCESS, NIL(Void_t*) ); \
382 			sfmutex((ff), SFMTX_UNLOCK); \
383 			(ff) = (_mf_); \
384 		} \
385 	}
386 
387 #define SFONCE()		(_Sfdone ? 0 : vtonce(_Sfonce,_Sfoncef))
388 
389 #define SFMTXLOCK(f)		(((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_LOCK) : 0)
390 #define SFMTXUNLOCK(f)		(((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_UNLOCK) : 0)
391 
392 #define SFMTXDECL(ff)		SFMTXdecl((ff), _mtxf1_)
393 #define SFMTXBEGIN(ff,v) 	{ SFMTXbegin((ff), _mtxf1_, (v) ); }
394 #define SFMTXEND(ff)		{ SFMTXend(ff, _mtxf1_); }
395 #define SFMTXENTER(ff,v) 	{ if(!(ff)) return(v); SFMTXBEGIN((ff), (v)); }
396 #define SFMTXRETURN(ff,v)	{ SFMTXEND(ff); return(v); }
397 
398 #define SFMTXDECL2(ff)		SFMTXdecl((ff), _mtxf2_)
399 #define SFMTXBEGIN2(ff,v) 	{ SFMTXbegin((ff), _mtxf2_, (v) ); }
400 #define SFMTXEND2(ff)		{ SFMTXend((ff), _mtxf2_); }
401 
402 #define POOLMTXLOCK(p)		( vtmtxlock(&(p)->mutex) )
403 #define POOLMTXUNLOCK(p)	( vtmtxunlock(&(p)->mutex) )
404 #define POOLMTXENTER(p)		{ POOLMTXLOCK(p); }
405 #define POOLMTXRETURN(p,rv)	{ POOLMTXUNLOCK(p); return(rv); }
406 
407 #else /*!vt_threaded*/
408 
409 #undef SF_MTSAFE /* no need to worry about thread-safety */
410 #define SF_MTSAFE		0
411 
412 #define SFONCE()		/*(0)*/
413 
414 #define SFMTXLOCK(f)		/*(0)*/
415 #define SFMTXUNLOCK(f)		/*(0)*/
416 
417 #define	SFMTXDECL(ff)		/*(0)*/
418 #define SFMTXBEGIN(ff,v) 	/*(0)*/
419 #define SFMTXEND(ff)		/*(0)*/
420 #define SFMTXENTER(ff,v)	{ if(!(ff)) return(v); }
421 #define SFMTXRETURN(ff,v)	{ return(v); }
422 
423 #define SFMTXDECL2(ff)		/*(0)*/
424 #define SFMTXBEGIN2(ff,v) 	/*(0)*/
425 #define SFMTXEND2(ff)		/*(0)*/
426 
427 #define POOLMTXLOCK(p)
428 #define POOLMTXUNLOCK(p)
429 #define POOLMTXENTER(p)
430 #define POOLMTXRETURN(p,v)	{ return(v); }
431 
432 #endif /*vt_threaded*/
433 
434 
435 /* functions for polling readiness of streams */
436 #if _lib_select
437 #undef _lib_poll
438 #else
439 #if _lib_poll_fd_1 || _lib_poll_fd_2
440 #define _lib_poll	1
441 #endif
442 #endif /*_lib_select_*/
443 
444 #if _lib_poll
445 #include	<poll.h>
446 
447 #if _lib_poll_fd_1
448 #define SFPOLL(pfd,n,tm)	poll((pfd),(ulong)(n),(tm))
449 #else
450 #define SFPOLL(pfd,n,tm)	poll((ulong)(n),(pfd),(tm))
451 #endif
452 #endif /*_lib_poll*/
453 
454 #if _stream_peek
455 #include	<stropts.h>
456 #endif
457 
458 #if _socket_peek
459 #include	<sys/socket.h>
460 #endif
461 
462 /* to test for executable access mode of a file */
463 #ifndef X_OK
464 #define X_OK	01
465 #endif
466 
467 /* alternative process forking */
468 #if _lib_vfork && !defined(fork) && !defined(sparc) && !defined(__sparc)
469 #if _hdr_vfork
470 #include	<vfork.h>
471 #endif
472 #if _sys_vfork
473 #include	<sys/vfork.h>
474 #endif
475 #define fork	vfork
476 #endif
477 
478 /* to get rid of pesky compiler warnings */
479 #if __STD_C
480 #define NOTUSED(x)	(void)(x)
481 #else
482 #define NOTUSED(x)	(&x,1)
483 #endif
484 
485 /* Private flags in the "bits" field */
486 #define SF_MMAP		00000001	/* in memory mapping mode		*/
487 #define SF_BOTH		00000002	/* both read/write			*/
488 #define SF_HOLE		00000004	/* a hole of zero's was created		*/
489 #define SF_NULL		00000010	/* stream is /dev/null			*/
490 #define SF_SEQUENTIAL	00000020	/* sequential access			*/
491 #define SF_JUSTSEEK	00000040	/* just did a sfseek			*/
492 #define SF_PRIVATE	00000100	/* private stream to Sfio, no mutex	*/
493 #define SF_ENDING	00000200	/* no re-io on interrupts at closing	*/
494 #define SF_WIDE		00000400	/* in wide mode - stdio only		*/
495 
496 /* "bits" flags that must be cleared in sfclrlock */
497 #define SF_TMPBITS	00170000
498 #define SF_DCDOWN	00010000	/* recurse down the discipline stack	*/
499 
500 #define SF_WCFORMAT	00020000	/* wchar_t formatting - stdio only	*/
501 #if _has_multibyte
502 #define SFWCSET(f)	((f)->bits |= SF_WCFORMAT)
503 #define SFWCGET(f,v)	(((v) = (f)->bits & SF_WCFORMAT), ((f)->bits &= ~SF_WCFORMAT) )
504 #else
505 #define SFWCSET(f)
506 #define SFWCGET(f,v)
507 #endif
508 
509 #define SF_MVSIZE	00040000	/* f->size was reset in sfmove()	*/
510 #define SFMVSET(f)	(((f)->size *= SF_NMAP), ((f)->bits |= SF_MVSIZE) )
511 #define SFMVUNSET(f)	(!((f)->bits&SF_MVSIZE) ? 0 : \
512 				(((f)->bits &= ~SF_MVSIZE), ((f)->size /= SF_NMAP)) )
513 
514 #define SFCLRBITS(f)	(SFMVUNSET(f), ((f)->bits &= ~SF_TMPBITS) )
515 
516 
517 /* bits for the mode field, SF_INIT defined in sfio_t.h */
518 #define SF_RC		00000010	/* peeking for a record			*/
519 #define SF_RV		00000020	/* reserve without read	or most write	*/
520 #define SF_LOCK		00000040	/* stream is locked for io op		*/
521 #define SF_PUSH		00000100	/* stream has been pushed		*/
522 #define SF_POOL		00000200	/* stream is in a pool but not current	*/
523 #define SF_PEEK		00000400	/* there is a pending peek		*/
524 #define SF_PKRD		00001000	/* did a peek read			*/
525 #define SF_GETR		00002000	/* did a getr on this stream		*/
526 #define SF_SYNCED	00004000	/* stream was synced			*/
527 #define SF_STDIO	00010000	/* given up the buffer to stdio		*/
528 #define SF_AVAIL	00020000	/* was closed, available for reuse	*/
529 #define SF_LOCAL	00100000	/* sentinel for a local call		*/
530 
531 #ifdef DEBUG
532 #define ASSERT(p)	((p) ? 0 : (abort(),0) )
533 #else
534 #define ASSERT(p)
535 #endif
536 
537 /* short-hands */
538 #define NIL(t)		((t)0)
539 #define reg		register
540 #ifndef uchar
541 #define uchar		unsigned char
542 #endif
543 #ifndef ulong
544 #define ulong		unsigned long
545 #endif
546 #ifndef uint
547 #define uint		unsigned int
548 #endif
549 #ifndef ushort
550 #define ushort		unsigned short
551 #endif
552 
553 #define SECOND		1000	/* millisecond units */
554 
555 /* macros do determine stream types from sfstat_t data */
556 #ifndef S_IFMT
557 #define S_IFMT	0
558 #endif
559 #ifndef S_IFDIR
560 #define S_IFDIR	0
561 #endif
562 #ifndef S_IFREG
563 #define S_IFREG	0
564 #endif
565 #ifndef S_IFCHR
566 #define S_IFCHR	0
567 #endif
568 #ifndef S_IFIFO
569 #define S_IFIFO	0
570 #endif
571 
572 #ifndef S_ISDIR
573 #define S_ISDIR(m)	(((m)&S_IFMT) == S_IFDIR)
574 #endif
575 #ifndef S_ISREG
576 #define S_ISREG(m)	(((m)&S_IFMT) == S_IFREG)
577 #endif
578 #ifndef S_ISCHR
579 #define S_ISCHR(m)	(((m)&S_IFMT) == S_IFCHR)
580 #endif
581 
582 #ifndef S_ISFIFO
583 #	ifdef S_IFIFO
584 #		define S_ISFIFO(m)	(((m)&S_IFMT) == S_IFIFO)
585 #	else
586 #		define S_ISFIFO(m)	(0)
587 #	endif
588 #endif
589 
590 #ifdef S_IRUSR
591 #define SF_CREATMODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
592 #else
593 #define SF_CREATMODE	0666
594 #endif
595 
596 /* set close-on-exec */
597 #ifdef F_SETFD
598 #	ifndef FD_CLOEXEC
599 #		define FD_CLOEXEC	1
600 #	endif /*FD_CLOEXEC*/
601 #	define SETCLOEXEC(fd)		((void)fcntl((fd),F_SETFD,FD_CLOEXEC))
602 #else
603 #	ifdef FIOCLEX
604 #		define SETCLOEXEC(fd)	((void)ioctl((fd),FIOCLEX,0))
605 #	else
606 #		define SETCLOEXEC(fd)
607 #	endif /*FIOCLEX*/
608 #endif /*F_SETFD*/
609 
610 /* a couple of error number that we use, default values are like Linux */
611 #ifndef EINTR
612 #define EINTR	4
613 #endif
614 #ifndef EBADF
615 #define EBADF	9
616 #endif
617 #ifndef EAGAIN
618 #define EAGAIN	11
619 #endif
620 #ifndef ENOMEM
621 #define ENOMEM	12
622 #endif
623 #ifndef EINVAL
624 #define EINVAL	22
625 #endif
626 #ifndef ESPIPE
627 #define ESPIPE	29
628 #endif
629 
630 /* function to get the decimal point for local environment */
631 #if !defined(SFSETLOCALE) && _PACKAGE_ast
632 #include "lclib.h"
633 #define SFSETLOCALE(dp,tp) \
634 	do if (*(dp) == 0) { \
635 		Lc_numeric_t*	lv = (Lc_numeric_t*)LCINFO(AST_LC_NUMERIC)->data; \
636 		*(dp) = lv->decimal; \
637 		if (tp) *(tp) = lv->thousand; \
638 	} while (0)
639 #endif /*!defined(SFSETLOCALE) && _PACKAGE_ast*/
640 
641 #if !defined(SFSETLOCALE) && _lib_locale
642 #include	<locale.h>
643 #define SFSETLOCALE(decimal,thousand) \
644 	do { struct lconv*	lv; \
645 	  if(*(decimal) == 0) \
646 	  { *(decimal) = '.'; \
647 	    if (thousand) *(thousand) = -1; \
648 	    if((lv = localeconv())) \
649 	    { if(lv->decimal_point && *lv->decimal_point) \
650 	    	*(decimal) = *(unsigned char*)lv->decimal_point; \
651 	      if(thousand && lv->thousands_sep && *lv->thousands_sep) \
652 	    	*(thousand) = *(unsigned char*)lv->thousands_sep; \
653 	    } \
654 	  } \
655 	} while (0)
656 #endif /*!defined(SFSETLOCALE) && _lib_locale*/
657 
658 #if !defined(SFSETLOCALE)
659 #define SFSETLOCALE(decimal,thousand)	(*(decimal)='.')
660 #endif
661 
662 /* stream pool structure. */
663 typedef struct _sfpool_s	Sfpool_t;
664 struct _sfpool_s
665 {	Sfpool_t*	next;
666 	int		mode;		/* type of pool			*/
667 	int		s_sf;		/* size of pool array		*/
668 	int		n_sf;		/* number currently in pool	*/
669 	Sfio_t**	sf;		/* array of streams		*/
670 	Sfio_t*		array[3];	/* start with 3			*/
671 	Vtmutex_t	mutex;		/* mutex lock object		*/
672 };
673 
674 /* reserve buffer structure */
675 typedef struct _sfrsrv_s	Sfrsrv_t;
676 struct _sfrsrv_s
677 {	ssize_t		slen;		/* last string length		*/
678 	ssize_t		size;		/* buffer size			*/
679 	uchar		data[1];	/* data buffer			*/
680 };
681 
682 /* co-process structure */
683 typedef struct _sfproc_s	Sfproc_t;
684 struct _sfproc_s
685 {	int		pid;	/* process id			*/
686 	uchar*		rdata;	/* read data being cached	*/
687 	int		ndata;	/* size of cached data		*/
688 	int		size;	/* buffer size			*/
689 	int		file;	/* saved file descriptor	*/
690 	int		sigp;	/* sigpipe protection needed	*/
691 };
692 
693 /* extensions to sfvprintf/sfvscanf */
694 #define FP_SET(fp,fn)	(fp < 0 ? (fn += 1) : (fn = fp) )
695 #define FP_WIDTH	0
696 #define FP_PRECIS	1
697 #define FP_BASE		2
698 #define FP_STR		3
699 #define FP_SIZE		4
700 #define FP_INDEX	5	/* index size	*/
701 
702 typedef struct _fmt_s		Fmt_t;
703 typedef struct _fmtpos_s	Fmtpos_t;
704 typedef union
705 {	int		i, *ip;
706 	long		l, *lp;
707 	short		h, *hp;
708 	uint		ui;
709 	ulong		ul;
710 	ushort		uh;
711 	Sflong_t	ll, *llp;
712 	Sfulong_t	lu;
713 	Sfdouble_t	ld;
714 	double		d;
715 	float		f;
716 #if _has_multibyte
717 	wchar_t		wc;
718 	wchar_t		*ws, **wsp;
719 #endif
720 	char		c, *s, **sp;
721 	uchar		uc, *us, **usp;
722 	Void_t		*vp;
723 	Sffmt_t		*ft;
724 } Argv_t;
725 
726 struct _fmt_s
727 {	char*		form;		/* format string		*/
728 	va_list		args;		/* corresponding arglist	*/
729 	SFMBDCL(mbs)			/* multibyte parsing state	*/
730 
731 	char*		oform;		/* original format string	*/
732 	va_list		oargs;		/* original arg list		*/
733 	int		argn;		/* number of args already used	*/
734 	Fmtpos_t*	fp;		/* position list		*/
735 
736 	Sffmt_t*	ft;		/* formatting environment	*/
737 	Sffmtevent_f	eventf;		/* event function		*/
738 	Fmt_t*		next;		/* stack frame pointer		*/
739 };
740 
741 struct _fmtpos_s
742 {	Sffmt_t	ft;			/* environment			*/
743 	Argv_t	argv;			/* argument value		*/
744 	int	fmt;			/* original format		*/
745 	int	need[FP_INDEX];		/* positions depending on	*/
746 };
747 
748 #define LEFTP		'('
749 #define RIGHTP		')'
750 #define QUOTE		'\''
751 
752 #ifndef CHAR_BIT
753 #define CHAR_BIT	8
754 #endif
755 
756 #define FMTSET(ft, frm,ags, fv, sz, flgs, wid,pr,bs, ts,ns) \
757 	((ft->form = (char*)frm), va_copy(ft->args,ags), \
758 	 (ft->fmt = fv), (ft->size = sz), \
759 	 (ft->flags = (flgs&SFFMT_SET)), \
760 	 (ft->width = wid), (ft->precis = pr), (ft->base = bs), \
761 	 (ft->t_str = ts), (ft->n_str = ns) )
762 #define FMTGET(ft, frm,ags, fv, sz, flgs, wid,pr,bs) \
763 	((frm = ft->form), va_copy(ags,ft->args), \
764 	 (fv = ft->fmt), (sz = ft->size), \
765 	 (flgs = (flgs&~(SFFMT_SET))|(ft->flags&SFFMT_SET)), \
766 	 (wid = ft->width), (pr = ft->precis), (bs = ft->base) )
767 
768 /* format flags&types, must coexist with those in sfio.h */
769 #define SFFMT_FORBIDDEN 000077777777	/* for sfio.h only		*/
770 #define SFFMT_EFORMAT	001000000000	/* sfcvt converting %e		*/
771 #define SFFMT_MINUS	002000000000	/* minus sign			*/
772 #define SFFMT_AFORMAT	004000000000	/* sfcvt converting %a		*/
773 #define SFFMT_UPPER	010000000000	/* sfcvt converting upper	*/
774 
775 #define SFFMT_TYPES	(SFFMT_SHORT|SFFMT_SSHORT | SFFMT_LONG|SFFMT_LLONG|\
776 			 SFFMT_LDOUBLE | SFFMT_IFLAG|SFFMT_JFLAG| \
777 			 SFFMT_TFLAG | SFFMT_ZFLAG )
778 
779 /* type of elements to be converted */
780 #define SFFMT_INT	001		/* %d,%i 		*/
781 #define SFFMT_UINT	002		/* %u,o,x etc.		*/
782 #define SFFMT_FLOAT	004		/* %f,e,g etc.		*/
783 #define SFFMT_CHAR	010		/* %c,C			*/
784 #define SFFMT_POINTER	020		/* %p,n,s,S		*/
785 #define SFFMT_CLASS	040		/* %[			*/
786 
787 /* local variables used across sf-functions */
788 #define _Sfpage		(_Sfextern.sf_page)
789 #define _Sfpool		(_Sfextern.sf_pool)
790 #define _Sfpmove	(_Sfextern.sf_pmove)
791 #define _Sfstack	(_Sfextern.sf_stack)
792 #define _Sfnotify	(_Sfextern.sf_notify)
793 #define _Sfstdsync	(_Sfextern.sf_stdsync)
794 #define _Sfudisc	(&(_Sfextern.sf_udisc))
795 #define _Sfcleanup	(_Sfextern.sf_cleanup)
796 #define _Sfexiting	(_Sfextern.sf_exiting)
797 #define _Sfdone		(_Sfextern.sf_done)
798 #define _Sfonce		(_Sfextern.sf_once)
799 #define _Sfoncef	(_Sfextern.sf_oncef)
800 #define _Sfmutex	(_Sfextern.sf_mutex)
801 typedef struct _sfextern_s
802 {	ssize_t			sf_page;
803 	struct _sfpool_s	sf_pool;
804 	int			(*sf_pmove)_ARG_((Sfio_t*, int));
805 	Sfio_t*			(*sf_stack)_ARG_((Sfio_t*, Sfio_t*));
806 	void			(*sf_notify)_ARG_((Sfio_t*, int, void*));
807 	int			(*sf_stdsync)_ARG_((Sfio_t*));
808 	struct _sfdisc_s	sf_udisc;
809 	void			(*sf_cleanup)_ARG_((void));
810 	int			sf_exiting;
811 	int			sf_done;
812 	Vtonce_t*		sf_once;
813 	void			(*sf_oncef)_ARG_((void));
814 	Vtmutex_t*		sf_mutex;
815 } Sfextern_t;
816 
817 /* get the real value of a byte in a coded long or ulong */
818 #define SFUVALUE(v)	(((ulong)(v))&(SF_MORE-1))
819 #define SFSVALUE(v)	((( long)(v))&(SF_SIGN-1))
820 #define SFBVALUE(v)	(((ulong)(v))&(SF_BYTE-1))
821 
822 /* pick this many bits in each iteration of double encoding */
823 #define SF_PRECIS	7
824 
825 /* grain size for buffer increment */
826 #define SF_GRAIN	1024
827 #define SF_PAGE		((ssize_t)(SF_GRAIN*sizeof(int)*2))
828 
829 /* when the buffer is empty, certain io requests may be better done directly
830    on the given application buffers. The below condition determines when.
831 */
832 #define SFDIRECT(f,n)	(((ssize_t)(n) >= (f)->size) || \
833 			 ((n) >= SF_GRAIN && (ssize_t)(n) >= (f)->size/16 ) )
834 
835 /* number of pages to memory map at a time */
836 #define SF_NMAP		4
837 
838 #ifndef MAP_VARIABLE
839 #define MAP_VARIABLE	0
840 #endif
841 #ifndef _mmap_fixed
842 #define _mmap_fixed	0
843 #endif
844 
845 /* set/unset sequential states for mmap */
846 #if _lib_madvise && defined(MADV_SEQUENTIAL) && defined(MADV_NORMAL)
847 #define SFMMSEQON(f,a,s) \
848 		do { int oerrno = errno; \
849 		     (void)madvise((caddr_t)(a),(size_t)(s),MADV_SEQUENTIAL); \
850 		     errno = oerrno; \
851 		} while(0)
852 #define SFMMSEQOFF(f,a,s) \
853 		do { int oerrno = errno; \
854 		     (void)madvise((caddr_t)(a),(size_t)(s),MADV_NORMAL); \
855 		     errno = oerrno; \
856 		} while(0)
857 #else
858 #define SFMMSEQON(f,a,s)
859 #define SFMMSEQOFF(f,a,s)
860 #endif
861 
862 #define SFMUNMAP(f,a,s)		(sysmunmapf((caddr_t)(a),(size_t)(s)), \
863 				 ((f)->endb = (f)->endr = (f)->endw = (f)->next = \
864 				  (f)->data = NIL(uchar*)) )
865 
866 /* safe closing function */
867 #define CLOSE(f)	{ while(sysclosef(f) < 0 && errno == EINTR) errno = 0; }
868 
869 /* the bottomless bit bucket */
870 #define DEVNULL		"/dev/null"
871 #define SFSETNULL(f)	((f)->extent = (Sfoff_t)(-1), (f)->bits |= SF_NULL)
872 #define SFISNULL(f)	((f)->extent < 0 && ((f)->bits&SF_NULL) )
873 
874 #define SFKILL(f)	((f)->mode = (SF_AVAIL|SF_LOCK) )
875 #define SFKILLED(f)	(((f)->mode&(SF_AVAIL|SF_LOCK)) == (SF_AVAIL|SF_LOCK) )
876 
877 /* exception types */
878 #define SF_EDONE	0	/* stop this operation and return	*/
879 #define SF_EDISC	1	/* discipline says it's ok		*/
880 #define SF_ESTACK	2	/* stack was popped			*/
881 #define SF_ECONT	3	/* can continue normally		*/
882 
883 #define SETLOCAL(f)	((f)->mode |= SF_LOCAL)
884 #define GETLOCAL(f,v)	((v) = ((f)->mode&SF_LOCAL), (f)->mode &= ~SF_LOCAL, (v))
885 #define SFWRALL(f)	((f)->mode |= SF_RV)
886 #define SFISALL(f,v)	((((v) = (f)->mode&SF_RV) ? ((f)->mode &= ~SF_RV) : 0), \
887 			 ((v) || (f)->extent < 0 || \
888 			  ((f)->flags&(SF_SHARE|SF_APPENDWR|SF_WHOLE)) ) )
889 #define SFSK(f,a,o,d)	(SETLOCAL(f),sfsk(f,(Sfoff_t)a,o,d))
890 #define SFRD(f,b,n,d)	(SETLOCAL(f),sfrd(f,(Void_t*)b,n,d))
891 #define SFWR(f,b,n,d)	(SETLOCAL(f),sfwr(f,(Void_t*)b,n,d))
892 #define SFSYNC(f)	(SETLOCAL(f),sfsync(f))
893 #define SFCLOSE(f)	(SETLOCAL(f),sfclose(f))
894 #define SFFLSBUF(f,n)	(SETLOCAL(f),_sfflsbuf(f,n))
895 #define SFFILBUF(f,n)	(SETLOCAL(f),_sffilbuf(f,n))
896 #define SFSETBUF(f,s,n)	(SETLOCAL(f),sfsetbuf(f,s,n))
897 #define SFWRITE(f,s,n)	(SETLOCAL(f),sfwrite(f,s,n))
898 #define SFREAD(f,s,n)	(SETLOCAL(f),sfread(f,s,n))
899 #define SFSEEK(f,p,t)	(SETLOCAL(f),sfseek(f,p,t))
900 #define SFNPUTC(f,c,n)	(SETLOCAL(f),sfnputc(f,c,n))
901 #define SFRAISE(f,e,d)	(SETLOCAL(f),sfraise(f,e,d))
902 
903 /* lock/open a stream */
904 #define SFMODE(f,l)	((f)->mode & ~(SF_RV|SF_RC|((l) ? SF_LOCK : 0)) )
905 #define SFLOCK(f,l)	(void)((f)->mode |= SF_LOCK, (f)->endr = (f)->endw = (f)->data)
906 #define _SFOPENRD(f)	((f)->endr = ((f)->flags&SF_MTSAFE) ? (f)->data : (f)->endb)
907 #define _SFOPENWR(f)	((f)->endw = ((f)->flags&(SF_MTSAFE|SF_LINE)) ? (f)->data : (f)->endb)
908 #define _SFOPEN(f)	((f)->mode == SF_READ  ? _SFOPENRD(f) : \
909 			 (f)->mode == SF_WRITE ? _SFOPENWR(f) : \
910 			 ((f)->endw = (f)->endr = (f)->data) )
911 #define SFOPEN(f,l)	(void)((l) ? 0 : \
912 				((f)->mode &= ~(SF_LOCK|SF_RC|SF_RV), _SFOPEN(f), 0) )
913 
914 /* check to see if the stream can be accessed */
915 #define SFFROZEN(f)	(((f)->mode&(SF_PUSH|SF_LOCK|SF_PEEK)) ? 1 : \
916 			 !((f)->mode&SF_STDIO) ? 0 : \
917 			 _Sfstdsync ? (*_Sfstdsync)(f) : (((f)->mode &= ~SF_STDIO),0) )
918 
919 
920 /* set discipline code */
921 #define SFDISC(f,dc,iof) \
922 	{	Sfdisc_t* d; \
923 		if(!(dc)) \
924 			d = (dc) = (f)->disc; \
925 		else 	d = (f->bits&SF_DCDOWN) ? ((dc) = (dc)->disc) : (dc); \
926 		while(d && !(d->iof))	d = d->disc; \
927 		if(d)	(dc) = d; \
928 	}
929 #define SFDCRD(f,buf,n,dc,rv) \
930 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
931 		rv = (*dc->readf)(f,buf,n,dc); \
932 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
933 	}
934 #define SFDCWR(f,buf,n,dc,rv) \
935 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
936 		rv = (*dc->writef)(f,buf,n,dc); \
937 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
938 	}
939 #define SFDCSK(f,addr,type,dc,rv) \
940 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
941 		rv = (*dc->seekf)(f,addr,type,dc); \
942 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
943 	}
944 
945 /* fast peek of a stream */
946 #define _SFAVAIL(f,s,n)	((n) = (f)->endb - ((s) = (f)->next) )
947 #define SFRPEEK(f,s,n)	(_SFAVAIL(f,s,n) > 0 ? (n) : \
948 				((n) = SFFILBUF(f,-1), (s) = (f)->next, (n)) )
949 #define SFWPEEK(f,s,n)	(_SFAVAIL(f,s,n) > 0 ? (n) : \
950 				((n) = SFFLSBUF(f,-1), (s) = (f)->next, (n)) )
951 
952 /* more than this for a line buffer, we might as well flush */
953 #define HIFORLINE	128
954 
955 /* string stream extent */
956 #define SFSTRSIZE(f)	{ Sfoff_t s = (f)->next - (f)->data; \
957 			  if(s > (f)->here) \
958 			    { (f)->here = s; if(s > (f)->extent) (f)->extent = s; } \
959 			}
960 
961 /* control flags for open() */
962 #ifdef O_CREAT
963 #define _has_oflags	1
964 #else	/* for example, research UNIX */
965 #define _has_oflags	0
966 #define O_CREAT		004
967 #define O_TRUNC		010
968 #define O_APPEND	020
969 #define O_EXCL		040
970 
971 #ifndef O_RDONLY
972 #define	O_RDONLY	000
973 #endif
974 #ifndef O_WRONLY
975 #define O_WRONLY	001
976 #endif
977 #ifndef O_RDWR
978 #define O_RDWR		002
979 #endif
980 #endif /*O_CREAT*/
981 
982 #ifndef O_BINARY
983 #define O_BINARY	000
984 #endif
985 #ifndef O_TEXT
986 #define O_TEXT		000
987 #endif
988 #ifndef O_TEMPORARY
989 #define O_TEMPORARY	000
990 #endif
991 
992 #define	SF_RADIX	64	/* maximum integer conversion base */
993 
994 #if _PACKAGE_ast
995 #define SF_MAXINT	INT_MAX
996 #define SF_MAXLONG	LONG_MAX
997 #else
998 #define SF_MAXINT	((int)(((uint)~0) >> 1))
999 #define SF_MAXLONG	((long)(((ulong)~0L) >> 1))
1000 #endif
1001 
1002 #define SF_MAXCHAR	((uchar)(~0))
1003 
1004 /* floating point to ascii conversion */
1005 #define SF_MAXEXP10	6
1006 #define SF_MAXPOW10	(1 << SF_MAXEXP10)
1007 #if !_ast_fltmax_double
1008 #define SF_FDIGITS	1024		/* max allowed fractional digits */
1009 #define SF_IDIGITS	(8*1024)	/* max number of digits in int part */
1010 #else
1011 #define SF_FDIGITS	256		/* max allowed fractional digits */
1012 #define SF_IDIGITS	1024		/* max number of digits in int part */
1013 #endif
1014 #define SF_MAXDIGITS	(((SF_FDIGITS+SF_IDIGITS)/sizeof(int) + 1)*sizeof(int))
1015 
1016 /* tables for numerical translation */
1017 #define _Sfpos10	(_Sftable.sf_pos10)
1018 #define _Sfneg10	(_Sftable.sf_neg10)
1019 #define _Sfdec		(_Sftable.sf_dec)
1020 #define _Sfdigits	(_Sftable.sf_digits)
1021 #define _Sfcvinitf	(_Sftable.sf_cvinitf)
1022 #define _Sfcvinit	(_Sftable.sf_cvinit)
1023 #define _Sffmtposf	(_Sftable.sf_fmtposf)
1024 #define _Sffmtintf	(_Sftable.sf_fmtintf)
1025 #define _Sfcv36		(_Sftable.sf_cv36)
1026 #define _Sfcv64		(_Sftable.sf_cv64)
1027 #define _Sftype		(_Sftable.sf_type)
1028 #define _Sfieee		(&_Sftable.sf_ieee)
1029 #define _Sffinf		(_Sftable.sf_ieee.fltinf)
1030 #define _Sfdinf		(_Sftable.sf_ieee.dblinf)
1031 #define _Sflinf		(_Sftable.sf_ieee.ldblinf)
1032 #define _Sffnan		(_Sftable.sf_ieee.fltnan)
1033 #define _Sfdnan		(_Sftable.sf_ieee.dblnan)
1034 #define _Sflnan		(_Sftable.sf_ieee.ldblnan)
1035 #define _Sffpow10	(_Sftable.sf_flt_pow10)
1036 #define _Sfdpow10	(_Sftable.sf_dbl_pow10)
1037 #define _Sflpow10	(_Sftable.sf_ldbl_pow10)
1038 typedef struct _sfieee_s	Sfieee_t;
1039 struct _sfieee_s
1040 {	float		fltnan;		/* float NAN			*/
1041 	float		fltinf;		/* float INF			*/
1042 	double		dblnan;		/* double NAN			*/
1043 	double		dblinf;		/* double INF			*/
1044 	Sfdouble_t	ldblnan;	/* Sfdouble_t NAN		*/
1045 	Sfdouble_t	ldblinf;	/* Sfdouble_t INF		*/
1046 };
1047 typedef struct _sftab_
1048 {	Sfdouble_t	sf_pos10[SF_MAXEXP10];	/* positive powers of 10	*/
1049 	Sfdouble_t	sf_neg10[SF_MAXEXP10];	/* negative powers of 10	*/
1050 	uchar		sf_dec[200];		/* ascii reps of values < 100	*/
1051 	char*		sf_digits;		/* digits for general bases	*/
1052 	int		(*sf_cvinitf)();	/* initialization function	*/
1053 	int		sf_cvinit;		/* initialization state		*/
1054 	Fmtpos_t*	(*sf_fmtposf)_ARG_((Sfio_t*,const char*,va_list,Sffmt_t*,int));
1055 	char*		(*sf_fmtintf)_ARG_((const char*,int*));
1056 	float*		sf_flt_pow10;		/* float powers of 10		*/
1057 	double*		sf_dbl_pow10;		/* double powers of 10		*/
1058 	Sfdouble_t*	sf_ldbl_pow10;		/* Sfdouble_t powers of 10	*/
1059 	uchar		sf_cv36[SF_MAXCHAR+1];	/* conversion for base [2-36]	*/
1060 	uchar		sf_cv64[SF_MAXCHAR+1];	/* conversion for base [37-64]	*/
1061 	uchar		sf_type[SF_MAXCHAR+1];	/* conversion formats&types	*/
1062 	Sfieee_t	sf_ieee;		/* IEEE floating point constants*/
1063 } Sftab_t;
1064 
1065 /* thread-safe macro/function to initialize _Sfcv* conversion tables */
1066 #define SFCVINIT()      (_Sfcvinit ? 1 : (_Sfcvinit = (*_Sfcvinitf)()) )
1067 
1068 /* sfucvt() converts decimal integers to ASCII */
1069 #define SFDIGIT(v,scale,digit) \
1070 	{ if(v < 5*scale) \
1071 		if(v < 2*scale) \
1072 			if(v < 1*scale) \
1073 				{ digit = '0'; } \
1074 			else	{ digit = '1'; v -= 1*scale; } \
1075 		else	if(v < 3*scale) \
1076 				{ digit = '2'; v -= 2*scale; } \
1077 			else if(v < 4*scale) \
1078 				{ digit = '3'; v -= 3*scale; } \
1079 			else	{ digit = '4'; v -= 4*scale; } \
1080 	  else	if(v < 7*scale) \
1081 			if(v < 6*scale) \
1082 				{ digit = '5'; v -= 5*scale; } \
1083 			else	{ digit = '6'; v -= 6*scale; } \
1084 		else	if(v < 8*scale) \
1085 				{ digit = '7'; v -= 7*scale; } \
1086 			else if(v < 9*scale) \
1087 				{ digit = '8'; v -= 8*scale; } \
1088 			else	{ digit = '9'; v -= 9*scale; } \
1089 	}
1090 #define sfucvt(v,s,n,list,type,utype) \
1091 	{ while((utype)v >= 10000) \
1092 	  {	n = v; v = (type)(((utype)v)/10000); \
1093 		n = (type)((utype)n - ((utype)v)*10000); \
1094 	  	s -= 4; SFDIGIT(n,1000,s[0]); SFDIGIT(n,100,s[1]); \
1095 			s[2] = *(list = (char*)_Sfdec + (n <<= 1)); s[3] = *(list+1); \
1096 	  } \
1097 	  if(v < 100) \
1098 	  { if(v < 10) \
1099 	    { 	s -= 1; s[0] = (char)('0'+v); \
1100 	    } else \
1101 	    { 	s -= 2; s[0] = *(list = (char*)_Sfdec + (v <<= 1)); s[1] = *(list+1); \
1102 	    } \
1103 	  } else \
1104 	  { if(v < 1000) \
1105 	    { 	s -= 3; SFDIGIT(v,100,s[0]); \
1106 			s[1] = *(list = (char*)_Sfdec + (v <<= 1)); s[2] = *(list+1); \
1107 	    } else \
1108 	    {	s -= 4; SFDIGIT(v,1000,s[0]); SFDIGIT(v,100,s[1]); \
1109 			s[2] = *(list = (char*)_Sfdec + (v <<= 1)); s[3] = *(list+1); \
1110 	    } \
1111 	  } \
1112 	}
1113 
1114 /* handy functions */
1115 #undef min
1116 #undef max
1117 #define min(x,y)	((x) < (y) ? (x) : (y))
1118 #define max(x,y)	((x) > (y) ? (x) : (y))
1119 
1120 /* fast functions for memory copy and memory clear */
1121 #if _PACKAGE_ast
1122 #define memclear(s,n)	memzero(s,n)
1123 #else
1124 #if _lib_bcopy && !_lib_memcpy
1125 #define memcpy(to,fr,n)	bcopy((fr),(to),(n))
1126 #endif
1127 #if _lib_bzero && !_lib_memset
1128 #define memclear(s,n)	bzero((s),(n))
1129 #else
1130 #define memclear(s,n)	memset((s),'\0',(n))
1131 #endif
1132 #endif /*_PACKAGE_ast*/
1133 
1134 /* note that MEMCPY advances the associated pointers */
1135 #define MEMCPY(to,fr,n) \
1136 	switch(n) \
1137 	{ default : memcpy((Void_t*)to,(Void_t*)fr,n); to += n; fr += n; break; \
1138 	  case  7 : *to++ = *fr++; \
1139 	  case  6 : *to++ = *fr++; \
1140 	  case  5 : *to++ = *fr++; \
1141 	  case  4 : *to++ = *fr++; \
1142 	  case  3 : *to++ = *fr++; \
1143 	  case  2 : *to++ = *fr++; \
1144 	  case  1 : *to++ = *fr++; \
1145 	}
1146 #define MEMSET(s,c,n) \
1147 	switch(n) \
1148 	{ default : memset((Void_t*)s,(int)c,n); s += n; break; \
1149 	  case  7 : *s++ = c; \
1150 	  case  6 : *s++ = c; \
1151 	  case  5 : *s++ = c; \
1152 	  case  4 : *s++ = c; \
1153 	  case  3 : *s++ = c; \
1154 	  case  2 : *s++ = c; \
1155 	  case  1 : *s++ = c; \
1156 	}
1157 
1158 _BEGIN_EXTERNS_
1159 
1160 extern Sftab_t		_Sftable;
1161 
1162 extern int		_sfpopen _ARG_((Sfio_t*, int, int, int));
1163 extern int		_sfpclose _ARG_((Sfio_t*));
1164 extern int		_sfexcept _ARG_((Sfio_t*, int, ssize_t, Sfdisc_t*));
1165 extern Sfrsrv_t*	_sfrsrv _ARG_((Sfio_t*, ssize_t));
1166 extern int		_sfsetpool _ARG_((Sfio_t*));
1167 extern char*		_sfcvt _ARG_((Void_t*,char*,size_t,int,int*,int*,int*,int));
1168 extern char**		_sfgetpath _ARG_((char*));
1169 
1170 #if _BLD_sfio && defined(__EXPORT__)
1171 #define extern		__EXPORT__
1172 #endif
1173 #if !_BLD_sfio && defined(__IMPORT__)
1174 #define extern		extern __IMPORT__
1175 #endif
1176 
1177 extern Sfextern_t	_Sfextern;
1178 
1179 extern int		_sfmode _ARG_((Sfio_t*, int, int));
1180 extern int		_sftype _ARG_((const char*, int*, int*));
1181 
1182 #undef	extern
1183 
1184 #ifndef errno
1185 extern int		errno;
1186 #endif
1187 
1188 /* for portable encoding of double values */
1189 #ifndef frexpl
1190 #if _ast_fltmax_double
1191 #define frexpl		frexp
1192 #endif
1193 #if !__STDC__
1194 extern Sfdouble_t	frexpl _ARG_((Sfdouble_t, int*));
1195 #endif
1196 #endif
1197 #ifndef ldexpl
1198 #if _ast_fltmax_double
1199 #define ldexpl		ldexp
1200 #endif
1201 #if !__STDC__
1202 extern Sfdouble_t	ldexpl _ARG_((Sfdouble_t, int));
1203 #endif
1204 #endif
1205 
1206 #if !_PACKAGE_ast
1207 
1208 #if !__STDC__ && !_hdr_stdlib
1209 extern void	abort _ARG_((void));
1210 extern int	atexit _ARG_((void(*)(void)));
1211 extern char*	getenv _ARG_((const char*));
1212 extern void*	malloc _ARG_((size_t));
1213 extern void*	realloc _ARG_((void*, size_t));
1214 extern void	free _ARG_((void*));
1215 extern size_t	strlen _ARG_((const char*));
1216 extern char*	strcpy _ARG_((char*, const char*));
1217 
1218 extern Void_t*	memset _ARG_((void*, int, size_t));
1219 extern Void_t*	memchr _ARG_((const void*, int, size_t));
1220 extern Void_t*	memccpy _ARG_((void*, const void*, int, size_t));
1221 #ifndef memcpy
1222 extern Void_t*	memcpy _ARG_((void*, const void*, size_t));
1223 #endif
1224 #if !defined(strtod)
1225 extern double	strtod _ARG_((const char*, char**));
1226 #endif
1227 #if !defined(remove)
1228 extern int	sysremovef _ARG_((const char*));
1229 #endif
1230 #endif /* !__STDC__ && !_hdr_stdlib */
1231 
1232 #if !_hdr_unistd
1233 #if _proto_open && __cplusplus
1234 extern int	sysopenf _ARG_((const char*, int, ...));
1235 #endif
1236 extern int	sysclosef _ARG_((int));
1237 extern ssize_t	sysreadf _ARG_((int, void*, size_t));
1238 extern ssize_t	syswritef _ARG_((int, const void*, size_t));
1239 extern sfoff_t	syslseekf _ARG_((int, sfoff_t, int));
1240 extern int	sysdupf _ARG_((int));
1241 extern int	syspipef _ARG_((int*));
1242 extern int	sysaccessf _ARG_((const char*, int));
1243 extern int	sysremovef _ARG_((const char*));
1244 extern int	sysfstatf _ARG_((int, sfstat_t*));
1245 extern int	sysstatf _ARG_((const char*, sfstat_t*));
1246 
1247 extern int	isatty _ARG_((int));
1248 
1249 extern int	wait _ARG_((int*));
1250 extern uint	sleep _ARG_((uint));
1251 extern int	execl _ARG_((const char*, const char*,...));
1252 extern int	execv _ARG_((const char*, char**));
1253 #if !defined(fork)
1254 extern int	fork _ARG_((void));
1255 #endif
1256 #if _lib_unlink
1257 extern int	unlink _ARG_((const char*));
1258 #endif
1259 
1260 #endif /*_hdr_unistd*/
1261 
1262 #if _lib_bcopy && !_proto_bcopy
1263 extern void	bcopy _ARG_((const void*, void*, size_t));
1264 #endif
1265 #if _lib_bzero && !_proto_bzero
1266 extern void	bzero _ARG_((void*, size_t));
1267 #endif
1268 
1269 extern time_t	time _ARG_((time_t*));
1270 extern int	waitpid _ARG_((int,int*,int));
1271 extern void	_exit _ARG_((int));
1272 typedef int(*	Onexit_f)_ARG_((void));
1273 extern Onexit_f	onexit _ARG_((Onexit_f));
1274 
1275 #if _lib_vfork && !_hdr_vfork && !_sys_vfork
1276 extern pid_t	vfork _ARG_((void));
1277 #endif /*_lib_vfork*/
1278 
1279 #if _lib_poll
1280 #if _lib_poll_fd_1
1281 extern int	poll _ARG_((struct pollfd*, ulong, int));
1282 #else
1283 extern int	poll _ARG_((ulong, struct pollfd*, int));
1284 #endif
1285 #endif /*_lib_poll*/
1286 
1287 #endif /* _PACKAGE_ast */
1288 
1289 _END_EXTERNS_
1290 
1291 #endif /*_SFHDR_H*/
1292