xref: /titanic_50/usr/src/lib/libast/common/sfio/sfhdr.h (revision 8ef10d209f1443865a8ce5b1eb59838b64a64e67)
1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                      by AT&T Knowledge Ventures                      *
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 #if vt_threaded
365 
366 /* initialization */
367 #define SFONCE()	(_Sfdone ? 0 : vtonce(_Sfonce,_Sfoncef))
368 
369 /* to lock/unlock a stream on entering and returning from some function */
370 #define SFMTXLOCK(f)	 (((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_LOCK) : 0)
371 #define SFMTXUNLOCK(f)	 (((f)->flags&SF_MTSAFE) ? sfmutex(f,SFMTX_UNLOCK) : 0)
372 #define SFMTXSTART(f,v)  { if(!f || SFMTXLOCK(f) != 0) return(v); }
373 #define SFMTXRETURN(f,v) { SFMTXUNLOCK(f); return(v); }
374 
375 /* start and end critical region for a pool */
376 #define POOLMTXLOCK(p)		( vtmtxlock(&(p)->mutex) )
377 #define POOLMTXUNLOCK(p)	( vtmtxunlock(&(p)->mutex) )
378 #define POOLMTXSTART(p)		{ POOLMTXLOCK(p); }
379 #define POOLMTXRETURN(p,v)	{ POOLMTXUNLOCK(p); return(v); }
380 
381 #else /*!vt_threaded*/
382 
383 #undef SF_MTSAFE /* no need to worry about thread-safety */
384 #define SF_MTSAFE		0
385 
386 #define SFONCE()		/*(0)*/
387 
388 #define SFMTXLOCK(f)		/*(0)*/
389 #define SFMTXUNLOCK(f)		/*(0)*/
390 #define SFMTXSTART(f,v)		{ if(!f) return(v); }
391 #define SFMTXRETURN(f,v)	{ return(v); }
392 
393 #define POOLMTXLOCK(p)
394 #define POOLMTXUNLOCK(p)
395 #define POOLMTXSTART(p)
396 #define POOLMTXRETURN(p,v)	{ return(v); }
397 
398 #endif /*vt_threaded*/
399 
400 
401 /* functions for polling readiness of streams */
402 #if _lib_select
403 #undef _lib_poll
404 #else
405 #if _lib_poll_fd_1 || _lib_poll_fd_2
406 #define _lib_poll	1
407 #endif
408 #endif /*_lib_select_*/
409 
410 #if _lib_poll
411 #include	<poll.h>
412 
413 #if _lib_poll_fd_1
414 #define SFPOLL(pfd,n,tm)	poll((pfd),(ulong)(n),(tm))
415 #else
416 #define SFPOLL(pfd,n,tm)	poll((ulong)(n),(pfd),(tm))
417 #endif
418 #endif /*_lib_poll*/
419 
420 #if _stream_peek
421 #include	<stropts.h>
422 #endif
423 
424 #if _socket_peek
425 #include	<sys/socket.h>
426 #endif
427 
428 /* to test for executable access mode of a file */
429 #ifndef X_OK
430 #define X_OK	01
431 #endif
432 
433 /* alternative process forking */
434 #if _lib_vfork && !defined(fork) && !defined(sparc) && !defined(__sparc)
435 #if _hdr_vfork
436 #include	<vfork.h>
437 #endif
438 #if _sys_vfork
439 #include	<sys/vfork.h>
440 #endif
441 #define fork	vfork
442 #endif
443 
444 /* to get rid of pesky compiler warnings */
445 #if __STD_C
446 #define NOTUSED(x)	(void)(x)
447 #else
448 #define NOTUSED(x)	(&x,1)
449 #endif
450 
451 /* Private flags in the "bits" field */
452 #define SF_MMAP		00000001	/* in memory mapping mode		*/
453 #define SF_BOTH		00000002	/* both read/write			*/
454 #define SF_HOLE		00000004	/* a hole of zero's was created		*/
455 #define SF_NULL		00000010	/* stream is /dev/null			*/
456 #define SF_SEQUENTIAL	00000020	/* sequential access			*/
457 #define SF_JUSTSEEK	00000040	/* just did a sfseek			*/
458 #define SF_PRIVATE	00000100	/* private stream to Sfio, no mutex	*/
459 #define SF_ENDING	00000200	/* no re-io on interrupts at closing	*/
460 #define SF_WIDE		00000400	/* in wide mode - stdio only		*/
461 
462 /* "bits" flags that must be cleared in sfclrlock */
463 #define SF_TMPBITS	00170000
464 #define SF_DCDOWN	00010000	/* recurse down the discipline stack	*/
465 
466 #define SF_WCFORMAT	00020000	/* wchar_t formatting - stdio only	*/
467 #if _has_multibyte
468 #define SFWCSET(f)	((f)->bits |= SF_WCFORMAT)
469 #define SFWCGET(f,v)	(((v) = (f)->bits & SF_WCFORMAT), ((f)->bits &= ~SF_WCFORMAT) )
470 #else
471 #define SFWCSET(f)
472 #define SFWCGET(f,v)
473 #endif
474 
475 #define SF_MVSIZE	00040000	/* f->size was reset in sfmove()	*/
476 #define SFMVSET(f)	(((f)->size *= SF_NMAP), ((f)->bits |= SF_MVSIZE) )
477 #define SFMVUNSET(f)	(!((f)->bits&SF_MVSIZE) ? 0 : \
478 				(((f)->bits &= ~SF_MVSIZE), ((f)->size /= SF_NMAP)) )
479 
480 #define SFCLRBITS(f)	(SFMVUNSET(f), ((f)->bits &= ~SF_TMPBITS) )
481 
482 
483 /* bits for the mode field, SF_INIT defined in sfio_t.h */
484 #define SF_RC		00000010	/* peeking for a record			*/
485 #define SF_RV		00000020	/* reserve without read	or most write	*/
486 #define SF_LOCK		00000040	/* stream is locked for io op		*/
487 #define SF_PUSH		00000100	/* stream has been pushed		*/
488 #define SF_POOL		00000200	/* stream is in a pool but not current	*/
489 #define SF_PEEK		00000400	/* there is a pending peek		*/
490 #define SF_PKRD		00001000	/* did a peek read			*/
491 #define SF_GETR		00002000	/* did a getr on this stream		*/
492 #define SF_SYNCED	00004000	/* stream was synced			*/
493 #define SF_STDIO	00010000	/* given up the buffer to stdio		*/
494 #define SF_AVAIL	00020000	/* was closed, available for reuse	*/
495 #define SF_LOCAL	00100000	/* sentinel for a local call		*/
496 
497 #ifdef DEBUG
498 #define ASSERT(p)	((p) ? 0 : (abort(),0) )
499 #else
500 #define ASSERT(p)
501 #endif
502 
503 /* short-hands */
504 #define NIL(t)		((t)0)
505 #define reg		register
506 #ifndef uchar
507 #define uchar		unsigned char
508 #endif
509 #ifndef ulong
510 #define ulong		unsigned long
511 #endif
512 #ifndef uint
513 #define uint		unsigned int
514 #endif
515 #ifndef ushort
516 #define ushort		unsigned short
517 #endif
518 
519 #define SECOND		1000	/* millisecond units */
520 
521 /* macros do determine stream types from sfstat_t data */
522 #ifndef S_IFMT
523 #define S_IFMT	0
524 #endif
525 #ifndef S_IFDIR
526 #define S_IFDIR	0
527 #endif
528 #ifndef S_IFREG
529 #define S_IFREG	0
530 #endif
531 #ifndef S_IFCHR
532 #define S_IFCHR	0
533 #endif
534 #ifndef S_IFIFO
535 #define S_IFIFO	0
536 #endif
537 
538 #ifndef S_ISDIR
539 #define S_ISDIR(m)	(((m)&S_IFMT) == S_IFDIR)
540 #endif
541 #ifndef S_ISREG
542 #define S_ISREG(m)	(((m)&S_IFMT) == S_IFREG)
543 #endif
544 #ifndef S_ISCHR
545 #define S_ISCHR(m)	(((m)&S_IFMT) == S_IFCHR)
546 #endif
547 
548 #ifndef S_ISFIFO
549 #	ifdef S_IFIFO
550 #		define S_ISFIFO(m)	(((m)&S_IFIFO) == S_IFIFO)
551 #	else
552 #		define S_ISFIFO(m)	(0)
553 #	endif
554 #endif
555 
556 #ifdef S_IRUSR
557 #define SF_CREATMODE	(S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
558 #else
559 #define SF_CREATMODE	0666
560 #endif
561 
562 /* set close-on-exec */
563 #ifdef F_SETFD
564 #	ifndef FD_CLOEXEC
565 #		define FD_CLOEXEC	1
566 #	endif /*FD_CLOEXEC*/
567 #	define SETCLOEXEC(fd)		((void)fcntl((fd),F_SETFD,FD_CLOEXEC))
568 #else
569 #	ifdef FIOCLEX
570 #		define SETCLOEXEC(fd)	((void)ioctl((fd),FIOCLEX,0))
571 #	else
572 #		define SETCLOEXEC(fd)
573 #	endif /*FIOCLEX*/
574 #endif /*F_SETFD*/
575 
576 /* a couple of error number that we use, default values are like Linux */
577 #ifndef EINTR
578 #define EINTR	4
579 #endif
580 #ifndef EBADF
581 #define EBADF	9
582 #endif
583 #ifndef EAGAIN
584 #define EAGAIN	11
585 #endif
586 #ifndef ENOMEM
587 #define ENOMEM	12
588 #endif
589 #ifndef EINVAL
590 #define EINVAL	22
591 #endif
592 #ifndef ESPIPE
593 #define ESPIPE	29
594 #endif
595 
596 /* function to get the decimal point for local environment */
597 #if !defined(SFSETLOCALE) && _PACKAGE_ast
598 #include "lclib.h"
599 #define SFSETLOCALE(dp,tp) \
600 	do if (*(dp) == 0) { \
601 		Lc_numeric_t*	lv = (Lc_numeric_t*)LCINFO(AST_LC_NUMERIC)->data; \
602 		*(dp) = lv->decimal; \
603 		if (tp) *(tp) = lv->thousand; \
604 	} while (0)
605 #endif /*!defined(SFSETLOCALE) && _PACKAGE_ast*/
606 
607 #if !defined(SFSETLOCALE) && _lib_locale
608 #include	<locale.h>
609 #define SFSETLOCALE(decimal,thousand) \
610 	do { struct lconv*	lv; \
611 	  if(*(decimal) == 0) \
612 	  { *(decimal) = '.'; \
613 	    if (thousand) *(thousand) = -1; \
614 	    if((lv = localeconv())) \
615 	    { if(lv->decimal_point && *lv->decimal_point) \
616 	    	*(decimal) = *(unsigned char*)lv->decimal_point; \
617 	      if(thousand && lv->thousands_sep && *lv->thousands_sep) \
618 	    	*(thousand) = *(unsigned char*)lv->thousands_sep; \
619 	    } \
620 	  } \
621 	} while (0)
622 #endif /*!defined(SFSETLOCALE) && _lib_locale*/
623 
624 #if !defined(SFSETLOCALE)
625 #define SFSETLOCALE(decimal,thousand)	(*(decimal)='.')
626 #endif
627 
628 /* stream pool structure. */
629 typedef struct _sfpool_s	Sfpool_t;
630 struct _sfpool_s
631 {	Sfpool_t*	next;
632 	int		mode;		/* type of pool			*/
633 	int		s_sf;		/* size of pool array		*/
634 	int		n_sf;		/* number currently in pool	*/
635 	Sfio_t**	sf;		/* array of streams		*/
636 	Sfio_t*		array[3];	/* start with 3			*/
637 	Vtmutex_t	mutex;		/* mutex lock object		*/
638 };
639 
640 /* reserve buffer structure */
641 typedef struct _sfrsrv_s	Sfrsrv_t;
642 struct _sfrsrv_s
643 {	ssize_t		slen;		/* last string length		*/
644 	ssize_t		size;		/* buffer size			*/
645 	uchar		data[1];	/* data buffer			*/
646 };
647 
648 /* co-process structure */
649 typedef struct _sfproc_s	Sfproc_t;
650 struct _sfproc_s
651 {	int		pid;	/* process id			*/
652 	uchar*		rdata;	/* read data being cached	*/
653 	int		ndata;	/* size of cached data		*/
654 	int		size;	/* buffer size			*/
655 	int		file;	/* saved file descriptor	*/
656 	int		sigp;	/* sigpipe protection needed	*/
657 };
658 
659 /* extensions to sfvprintf/sfvscanf */
660 #define FP_SET(fp,fn)	(fp < 0 ? (fn += 1) : (fn = fp) )
661 #define FP_WIDTH	0
662 #define FP_PRECIS	1
663 #define FP_BASE		2
664 #define FP_STR		3
665 #define FP_SIZE		4
666 #define FP_INDEX	5	/* index size	*/
667 
668 typedef struct _fmt_s		Fmt_t;
669 typedef struct _fmtpos_s	Fmtpos_t;
670 typedef union
671 {	int		i, *ip;
672 	long		l, *lp;
673 	short		h, *hp;
674 	uint		ui;
675 	ulong		ul;
676 	ushort		uh;
677 	Sflong_t	ll, *llp;
678 	Sfulong_t	lu;
679 	Sfdouble_t	ld;
680 	double		d;
681 	float		f;
682 #if _has_multibyte
683 	wchar_t		wc;
684 	wchar_t		*ws, **wsp;
685 #endif
686 	char		c, *s, **sp;
687 	uchar		uc, *us, **usp;
688 	Void_t		*vp;
689 	Sffmt_t		*ft;
690 } Argv_t;
691 
692 struct _fmt_s
693 {	char*		form;		/* format string		*/
694 	va_list		args;		/* corresponding arglist	*/
695 	SFMBDCL(mbs)			/* multibyte parsing state	*/
696 
697 	char*		oform;		/* original format string	*/
698 	va_list		oargs;		/* original arg list		*/
699 	int		argn;		/* number of args already used	*/
700 	Fmtpos_t*	fp;		/* position list		*/
701 
702 	Sffmt_t*	ft;		/* formatting environment	*/
703 	Sffmtevent_f	eventf;		/* event function		*/
704 	Fmt_t*		next;		/* stack frame pointer		*/
705 };
706 
707 struct _fmtpos_s
708 {	Sffmt_t	ft;			/* environment			*/
709 	Argv_t	argv;			/* argument value		*/
710 	int	fmt;			/* original format		*/
711 	int	need[FP_INDEX];		/* positions depending on	*/
712 };
713 
714 #define LEFTP		'('
715 #define RIGHTP		')'
716 #define QUOTE		'\''
717 
718 #ifndef CHAR_BIT
719 #define CHAR_BIT	8
720 #endif
721 
722 #define FMTSET(ft, frm,ags, fv, sz, flgs, wid,pr,bs, ts,ns) \
723 	((ft->form = (char*)frm), va_copy(ft->args,ags), \
724 	 (ft->fmt = fv), (ft->size = sz), \
725 	 (ft->flags = (flgs&SFFMT_SET)), \
726 	 (ft->width = wid), (ft->precis = pr), (ft->base = bs), \
727 	 (ft->t_str = ts), (ft->n_str = ns) )
728 #define FMTGET(ft, frm,ags, fv, sz, flgs, wid,pr,bs) \
729 	((frm = ft->form), va_copy(ags,ft->args), \
730 	 (fv = ft->fmt), (sz = ft->size), \
731 	 (flgs = (flgs&~(SFFMT_SET))|(ft->flags&SFFMT_SET)), \
732 	 (wid = ft->width), (pr = ft->precis), (bs = ft->base) )
733 
734 /* format flags&types, must coexist with those in sfio.h */
735 #define SFFMT_FORBIDDEN 000077777777	/* for sfio.h only		*/
736 #define SFFMT_EFORMAT	001000000000	/* sfcvt converting %e		*/
737 #define SFFMT_MINUS	002000000000	/* minus sign			*/
738 #define SFFMT_AFORMAT	004000000000	/* sfcvt converting %a		*/
739 #define SFFMT_UPPER	010000000000	/* sfcvt converting upper	*/
740 
741 #define SFFMT_TYPES	(SFFMT_SHORT|SFFMT_SSHORT | SFFMT_LONG|SFFMT_LLONG|\
742 			 SFFMT_LDOUBLE | SFFMT_IFLAG|SFFMT_JFLAG| \
743 			 SFFMT_TFLAG | SFFMT_ZFLAG )
744 
745 /* type of elements to be converted */
746 #define SFFMT_INT	001		/* %d,%i 		*/
747 #define SFFMT_UINT	002		/* %u,o,x etc.		*/
748 #define SFFMT_FLOAT	004		/* %f,e,g etc.		*/
749 #define SFFMT_CHAR	010		/* %c,C			*/
750 #define SFFMT_POINTER	020		/* %p,n,s,S		*/
751 #define SFFMT_CLASS	040		/* %[			*/
752 
753 /* local variables used across sf-functions */
754 #define _Sfpage		(_Sfextern.sf_page)
755 #define _Sfpool		(_Sfextern.sf_pool)
756 #define _Sfpmove	(_Sfextern.sf_pmove)
757 #define _Sfstack	(_Sfextern.sf_stack)
758 #define _Sfnotify	(_Sfextern.sf_notify)
759 #define _Sfstdsync	(_Sfextern.sf_stdsync)
760 #define _Sfudisc	(&(_Sfextern.sf_udisc))
761 #define _Sfcleanup	(_Sfextern.sf_cleanup)
762 #define _Sfexiting	(_Sfextern.sf_exiting)
763 #define _Sfdone		(_Sfextern.sf_done)
764 #define _Sfonce		(_Sfextern.sf_once)
765 #define _Sfoncef	(_Sfextern.sf_oncef)
766 #define _Sfmutex	(_Sfextern.sf_mutex)
767 typedef struct _sfextern_s
768 {	ssize_t			sf_page;
769 	struct _sfpool_s	sf_pool;
770 	int			(*sf_pmove)_ARG_((Sfio_t*, int));
771 	Sfio_t*			(*sf_stack)_ARG_((Sfio_t*, Sfio_t*));
772 	void			(*sf_notify)_ARG_((Sfio_t*, int, int));
773 	int			(*sf_stdsync)_ARG_((Sfio_t*));
774 	struct _sfdisc_s	sf_udisc;
775 	void			(*sf_cleanup)_ARG_((void));
776 	int			sf_exiting;
777 	int			sf_done;
778 	Vtonce_t*		sf_once;
779 	void			(*sf_oncef)_ARG_((void));
780 	Vtmutex_t*		sf_mutex;
781 } Sfextern_t;
782 
783 /* get the real value of a byte in a coded long or ulong */
784 #define SFUVALUE(v)	(((ulong)(v))&(SF_MORE-1))
785 #define SFSVALUE(v)	((( long)(v))&(SF_SIGN-1))
786 #define SFBVALUE(v)	(((ulong)(v))&(SF_BYTE-1))
787 
788 /* pick this many bits in each iteration of double encoding */
789 #define SF_PRECIS	7
790 
791 /* grain size for buffer increment */
792 #define SF_GRAIN	1024
793 #define SF_PAGE		((ssize_t)(SF_GRAIN*sizeof(int)*2))
794 
795 /* when the buffer is empty, certain io requests may be better done directly
796    on the given application buffers. The below condition determines when.
797 */
798 #define SFDIRECT(f,n)	(((ssize_t)(n) >= (f)->size) || \
799 			 ((n) >= SF_GRAIN && (ssize_t)(n) >= (f)->size/16 ) )
800 
801 /* number of pages to memory map at a time */
802 #define SF_NMAP		4
803 
804 #ifndef MAP_VARIABLE
805 #define MAP_VARIABLE	0
806 #endif
807 #ifndef _mmap_fixed
808 #define _mmap_fixed	0
809 #endif
810 
811 /* set/unset sequential states for mmap */
812 #if _lib_madvise && defined(MADV_SEQUENTIAL) && defined(MADV_NORMAL)
813 #define SFMMSEQON(f,a,s) \
814 		do { int oerrno = errno; \
815 		     (void)madvise((caddr_t)(a),(size_t)(s),MADV_SEQUENTIAL); \
816 		     errno = oerrno; \
817 		} while(0)
818 #define SFMMSEQOFF(f,a,s) \
819 		do { int oerrno = errno; \
820 		     (void)madvise((caddr_t)(a),(size_t)(s),MADV_NORMAL); \
821 		     errno = oerrno; \
822 		} while(0)
823 #else
824 #define SFMMSEQON(f,a,s)
825 #define SFMMSEQOFF(f,a,s)
826 #endif
827 
828 #define SFMUNMAP(f,a,s)		(sysmunmapf((caddr_t)(a),(size_t)(s)), \
829 				 ((f)->endb = (f)->endr = (f)->endw = (f)->next = \
830 				  (f)->data = NIL(uchar*)) )
831 
832 /* safe closing function */
833 #define CLOSE(f)	{ while(sysclosef(f) < 0 && errno == EINTR) errno = 0; }
834 
835 /* the bottomless bit bucket */
836 #define DEVNULL		"/dev/null"
837 #define SFSETNULL(f)	((f)->extent = (Sfoff_t)(-1), (f)->bits |= SF_NULL)
838 #define SFISNULL(f)	((f)->extent < 0 && ((f)->bits&SF_NULL) )
839 
840 #define SFKILL(f)	((f)->mode = (SF_AVAIL|SF_LOCK) )
841 #define SFKILLED(f)	(((f)->mode&(SF_AVAIL|SF_LOCK)) == (SF_AVAIL|SF_LOCK) )
842 
843 /* exception types */
844 #define SF_EDONE	0	/* stop this operation and return	*/
845 #define SF_EDISC	1	/* discipline says it's ok		*/
846 #define SF_ESTACK	2	/* stack was popped			*/
847 #define SF_ECONT	3	/* can continue normally		*/
848 
849 #define SETLOCAL(f)	((f)->mode |= SF_LOCAL)
850 #define GETLOCAL(f,v)	((v) = ((f)->mode&SF_LOCAL), (f)->mode &= ~SF_LOCAL, (v))
851 #define SFWRALL(f)	((f)->mode |= SF_RV)
852 #define SFISALL(f,v)	((((v) = (f)->mode&SF_RV) ? ((f)->mode &= ~SF_RV) : 0), \
853 			 ((v) || (f)->extent < 0 || \
854 			  ((f)->flags&(SF_SHARE|SF_APPENDWR|SF_WHOLE)) ) )
855 #define SFSK(f,a,o,d)	(SETLOCAL(f),sfsk(f,(Sfoff_t)a,o,d))
856 #define SFRD(f,b,n,d)	(SETLOCAL(f),sfrd(f,(Void_t*)b,n,d))
857 #define SFWR(f,b,n,d)	(SETLOCAL(f),sfwr(f,(Void_t*)b,n,d))
858 #define SFSYNC(f)	(SETLOCAL(f),sfsync(f))
859 #define SFCLOSE(f)	(SETLOCAL(f),sfclose(f))
860 #define SFFLSBUF(f,n)	(SETLOCAL(f),_sfflsbuf(f,n))
861 #define SFFILBUF(f,n)	(SETLOCAL(f),_sffilbuf(f,n))
862 #define SFSETBUF(f,s,n)	(SETLOCAL(f),sfsetbuf(f,s,n))
863 #define SFWRITE(f,s,n)	(SETLOCAL(f),sfwrite(f,s,n))
864 #define SFREAD(f,s,n)	(SETLOCAL(f),sfread(f,s,n))
865 #define SFSEEK(f,p,t)	(SETLOCAL(f),sfseek(f,p,t))
866 #define SFNPUTC(f,c,n)	(SETLOCAL(f),sfnputc(f,c,n))
867 #define SFRAISE(f,e,d)	(SETLOCAL(f),sfraise(f,e,d))
868 
869 /* lock/open a stream */
870 #define SFMODE(f,l)	((f)->mode & ~(SF_RV|SF_RC|((l) ? SF_LOCK : 0)) )
871 #define SFLOCK(f,l)	(void)((f)->mode |= SF_LOCK, (f)->endr = (f)->endw = (f)->data)
872 #define _SFOPENRD(f)	((f)->endr = ((f)->flags&SF_MTSAFE) ? (f)->data : (f)->endb)
873 #define _SFOPENWR(f)	((f)->endw = ((f)->flags&(SF_MTSAFE|SF_LINE)) ? (f)->data : (f)->endb)
874 #define _SFOPEN(f)	((f)->mode == SF_READ  ? _SFOPENRD(f) : \
875 			 (f)->mode == SF_WRITE ? _SFOPENWR(f) : \
876 			 ((f)->endw = (f)->endr = (f)->data) )
877 #define SFOPEN(f,l)	(void)((l) ? 0 : \
878 				((f)->mode &= ~(SF_LOCK|SF_RC|SF_RV), _SFOPEN(f), 0) )
879 
880 /* check to see if the stream can be accessed */
881 #define SFFROZEN(f)	(((f)->mode&(SF_PUSH|SF_LOCK|SF_PEEK)) ? 1 : \
882 			 !((f)->mode&SF_STDIO) ? 0 : \
883 			 _Sfstdsync ? (*_Sfstdsync)(f) : (((f)->mode &= ~SF_STDIO),0) )
884 
885 
886 /* set discipline code */
887 #define SFDISC(f,dc,iof) \
888 	{	Sfdisc_t* d; \
889 		if(!(dc)) \
890 			d = (dc) = (f)->disc; \
891 		else 	d = (f->bits&SF_DCDOWN) ? ((dc) = (dc)->disc) : (dc); \
892 		while(d && !(d->iof))	d = d->disc; \
893 		if(d)	(dc) = d; \
894 	}
895 #define SFDCRD(f,buf,n,dc,rv) \
896 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
897 		rv = (*dc->readf)(f,buf,n,dc); \
898 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
899 	}
900 #define SFDCWR(f,buf,n,dc,rv) \
901 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
902 		rv = (*dc->writef)(f,buf,n,dc); \
903 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
904 	}
905 #define SFDCSK(f,addr,type,dc,rv) \
906 	{	int		dcdown = f->bits&SF_DCDOWN; f->bits |= SF_DCDOWN; \
907 		rv = (*dc->seekf)(f,addr,type,dc); \
908 		if(!dcdown)	f->bits &= ~SF_DCDOWN; \
909 	}
910 
911 /* fast peek of a stream */
912 #define _SFAVAIL(f,s,n)	((n) = (f)->endb - ((s) = (f)->next) )
913 #define SFRPEEK(f,s,n)	(_SFAVAIL(f,s,n) > 0 ? (n) : \
914 				((n) = SFFILBUF(f,-1), (s) = (f)->next, (n)) )
915 #define SFWPEEK(f,s,n)	(_SFAVAIL(f,s,n) > 0 ? (n) : \
916 				((n) = SFFLSBUF(f,-1), (s) = (f)->next, (n)) )
917 
918 /* more than this for a line buffer, we might as well flush */
919 #define HIFORLINE	128
920 
921 /* string stream extent */
922 #define SFSTRSIZE(f)	{ Sfoff_t s = (f)->next - (f)->data; \
923 			  if(s > (f)->here) \
924 			    { (f)->here = s; if(s > (f)->extent) (f)->extent = s; } \
925 			}
926 
927 /* control flags for open() */
928 #ifdef O_CREAT
929 #define _has_oflags	1
930 #else	/* for example, research UNIX */
931 #define _has_oflags	0
932 #define O_CREAT		004
933 #define O_TRUNC		010
934 #define O_APPEND	020
935 #define O_EXCL		040
936 
937 #ifndef O_RDONLY
938 #define	O_RDONLY	000
939 #endif
940 #ifndef O_WRONLY
941 #define O_WRONLY	001
942 #endif
943 #ifndef O_RDWR
944 #define O_RDWR		002
945 #endif
946 #endif /*O_CREAT*/
947 
948 #ifndef O_BINARY
949 #define O_BINARY	000
950 #endif
951 #ifndef O_TEXT
952 #define O_TEXT		000
953 #endif
954 #ifndef O_TEMPORARY
955 #define O_TEMPORARY	000
956 #endif
957 
958 #define	SF_RADIX	64	/* maximum integer conversion base */
959 
960 #if _PACKAGE_ast
961 #define SF_MAXINT	INT_MAX
962 #define SF_MAXLONG	LONG_MAX
963 #else
964 #define SF_MAXINT	((int)(((uint)~0) >> 1))
965 #define SF_MAXLONG	((long)(((ulong)~0L) >> 1))
966 #endif
967 
968 #define SF_MAXCHAR	((uchar)(~0))
969 
970 /* floating point to ascii conversion */
971 #define SF_MAXEXP10	6
972 #define SF_MAXPOW10	(1 << SF_MAXEXP10)
973 #if !_ast_fltmax_double
974 #define SF_FDIGITS	1024		/* max allowed fractional digits */
975 #define SF_IDIGITS	(8*1024)	/* max number of digits in int part */
976 #else
977 #define SF_FDIGITS	256		/* max allowed fractional digits */
978 #define SF_IDIGITS	1024		/* max number of digits in int part */
979 #endif
980 #define SF_MAXDIGITS	(((SF_FDIGITS+SF_IDIGITS)/sizeof(int) + 1)*sizeof(int))
981 
982 /* tables for numerical translation */
983 #define _Sfpos10	(_Sftable.sf_pos10)
984 #define _Sfneg10	(_Sftable.sf_neg10)
985 #define _Sfdec		(_Sftable.sf_dec)
986 #define _Sfdigits	(_Sftable.sf_digits)
987 #define _Sfcvinitf	(_Sftable.sf_cvinitf)
988 #define _Sfcvinit	(_Sftable.sf_cvinit)
989 #define _Sffmtposf	(_Sftable.sf_fmtposf)
990 #define _Sffmtintf	(_Sftable.sf_fmtintf)
991 #define _Sfcv36		(_Sftable.sf_cv36)
992 #define _Sfcv64		(_Sftable.sf_cv64)
993 #define _Sftype		(_Sftable.sf_type)
994 #define _Sfieee		(&_Sftable.sf_ieee)
995 #define _Sffinf		(_Sftable.sf_ieee.fltinf)
996 #define _Sfdinf		(_Sftable.sf_ieee.dblinf)
997 #define _Sflinf		(_Sftable.sf_ieee.ldblinf)
998 #define _Sffnan		(_Sftable.sf_ieee.fltnan)
999 #define _Sfdnan		(_Sftable.sf_ieee.dblnan)
1000 #define _Sflnan		(_Sftable.sf_ieee.ldblnan)
1001 #define _Sffpow10	(_Sftable.sf_flt_pow10)
1002 #define _Sfdpow10	(_Sftable.sf_dbl_pow10)
1003 #define _Sflpow10	(_Sftable.sf_ldbl_pow10)
1004 typedef struct _sfieee_s	Sfieee_t;
1005 struct _sfieee_s
1006 {	float		fltnan;		/* float NAN			*/
1007 	float		fltinf;		/* float INF			*/
1008 	double		dblnan;		/* double NAN			*/
1009 	double		dblinf;		/* double INF			*/
1010 	Sfdouble_t	ldblnan;	/* Sfdouble_t NAN		*/
1011 	Sfdouble_t	ldblinf;	/* Sfdouble_t INF		*/
1012 };
1013 typedef struct _sftab_
1014 {	Sfdouble_t	sf_pos10[SF_MAXEXP10];	/* positive powers of 10	*/
1015 	Sfdouble_t	sf_neg10[SF_MAXEXP10];	/* negative powers of 10	*/
1016 	uchar		sf_dec[200];		/* ascii reps of values < 100	*/
1017 	char*		sf_digits;		/* digits for general bases	*/
1018 	int		(*sf_cvinitf)();	/* initialization function	*/
1019 	int		sf_cvinit;		/* initialization state		*/
1020 	Fmtpos_t*	(*sf_fmtposf)_ARG_((Sfio_t*,const char*,va_list,Sffmt_t*,int));
1021 	char*		(*sf_fmtintf)_ARG_((const char*,int*));
1022 	float*		sf_flt_pow10;		/* float powers of 10		*/
1023 	double*		sf_dbl_pow10;		/* double powers of 10		*/
1024 	Sfdouble_t*	sf_ldbl_pow10;		/* Sfdouble_t powers of 10	*/
1025 	uchar		sf_cv36[SF_MAXCHAR+1];	/* conversion for base [2-36]	*/
1026 	uchar		sf_cv64[SF_MAXCHAR+1];	/* conversion for base [37-64]	*/
1027 	uchar		sf_type[SF_MAXCHAR+1];	/* conversion formats&types	*/
1028 	Sfieee_t	sf_ieee;		/* IEEE floating point constants*/
1029 } Sftab_t;
1030 
1031 /* thread-safe macro/function to initialize _Sfcv* conversion tables */
1032 #define SFCVINIT()      (_Sfcvinit ? 1 : (_Sfcvinit = (*_Sfcvinitf)()) )
1033 
1034 /* sfucvt() converts decimal integers to ASCII */
1035 #define SFDIGIT(v,scale,digit) \
1036 	{ if(v < 5*scale) \
1037 		if(v < 2*scale) \
1038 			if(v < 1*scale) \
1039 				{ digit = '0'; } \
1040 			else	{ digit = '1'; v -= 1*scale; } \
1041 		else	if(v < 3*scale) \
1042 				{ digit = '2'; v -= 2*scale; } \
1043 			else if(v < 4*scale) \
1044 				{ digit = '3'; v -= 3*scale; } \
1045 			else	{ digit = '4'; v -= 4*scale; } \
1046 	  else	if(v < 7*scale) \
1047 			if(v < 6*scale) \
1048 				{ digit = '5'; v -= 5*scale; } \
1049 			else	{ digit = '6'; v -= 6*scale; } \
1050 		else	if(v < 8*scale) \
1051 				{ digit = '7'; v -= 7*scale; } \
1052 			else if(v < 9*scale) \
1053 				{ digit = '8'; v -= 8*scale; } \
1054 			else	{ digit = '9'; v -= 9*scale; } \
1055 	}
1056 #define sfucvt(v,s,n,list,type,utype) \
1057 	{ while((utype)v >= 10000) \
1058 	  {	n = v; v = (type)(((utype)v)/10000); \
1059 		n = (type)((utype)n - ((utype)v)*10000); \
1060 	  	s -= 4; SFDIGIT(n,1000,s[0]); SFDIGIT(n,100,s[1]); \
1061 			s[2] = *(list = (char*)_Sfdec + (n <<= 1)); s[3] = *(list+1); \
1062 	  } \
1063 	  if(v < 100) \
1064 	  { if(v < 10) \
1065 	    { 	s -= 1; s[0] = (char)('0'+v); \
1066 	    } else \
1067 	    { 	s -= 2; s[0] = *(list = (char*)_Sfdec + (v <<= 1)); s[1] = *(list+1); \
1068 	    } \
1069 	  } else \
1070 	  { if(v < 1000) \
1071 	    { 	s -= 3; SFDIGIT(v,100,s[0]); \
1072 			s[1] = *(list = (char*)_Sfdec + (v <<= 1)); s[2] = *(list+1); \
1073 	    } else \
1074 	    {	s -= 4; SFDIGIT(v,1000,s[0]); SFDIGIT(v,100,s[1]); \
1075 			s[2] = *(list = (char*)_Sfdec + (v <<= 1)); s[3] = *(list+1); \
1076 	    } \
1077 	  } \
1078 	}
1079 
1080 /* handy functions */
1081 #undef min
1082 #undef max
1083 #define min(x,y)	((x) < (y) ? (x) : (y))
1084 #define max(x,y)	((x) > (y) ? (x) : (y))
1085 
1086 /* fast functions for memory copy and memory clear */
1087 #if _PACKAGE_ast
1088 #define memclear(s,n)	memzero(s,n)
1089 #else
1090 #if _lib_bcopy && !_lib_memcpy
1091 #define memcpy(to,fr,n)	bcopy((fr),(to),(n))
1092 #endif
1093 #if _lib_bzero && !_lib_memset
1094 #define memclear(s,n)	bzero((s),(n))
1095 #else
1096 #define memclear(s,n)	memset((s),'\0',(n))
1097 #endif
1098 #endif /*_PACKAGE_ast*/
1099 
1100 /* note that MEMCPY advances the associated pointers */
1101 #define MEMCPY(to,fr,n) \
1102 	switch(n) \
1103 	{ default : memcpy((Void_t*)to,(Void_t*)fr,n); to += n; fr += n; break; \
1104 	  case  7 : *to++ = *fr++; \
1105 	  case  6 : *to++ = *fr++; \
1106 	  case  5 : *to++ = *fr++; \
1107 	  case  4 : *to++ = *fr++; \
1108 	  case  3 : *to++ = *fr++; \
1109 	  case  2 : *to++ = *fr++; \
1110 	  case  1 : *to++ = *fr++; \
1111 	}
1112 #define MEMSET(s,c,n) \
1113 	switch(n) \
1114 	{ default : memset((Void_t*)s,(int)c,n); s += n; break; \
1115 	  case  7 : *s++ = c; \
1116 	  case  6 : *s++ = c; \
1117 	  case  5 : *s++ = c; \
1118 	  case  4 : *s++ = c; \
1119 	  case  3 : *s++ = c; \
1120 	  case  2 : *s++ = c; \
1121 	  case  1 : *s++ = c; \
1122 	}
1123 
1124 _BEGIN_EXTERNS_
1125 
1126 extern Sftab_t		_Sftable;
1127 
1128 extern int		_sfpopen _ARG_((Sfio_t*, int, int, int));
1129 extern int		_sfpclose _ARG_((Sfio_t*));
1130 extern int		_sfexcept _ARG_((Sfio_t*, int, ssize_t, Sfdisc_t*));
1131 extern Sfrsrv_t*	_sfrsrv _ARG_((Sfio_t*, ssize_t));
1132 extern int		_sfsetpool _ARG_((Sfio_t*));
1133 extern char*		_sfcvt _ARG_((Sfdouble_t,char*,size_t,int,int*,int*,int*,int));
1134 extern char**		_sfgetpath _ARG_((char*));
1135 
1136 #if _BLD_sfio && defined(__EXPORT__)
1137 #define extern		__EXPORT__
1138 #endif
1139 #if !_BLD_sfio && defined(__IMPORT__)
1140 #define extern		extern __IMPORT__
1141 #endif
1142 
1143 extern Sfextern_t	_Sfextern;
1144 
1145 extern int		_sfmode _ARG_((Sfio_t*, int, int));
1146 extern int		_sftype _ARG_((const char*, int*, int*));
1147 
1148 #undef	extern
1149 
1150 #ifndef errno
1151 extern int		errno;
1152 #endif
1153 
1154 /* for portable encoding of double values */
1155 #ifndef frexpl
1156 #if _ast_fltmax_double
1157 #define frexpl		frexp
1158 #endif
1159 #if !__STDC__
1160 extern Sfdouble_t	frexpl _ARG_((Sfdouble_t, int*));
1161 #endif
1162 #endif
1163 #ifndef ldexpl
1164 #if _ast_fltmax_double
1165 #define ldexpl		ldexp
1166 #endif
1167 #if !__STDC__
1168 extern Sfdouble_t	ldexpl _ARG_((Sfdouble_t, int));
1169 #endif
1170 #endif
1171 
1172 #if !_PACKAGE_ast
1173 
1174 #if !__STDC__ && !_hdr_stdlib
1175 extern void	abort _ARG_((void));
1176 extern int	atexit _ARG_((void(*)(void)));
1177 extern char*	getenv _ARG_((const char*));
1178 extern void*	malloc _ARG_((size_t));
1179 extern void*	realloc _ARG_((void*, size_t));
1180 extern void	free _ARG_((void*));
1181 extern size_t	strlen _ARG_((const char*));
1182 extern char*	strcpy _ARG_((char*, const char*));
1183 
1184 extern Void_t*	memset _ARG_((void*, int, size_t));
1185 extern Void_t*	memchr _ARG_((const void*, int, size_t));
1186 extern Void_t*	memccpy _ARG_((void*, const void*, int, size_t));
1187 #ifndef memcpy
1188 extern Void_t*	memcpy _ARG_((void*, const void*, size_t));
1189 #endif
1190 #if !defined(strtod)
1191 extern double	strtod _ARG_((const char*, char**));
1192 #endif
1193 #if !defined(remove)
1194 extern int	sysremovef _ARG_((const char*));
1195 #endif
1196 #endif /* !__STDC__ && !_hdr_stdlib */
1197 
1198 #if !_hdr_unistd
1199 #if _proto_open && __cplusplus
1200 extern int	sysopenf _ARG_((const char*, int, ...));
1201 #endif
1202 extern int	sysclosef _ARG_((int));
1203 extern ssize_t	sysreadf _ARG_((int, void*, size_t));
1204 extern ssize_t	syswritef _ARG_((int, const void*, size_t));
1205 extern sfoff_t	syslseekf _ARG_((int, sfoff_t, int));
1206 extern int	sysdupf _ARG_((int));
1207 extern int	syspipef _ARG_((int*));
1208 extern int	sysaccessf _ARG_((const char*, int));
1209 extern int	sysremovef _ARG_((const char*));
1210 extern int	sysfstatf _ARG_((int, sfstat_t*));
1211 extern int	sysstatf _ARG_((const char*, sfstat_t*));
1212 
1213 extern int	isatty _ARG_((int));
1214 
1215 extern int	wait _ARG_((int*));
1216 extern uint	sleep _ARG_((uint));
1217 extern int	execl _ARG_((const char*, const char*,...));
1218 extern int	execv _ARG_((const char*, char**));
1219 #if !defined(fork)
1220 extern int	fork _ARG_((void));
1221 #endif
1222 #if _lib_unlink
1223 extern int	unlink _ARG_((const char*));
1224 #endif
1225 
1226 #endif /*_hdr_unistd*/
1227 
1228 #if _lib_bcopy && !_proto_bcopy
1229 extern void	bcopy _ARG_((const void*, void*, size_t));
1230 #endif
1231 #if _lib_bzero && !_proto_bzero
1232 extern void	bzero _ARG_((void*, size_t));
1233 #endif
1234 
1235 extern time_t	time _ARG_((time_t*));
1236 extern int	waitpid _ARG_((int,int*,int));
1237 extern void	_exit _ARG_((int));
1238 typedef int(*	Onexit_f)_ARG_((void));
1239 extern Onexit_f	onexit _ARG_((Onexit_f));
1240 
1241 #if _lib_vfork && !_hdr_vfork && !_sys_vfork
1242 extern pid_t	vfork _ARG_((void));
1243 #endif /*_lib_vfork*/
1244 
1245 #if _lib_poll
1246 #if _lib_poll_fd_1
1247 extern int	poll _ARG_((struct pollfd*, ulong, int));
1248 #else
1249 extern int	poll _ARG_((ulong, struct pollfd*, int));
1250 #endif
1251 #endif /*_lib_poll*/
1252 
1253 #endif /* _PACKAGE_ast */
1254 
1255 _END_EXTERNS_
1256 
1257 #endif /*_SFHDR_H*/
1258