1#! /bin/sh
2# Output RCS compile-time configuration.
3Id='$Id: conf.sh,v 5.25 1995/06/16 06:19:24 eggert Exp $'
4#	Copyright 1990, 1991, 1992, 1993, 1994, 1995 Paul Eggert
5#	Distributed under license by the Free Software Foundation, Inc.
6
7# This file is part of RCS.
8#
9# RCS is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13#
14# RCS is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with RCS; see the file COPYING.
21# If not, write to the Free Software Foundation,
22# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23#
24# Report problems and direct all questions to:
25#
26#	rcs-bugs@cs.purdue.edu
27
28
29# Standard output should already be directed to "a.h";
30# later parts of this procedure need it.
31# Standard error can be ignored if a.h is OK,
32# and can be inspected for clues otherwise.
33
34# The Makefile overrides the following defaults.
35: ${RCSPREFIX=/usr/local/bin/}
36: ${ALL_CFLAGS=-Dhas_conf_h}
37: ${CC=cc}
38: ${COMPAT2=0}
39: ${DIFF=${RCSPREFIX}diff}
40: ${DIFF3=${DIFF}3}
41: ${DIFF3_BIN=1}
42: ${DIFFFLAGS=-an}
43: ${DIFF_L=1}
44: ${DIFF_SUCCESS=0} ${DIFF_FAILURE=1} ${DIFF_TROUBLE=2}
45: ${ED=/bin/ed}
46: ${SENDMAIL='"/usr/lib/sendmail"'}
47# : ${LDFLAGS=} ${LIBS=} tickles old shell bug
48
49C="$CC $ALL_CFLAGS"
50CL="$CC $ALL_CFLAGS $LDFLAGS -o a.out"
51L=$LIBS
52
53cat <<EOF
54/* RCS compile-time configuration */
55
56	/* $Id */
57
58/*
59 * This file is generated automatically.
60 * If you edit it by hand your changes may be lost.
61 * Instead, please try to fix conf.sh,
62 * and send your fixes to rcs-bugs@cs.purdue.edu.
63 */
64
65EOF
66
67n='
68'
69case `echo -n` in
70-n)
71	ech='echo' dots='... \c';;
72*)
73	ech='echo -n' dots='... '
74esac
75
76$ech >&3 "$0: testing compiler for plausibility $dots"
77echo 'main() { return 0; }' >a.c
78rm -f a.exe a.out || exit
79$CL a.c $L >&2 || {
80	echo >&3 "$n$0: The command '$CL a.c $L' failed on a trivial program."
81	exit 1
82}
83echo 'this is not a C source file' >a.c
84rm -f a.exe a.out || exit
85$CL a.c $L >&2 && {
86	echo >&3 "$n$0: The command '$CL a.c $L' succeeds when it should fail."
87	exit 1
88}
89echo >&3 OK
90
91$ech >&3 "$0: configuring exitmain $dots"
92cat >a.c <<EOF
93#include "a.h"
94int main(argc,argv) int argc; char **argv; { return argc-1; }
95EOF
96rm -f a.exe a.out || exit
97if $CL a.c $L >&2
98then A_H=a.h
99else
100	echo >&3 failed
101	$ech >&3 "$0: attempting to work around Domain/OS brain damage $dots"
102	cat >a.c <<EOF
103#include "a.hap"
104int main(argc,argv) int argc; char **argv; { return argc-1; }
105EOF
106	cat <a.h >a.hap &&
107	$CL a.c $L >&2 || exit 1
108	# The Domain/OS C compiler refuses to read a.h because the file
109	# is currently open for writing.  Work around this brain damage by
110	# copying it to a.hap before each compilation; include a.hap instead.
111	A_H=a.hap
112fi
113if test -f a.out
114then aout=./a.out
115elif test -f a.exe
116then aout=./a.exe
117else
118	echo >&3 "$n$0: C compiler creates neither a.out nor a.exe."
119	exit 1
120fi
121e='exit(n), 3 /* lint fodder */'
122if $aout -
123then :
124elif $aout
125then e=n
126fi
127case $e in
128n) echo >&3 OK;;
129*) echo >&3 "return does not work; using exit instead"
130esac
131echo "#define exitmain(n) return $e /* how to exit from main() */"
132
133: PREPARE_CC
134case $A_H in
135a.h)
136	PREPARE_CC="rm -f $aout";;
137*)
138	echo "rm -f $aout \$1 && cat <a.h >$A_H" >a.pre
139	PREPARE_CC="sh a.pre"
140esac
141
142for id in _POSIX_C_SOURCE _POSIX_SOURCE
143do
144	$ech >&3 "$0: configuring $id $dots"
145	cat >a.c <<EOF
146#include "$A_H"
147#include <stdio.h>
148int
149main() {
150#	ifdef fileno
151#		define f(x) fileno(x)
152#	else
153		/* Force a compile-time error if fileno isn't declared.  */
154		int (*p)() = fileno;
155#		define f(x) (*p)(x)
156#	endif
157	/* Some buggy linkers seem to need the getchar.  */
158	exitmain(getchar() != '#' || fileno(stdout) != 1);
159}
160#if syntax_error
161syntax error
162#endif
163EOF
164	a='/* ' z='*/ '
165	case $id in
166	_POSIX_SOURCE)
167		version=1003.1-1990
168		value=;;
169	_POSIX_C_SOURCE)
170		version='1003.1b-1993 or later'
171		value='2147483647L ';;
172	esac
173	$PREPARE_CC || exit
174	if ($CL a.c $L && $aout <a.c) >&2
175	then :
176	elif $PREPARE_CC || exit; ($CL -D$id=$value a.c $L && $aout <a.c) >&2
177	then a= z=
178	fi
179	case $a in
180	?*) echo >&3 OK;;
181	'') echo >&3 "must define it, unfortunately"
182	esac
183	echo "$a#define $id $value$z/* if strict C + Posix $version */"
184done
185
186cat <<'EOF'
187
188#include <errno.h>
189#include <stdio.h>
190#include <time.h>
191
192/* Comment out #include lines below that do not work.  */
193EOF
194
195$ech >&3 "$0: configuring how to check for syntax errors $dots"
196# Run `$CS a.c $LS' instead of `$CL a.c $L' for compile-time checking only.
197# This speeds up the configuration process.
198if
199	rm -f a.s && $C -S a.c >&2 && test -s a.s && rm -f a.s &&
200	if $C -S -Dsyntax_error=1 a.c >&2 && test -s a.s
201	then false
202	else :
203	fi
204then
205	# Generate assembly language output.
206	CS="$C -S" LS= o=a.s PREPARE_CC="$PREPARE_CC $o"
207elif
208	rm -f a.o a.obj && $C -c a.c >&2 &&
209	if test -s a.o
210	then o=a.o
211	elif test -s a.obj
212	then o=a.obj
213	else false
214	fi &&
215	if $C -c -Dsyntax_error=1 a.c >&2 && test -s $o
216	then false
217	else :
218	fi
219then
220	# Generate object code.
221	CS="$C -c" LS= PREPARE_CC="$PREPARE_CC $o"
222else
223	# Generate an executable.
224	CS=$CL LS=$L o=$aout
225fi
226CS_OK="test -s $o"
227echo >&3 $CS
228
229# standard include files
230# sys/types.h and sys/stat.h must come first because others depend on them.
231has_signal=1
232for h in \
233	sys/types sys/stat \
234	dirent fcntl limits mach/mach net/errno \
235	pwd siginfo signal stdlib string \
236	sys/mman sys/wait ucontext unistd utime vfork
237do
238	i="#include <$h.h>"
239	$ech >&3 "$0: configuring $i $dots"
240	cat >a.c <<EOF
241#include "$A_H"
242$i
243int main(){ exitmain(0); }
244EOF
245	ok=OK
246	$PREPARE_CC || exit
247	$CS a.c $LS >&2 && $CS_OK || {
248		case $h in
249		string)
250			i='#include <strings.h>'
251			ok="$i instead";;
252		*)
253			i="/* $i */"
254			ok="commenting it out"
255		esac
256		case $h in
257		signal) has_signal=0
258		esac
259	}
260	echo >&3 "$ok"
261	echo "$i"
262done
263
264cat <<'EOF'
265
266/* Define boolean symbols to be 0 (false, the default), or 1 (true).  */
267EOF
268
269# has_sys_param_h
270$ech >&3 "$0: configuring has_sys_param_h $dots"
271cat >a.c <<EOF
272#include "$A_H"
273#include <sys/param.h>
274int main() { exitmain(0); }
275EOF
276$PREPARE_CC || exit
277if $CS a.c $LS >&2 && $CS_OK
278then h=1 ok=OK
279else h=0 ok=absent
280fi
281echo >&3 $ok
282echo "#define has_sys_param_h $h /* Does #include <sys/param.h> work?  */"
283
284# We must do errno next, because has_readlink needs it.
285/* <errno.h> */
286$ech >&3 "$0: configuring errno $dots"
287cat >a.c <<EOF
288#include "$A_H"
289int main() { exitmain(errno != errno); }
290EOF
291$PREPARE_CC || exit
292if $CS a.c $LS >&2
293then a='/* ' z=' */' ok=OK
294else a= z= ok='declaration missing'
295fi
296echo >&3 $ok
297echo "${a}extern int errno;$z /* Uncomment if <errno.h> doesn't declare errno.  */"
298rm -f a.c || exit
299
300# We must do has_readlink next, because it might generate
301# #include directives that affect later definitions.
302
303$ech >&3 "$0: configuring has_readlink, readlink_isreg_errno $dots"
304cat >a.c <<EOF
305#include "$A_H"
306static char b[7];
307int
308main() {
309	if (readlink("a.sym2",b,7) == 6  &&  strcmp(b,"a.sym1") == 0  &&
310		readlink("a.c",b,7) == -1  &&  errno != ENOENT
311	) {
312		if (errno == EINVAL)
313			printf("EINVAL\n");
314		else
315			printf("%d\n", errno);
316		exitmain(ferror(stdout) || fclose(stdout)!=0);
317	}
318	exitmain(1);
319}
320EOF
321$PREPARE_CC a.sym* || exit
322readlink_isreg_errno='?'
323if (ln -s a.sym1 a.sym2 && $CL a.c $L) >&2 && readlink_isreg_errno=`$aout`
324then h=1
325else h=0
326fi
327echo >&3 $h, $readlink_isreg_errno
328cat <<EOF
329#define has_readlink $h /* Does readlink() work?  */
330#define readlink_isreg_errno $readlink_isreg_errno /* errno after readlink on regular file */
331
332#if has_readlink && !defined(MAXSYMLINKS)
333#	if has_sys_param_h
334#		include <sys/param.h>
335#	endif
336#	ifndef MAXSYMLINKS
337#		define MAXSYMLINKS 20 /* BSD; not standard yet */
338#	endif
339#endif
340EOF
341
342# *_t
343cat <<'EOF'
344
345/* Comment out the typedefs below if the types are already declared.  */
346/* Fix any uncommented typedefs that are wrong.  */
347EOF
348cat >a.c <<EOF
349#include "$A_H"
350t x;
351int main() { exitmain(0); }
352EOF
353for t in mode_t off_t pid_t sig_atomic_t size_t ssize_t time_t uid_t
354do
355	$ech >&3 "$0: configuring $t $dots"
356	case $t in
357	size_t) i=unsigned;;
358	off_t|time_t) i=long;;
359	*) i=int;;
360	esac
361	$PREPARE_CC || exit
362	if $CS -Dt=$t a.c $LS >&2 && $CS_OK
363	then ok=OK a='/* ' z=' */'
364	else ok=$i a= z=
365	fi
366	echo >&3 $ok
367	echo "${a}typedef $i $t;$z"
368done
369
370cat <<'EOF'
371
372/* Comment out the keyword definitions below if the keywords work.  */
373EOF
374
375for i in const volatile
376do
377	$ech >&3 "$0: configuring $i $dots"
378	cat >a.c <<EOF
379#	include "$A_H"
380	int main();
381	enum Boolean { false, true };
382	int hpux8_05barf();
383	static enum Boolean $i zero;
384	int hpux8_05barf(x) int x; { return x; }
385	static enum Boolean $i * $i azero = &zero;
386	static enum Boolean $i * $i * $i aazero = &azero;
387	static enum Boolean * $i arzero[1];
388	static sig_atomic_t $i sigzero;
389	int sco3_2v4barf() {
390		/* SCO 3.2v4 native compiler barfs on this.  */
391		char *t;
392		char $i *s = 0 ? (char *)0 : (char $i *)0;
393		*t++ = '\0';
394	}
395	int main() {
396		enum Boolean *p = arzero[sigzero];
397		switch (zero) {
398			case false: exitmain(!p || **aazero);
399			default: exitmain(hpux8_05barf(1));
400		}
401	}
402EOF
403	a= z= ok='broken'
404	$PREPARE_CC || exit
405	if $CS a.c $LS >&2 && $CS_OK
406	then
407		cat >a.c <<EOF
408			char $i *p;
409			char *q;
410			typedef unsigned char $i *Iptr_type;
411			struct { Iptr_type lim; } s, *f = &s;
412			int main() {
413				Iptr_type lim;
414				lim = f->lim;
415				p = p == q ? p : "";
416				p = p == "" ? p : q;
417				return !!lim;
418			}
419EOF
420		$PREPARE_CC || exit
421		if $CS a.c $LS >&2 && $CS_OK
422		then
423			case $i in
424			const)
425				# Check for common execv misdeclaration.
426				cat >a.c <<EOF
427#					include "$A_H"
428					static char * const *p;
429					int main() { return execv("/bin/sh", p); }
430EOF
431				$PREPARE_CC || exit
432				$CS a.c $LS >&2 && $CS_OK
433			esac && a='/* ' z=' */' ok=OK
434		fi
435	fi
436	echo >&3 $ok
437	echo "$a#define $i$z"
438done
439
440echo >&3 "$0: configuring has_prototypes, has_stdarg, has_varargs, va_start_args $dots"
441cat >a.ha <<'EOF'
442#if has_prototypes
443#	define P(params) params
444#else
445#	define P(params) ()
446#endif
447#if has_stdarg
448#	include <stdarg.h>
449#else
450#	if has_varargs
451#		include <varargs.h>
452#	else
453		typedef char *va_list;
454#		define va_dcl int va_alist;
455#		define va_start(ap) ((ap) = (va_list)&va_alist)
456#		define va_arg(ap,t) (((t*) ((ap)+=sizeof(t)))  [-1])
457#		define va_end(ap)
458#	endif
459#endif
460#if va_start_args == 2
461#	define vararg_start va_start
462#else
463#	define vararg_start(ap,p) va_start(ap)
464#endif
465EOF
466cat >a.c <<EOF
467#include "$A_H"
468#include "a.ha"
469
470struct buf { int x; };
471int pairnames P((int,char**,FILE*(*)P((struct buf*,struct stat*,int)),int,int)); /* a la rcsbase.h */
472FILE *(*rcsopen)P((struct buf*,struct stat*,int));  /* a la rcsfnms.c */
473
474static char *e(p,i) char **p; int i; { return p[i]; }
475#if has_prototypes
476static char *f(char *(*g)(char**,int), char **p, ...)
477#else
478static char *f(g, p, va_alist) char *(*g)(); char **p; va_dcl
479#endif
480{
481	char *s;
482	va_list v;
483	vararg_start(v,p);
484	s = g(p, va_arg(v,int));
485	va_end(v);
486	return s;
487}
488int main P((int, char**));
489int
490main(argc, argv) int argc; char **argv; {
491	exitmain(f(e,argv,0) != argv[0]  ||  f(e,argv,1) != argv[1]);
492}
493EOF
494for has_prototypes in 1 0
495do
496	for has_stdarg in 1 v 0
497	do
498		case $has_stdarg in
499		1) has_varargs=-1;;
500		v) has_varargs=1 has_stdarg=0;;
501		*) has_varargs=0
502		esac
503		case $has_stdarg in
504		0) as='1 2';;
505		1) as='2 1'
506		esac
507		for va_start_args in $as
508		do
509			$PREPARE_CC || exit
510			$CL \
511				-Dhas_prototypes=$has_prototypes \
512				-Dhas_stdarg=$has_stdarg \
513				-Dhas_varargs=$has_varargs \
514				-Dva_start_args=$va_start_args \
515				a.c $L >&2 && $aout && break
516		done && break
517	done && break
518done || {
519	echo >&3 $0: cannot deduce has_prototypes, has_stdarg, va_start_args
520	exit 1
521}
522echo >&3 $has_prototypes, $has_stdarg, $has_varargs, $va_start_args
523case $has_varargs in
524-1) a='/* ' z='*/ ' has_varargs='?';;
525*) a= z=
526esac
527cat - a.ha <<EOF
528
529/* Define boolean symbols to be 0 (false, the default), or 1 (true).  */
530#define has_prototypes $has_prototypes /* Do function prototypes work?  */
531#define has_stdarg $has_stdarg /* Does <stdarg.h> work?  */
532$a#define has_varargs $has_varargs $z/* Does <varargs.h> work?  */
533#define va_start_args $va_start_args /* How many args does va_start() take?  */
534
535#if O_BINARY
536	/* Text and binary i/o behave differently.  */
537	/* This is incompatible with Posix and Unix.  */
538#	define FOPEN_RB "rb"
539#	define FOPEN_R_WORK (Expand==BINARY_EXPAND ? "r" : "rb")
540#	define FOPEN_WB "wb"
541#	define FOPEN_W_WORK (Expand==BINARY_EXPAND ? "w" : "wb")
542#	define FOPEN_WPLUS_WORK (Expand==BINARY_EXPAND ? "w+" : "w+b")
543#	define OPEN_O_BINARY O_BINARY
544#else
545	/*
546	* Text and binary i/o behave the same.
547	* Omit "b", since some nonstandard hosts reject it.
548	*/
549#	define FOPEN_RB "r"
550#	define FOPEN_R_WORK "r"
551#	define FOPEN_WB "w"
552#	define FOPEN_W_WORK "w"
553#	define FOPEN_WPLUS_WORK "w+"
554#	define OPEN_O_BINARY 0
555#endif
556
557/* This may need changing on non-Unix systems (notably DOS).  */
558#define OPEN_CREAT_READONLY (S_IRUSR|S_IRGRP|S_IROTH) /* lock file mode */
559#define OPEN_O_LOCK 0 /* extra open flags for creating lock file */
560#define OPEN_O_WRONLY O_WRONLY /* main open flag for creating a lock file */
561
562/* Define or comment out the following symbols as needed.  */
563EOF
564
565$ech >&3 "$0: configuring bad_chmod_close $dots"
566cat >a.c <<EOF
567#include "$A_H"
568#ifndef O_RDONLY
569#	define O_RDONLY 0
570#endif
571int
572main() {
573	int f;
574	exitmain(
575		(f = open("a.c", O_RDONLY)) < 0 ||
576		chmod("a.c", 0) != 0 ||
577		close(f) != 0
578	);
579}
580EOF
581$PREPARE_CC || exit
582if $CL a.c $L >&2 && $aout
583then b=0 ok=OK
584else b=1 ok='will work around bug'
585fi
586echo >&3 $ok
587echo "#define bad_chmod_close $b /* Can chmod() close file descriptors?  */"
588rm -f a.c || exit
589
590$ech >&3 "$0: configuring bad_creat0 $dots"
591cat >a.c <<EOF
592#include "$A_H"
593#if defined(O_CREAT) && defined(O_WRONLY)
594#	define creat0(f) open(f, O_CREAT|O_WRONLY, 0)
595#else
596#	define creat0(f) creat(f, 0)
597#endif
598char buf[17000];
599int
600main() {
601	int f;
602	exitmain(
603		(f = creat0("a.d")) < 0  ||
604		write(f, buf, sizeof(buf)) != sizeof(buf) ||
605		close(f) != 0
606	);
607}
608EOF
609$PREPARE_CC a.d || exit
610if $CL a.c $L >&2 && $aout && test -f a.d && test ! -w a.d
611then b=0 ok=OK
612else b=1 ok='will work around bug'
613fi
614echo >&3 $ok
615echo "#define bad_creat0 $b /* Do writes fail after creat(f,0)?  */"
616rm -f a.d || exit
617
618$ech >&3 "$0: configuring bad_fopen_wplus $dots"
619cat >a.c <<EOF
620#include "$A_H"
621int main() { exitmain(!fopen("a.d","w+")); }
622EOF
623$PREPARE_CC || exit
624if echo nonempty >a.d && $CL a.c $L >&2 && $aout && test ! -s a.d
625then b=0 ok=OK
626else b=1 ok='will work around bug'
627fi
628echo >&3 $ok
629echo "#define bad_fopen_wplus $b /* Does fopen(f,\"w+\") fail to truncate f?  */"
630
631echo "#define getlogin_is_secure 0 /* Is getlogin() secure?  Usually it's not.  */"
632
633$ech >&3 "$0: configuring has_attribute_noreturn $dots"
634cat >a.c <<EOF
635#include "$A_H"
636static void e P((int)) __attribute__((noreturn));
637static void e(i) int i; { exit(i); }
638int main() { e(0); }
639EOF
640$PREPARE_CC || exit
641h=0 ok='does not work'
642if out=`$CS a.c $LS 2>&1`
643then
644	case $out in
645	*noreturn*) ;;
646	*) h=1 ok=OK
647	esac
648fi
649echo >&2 "$out"
650echo >&3 $ok
651cat <<EOF
652#define has_attribute_noreturn $h /* Does __attribute__((noreturn)) work?  */
653#if has_attribute_noreturn
654#	define exiting __attribute__((noreturn))
655#else
656#	define exiting
657#endif
658EOF
659rm -f a.c || exit
660
661$ech >&3 "$0: configuring has_dirent, void_closedir $dots"
662cat >a.c <<EOF
663#include "$A_H"
664#if void_closedir
665#	define close_directory(d) (closedir(d), 0)
666#else
667#	define close_directory(d) closedir(d)
668#endif
669int
670main() {
671	DIR *d = opendir(".");
672	struct dirent *e;
673	while ((e = readdir(d)))
674		if (strcmp(e->d_name, "a.c") == 0  &&  close_directory(d) == 0)
675			exitmain(0);
676	exitmain(1);
677}
678EOF
679$PREPARE_CC || exit
680has_dirent=0 ok='does not work'
681void_closedir=? a='/* ' z='*/ '
682for v in 0 1
683do
684	if $CL -Dvoid_closedir=$v a.c $L >&2 && $aout
685	then
686		has_dirent=1 ok=OK
687		void_closedir=$v a= z=
688		case $v in
689		1) ok='OK, but closedir yields void'
690		esac
691		break
692	fi
693done
694echo >&3 $ok
695echo "#define has_dirent $has_dirent /* Do opendir(), readdir(), closedir() work?  */"
696echo "$a#define void_closedir $void_closedir $z/* Does closedir() yield void?  */"
697
698$ech >&3 "$0: configuring has_fchmod $dots"
699cat >a.c <<EOF
700#include "$A_H"
701#ifndef STDIN_FILENO
702#define STDIN_FILENO 0
703#endif
704int main() { exitmain(fchmod(STDIN_FILENO,0) != 0); }
705EOF
706$PREPARE_CC || exit
707if $CL a.c $L >&2 && $aout <a.c && test ! -r a.c
708then h=1 ok=OK
709else h=0 ok='does not work'
710fi
711echo >&3 $ok
712echo "#define has_fchmod $h /* Does fchmod() work?  */"
713rm -f a.c || exit
714
715$ech >&3 "$0: configuring has_fflush_input $dots"
716cat >a.c <<EOF
717#include "$A_H"
718#ifndef SEEK_SET
719#define SEEK_SET 0
720#endif
721#ifndef SEEK_CUR
722#define SEEK_CUR 1
723#endif
724#ifndef STDIN_FILENO
725#define STDIN_FILENO 0
726#endif
727int main() {
728	exitmain(
729		getchar() == EOF
730		|| fseek(stdin, 0L, SEEK_SET) != 0
731		|| fflush(stdin) != 0
732		|| lseek(STDIN_FILENO, (off_t)0, SEEK_CUR) != 0
733	);
734}
735EOF
736$PREPARE_CC || exit
737if $CL a.c $L >&2 && $aout <a.c
738then h=1 ok=OK
739else h=0 ok='does not work'
740fi
741echo >&3 $ok
742echo "#define has_fflush_input $h /* Does fflush() work on input files?  */"
743rm -f a.c || exit
744
745$ech >&3 "$0: configuring has_fputs $dots"
746cat >a.c <<EOF
747#include "$A_H"
748int main() { exitmain(fputs("Hello\"\nworld", stdout) < 0); }
749EOF
750Hello='Hello"
751world'
752$PREPARE_CC a.a || exit
753if $CL a.c $L >&2 && $aout >a.a && x=`$aout` && test " $x" = " $Hello"
754then h=1 ok=OK
755else h=0 ok='does not work'
756fi
757echo >&3 $ok
758echo "#define has_fputs $h /* Does fputs() work?  */"
759
760$ech >&3 "$0: configuring has_ftruncate $dots"
761cat >a.c <<EOF
762#include "$A_H"
763/*
764 * We'd like to test ftruncate(creat(f,0), 0),
765 * since that's the way RCS uses it,
766 * but a common bug causes it to fail over NFS.
767 * Since we must defend against this bug at run time anyway,
768 * we don't bother to check for it at compile time.
769 * So we test ftruncate(creat(f,0200), 0) instead.
770 */
771#if defined(O_CREAT) && defined(O_WRONLY) && defined(S_IWUSR)
772#	define creat0200(f) open(f, O_CREAT|O_WRONLY, S_IWUSR)
773#else
774#	define creat0200(f) creat(f, 0200)
775#endif
776int
777main(argc, argv) int argc; char **argv; {
778	int f = creat0200(argv[1]);
779	if (f<0 || write(f,"abc",3)!=3 || ftruncate(f,(off_t)0)!=0 || close(f)!=0)
780		exitmain(1);
781	exitmain(0);
782}
783EOF
784$PREPARE_CC a.a || exit
785if ($CL a.c $L && $aout a.a && test -w a.a && test ! -s a.a) >&2
786then h=1 ok=OK
787else h=0 ok='does not work'
788fi
789echo >&3 $ok
790echo "#define has_ftruncate $h /* Does ftruncate() work?  */"
791
792$ech >&3 "$0: configuring has_getuid $dots"
793cat >a.c <<EOF
794#include "$A_H"
795int main() { exitmain(getuid()!=getuid()); }
796EOF
797$PREPARE_CC || exit
798if ($CL a.c $L && $aout) >&2
799then has_getuid=1 ok=OK
800else has_getuid=0 ok='does not work'
801fi
802echo >&3 $ok
803echo "#define has_getuid $has_getuid /* Does getuid() work?  */"
804
805case $has_getuid in
8060)
807	a='/* ' z='*/ ' h=?;;
808*)
809	$ech >&3 "$0: configuring has_getpwuid $dots"
810	a= z=
811	cat >a.c <<EOF
812#include "$A_H"
813int main() { exitmain(!getpwuid(0)); }
814EOF
815	$PREPARE_CC || exit
816	if ($CL a.c $L && $aout) >&2
817	then h=1 ok=OK
818	else h=0 ok='does not work'
819	fi
820	echo >&3 $ok
821esac
822echo "$a#define has_getpwuid $h $z/* Does getpwuid() work?  */"
823
824$ech >&3 "$0: configuring has_kill $dots"
825cat >a.c <<EOF
826#include "$A_H"
827int main() { exitmain(kill(getpid(), 0) != 0); }
828EOF
829$PREPARE_CC || exit
830if ($CL a.c $L && $aout) >&2
831then has_kill=1 ok=OK
832else has_kill=0 ok='does not work'
833fi
834echo >&3 $ok
835# Used only by this script, not by RCS, so we don't output it to stdout.
836
837$ech >&3 "$0: configuring has_memcmp $dots"
838cat >a.c <<EOF
839#include "$A_H"
840int main() { exitmain(memcmp("beautiful","beautiful",10) != 0); }
841EOF
842$PREPARE_CC || exit
843if ($CL a.c $L && $aout) >&2
844then h=1 ok=OK
845else h=0 ok='does not work'
846fi
847echo >&3 $ok
848echo "#define has_memcmp $h /* Does memcmp() work?  */"
849
850$ech >&3 "$0: configuring has_memcpy $dots"
851cat >a.c <<EOF
852#include "$A_H"
853char a[3];
854int
855main() {
856	memcpy(a,"xy",3);
857	exitmain(strcmp(a,"xy")!=0);
858}
859EOF
860$PREPARE_CC || exit
861if ($CL a.c $L && $aout) >&2
862then h=1 ok=OK
863else h=0 ok='does not work'
864fi
865echo >&3 $ok
866echo "#define has_memcpy $h /* Does memcpy() work?  */"
867
868$ech >&3 "$0: configuring has_memmove $dots"
869cat >a.c <<EOF
870#include "$A_H"
871static char a[4];
872int
873main() {
874	strcpy(a, "xy");
875	memmove(a+1, a, 3);
876	exitmain(strcmp(a,"xxy")!=0);
877}
878EOF
879$PREPARE_CC || exit
880if ($CL a.c $L && $aout) >&2
881then h=1 ok=OK
882else h=0 ok='does not work'
883fi
884echo >&3 $ok
885echo "#define has_memmove $h /* Does memmove() work?  */"
886
887$ech >&3 "$0: configuring has_map_fd, has_mmap, has_madvise, mmap_signal $dots"
888rm -f a.c a.d a.e || exit
889cat >a.c <<EOF
890#define CHAR1 '#' /* the first character in this file */
891#include "$A_H"
892static char *a;
893static struct stat b;
894#ifndef MADVISE_OK
895#define MADVISE_OK (madvise(a,b.st_size,MADV_SEQUENTIAL)==0 && madvise(a,b.st_size,MADV_NORMAL)==0)
896#endif
897#ifndef WTERMSIG
898#define WTERMSIG(s) ((s)&0177)
899#undef WIFSIGNALED /* Avoid 4.3BSD incompatibility with Posix.  */
900#endif
901#ifndef WIFSIGNALED
902#define WIFSIGNALED(s) (((s)&0377) != 0177  &&  WTERMSIG(s) != 0)
903#endif
904#ifndef MAP_FAILED
905#define MAP_FAILED (-1)
906#endif
907#ifndef STDIN_FILENO
908#define STDIN_FILENO 0
909#endif
910int
911main(argc, argv) int argc; char **argv; {
912	int s = 0;
913#if TRY_MAP_FD
914	kern_return_t kr;
915	vm_address_t va;
916#endif
917
918	if (fstat(STDIN_FILENO, &b) != 0) {
919		perror("fstat");
920		exitmain(1);
921	}
922#	if TRY_MAP_FD
923		kr = map_fd(STDIN_FILENO, 0, &va, TRUE, b.st_size);
924		if (kr != KERN_SUCCESS) {
925			mach_error("map_fd", kr);
926			exitmain(1);
927		}
928		a = (char *) va;
929#	else
930		a = mmap(
931			(char *)0, b.st_size, PROT_READ, MAP_SHARED,
932			STDIN_FILENO, (off_t)0
933		);
934		if (a == (char *)MAP_FAILED) {
935			perror("mmap");
936			exitmain(1);
937		}
938		if (!MADVISE_OK) {
939			perror("madvise");
940			exitmain(1);
941		}
942#	endif
943	if (*a != CHAR1)
944		exitmain(1);
945	if (1 < argc) {
946		pid_t p, w;
947		int f = creat(argv[1], 0);
948		/*
949		* Some buggy hosts yield ETXTBSY if you try to use creat
950		* to truncate a file that is mmapped.  On such hosts,
951		* don't bother to try to figure out what mmap_signal is.
952		*/
953#		ifndef ETXTBSY
954#			define ETXTBSY (-1)
955#		endif
956		if (f<0 ? errno!=ETXTBSY : close(f)!=0) {
957			perror(argv[1]);
958			exitmain(1);
959		}
960		if ((p = fork()) < 0) {
961			perror("fork");
962			exitmain(1);
963		}
964		if (!p)
965			/* Refer to nonexistent storage, causing a signal in the child.  */
966			_exit(a[0] != 0);
967		while ((w = wait(&s)) != p)
968			if (w < 0) {
969				perror("wait");
970				exitmain(1);
971			}
972		s = WIFSIGNALED(s) ? WTERMSIG(s) : 0;
973	}
974#	if TRY_MAP_FD
975		kr = vm_deallocate(task_self(), va, (vm_size_t) b.st_size);
976		if (kr != KERN_SUCCESS) {
977			mach_error("vm_deallocate", kr);
978			exitmain(1);
979		}
980#	else
981		if (munmap(a, b.st_size)  !=  0) {
982			perror("munmap");
983			exitmain(1);
984		}
985#	endif
986	if (1 < argc) {
987#		ifdef SIGBUS
988			if (s == SIGBUS) { printf("SIGBUS\n"); s = 0; }
989#		endif
990#		ifdef SIGSEGV
991			if (s == SIGSEGV) { printf("SIGSEGV\n"); s = 0; }
992#		endif
993		if (s) printf("%d\n", s);
994	}
995	exitmain(ferror(stdout) || fclose(stdout)!=0);
996}
997EOF
998# AIX 3.2.0 read-only mmap updates last-modified time of file!  Check for this.
999sleep 2
1000cp a.c a.d || exit
1001sleep 2
1002has_map_fd=? has_mmap=? has_madvise=? mmap_signal=
1003case `(uname -s -r -v) 2>/dev/null` in
1004'HP-UX '[A-Z].08.07*) ;;
1005	# mmap can crash the OS under HP-UX 8.07, so don't even test for it.
1006'HP-UX '[A-Z].09.*) ;;
1007	# HP-UX 9.0[135]? s700 mmap has a data integrity problem
1008	# when a diskless cnode accesses data on the cnode's server disks.
1009	# We don't know of any way to test whether the bug is present.
1010	# HP patches PHKL_4605 and PHKL_4607 should fix the bug;
1011	# see <http://support.mayfield.hp.com/slx/html/ptc_hpux.html>.
1012	# The above code (perhaps rashly) assumes HP-UX 10 supports mmap.
1013'SunOS 5.4 Generic' | 'SunOS 5.4 Generic_101945-?') ;;
1014	# Early editions of SunOS 5.4 are reported to have problems with mmap
1015	# that generate NUL bytes in RCS files with a Solaris 2.2 NFS server.
1016	# This has been reported to be fixed as of patch 101945-10.
1017*)
1018	$PREPARE_CC || exit
1019	if ($CL -DTRY_MAP_FD=1 a.c $L && $aout <a.c) >&2
1020	then
1021		has_map_fd=1
1022	else
1023		has_map_fd=0 has_mmap=0 has_madvise=0
1024		if ($CL -DMADVISE_OK=1 a.c $L && $aout <a.c) >&2
1025		then
1026			case `ls -t a.c a.d` in
1027			a.d*)
1028				has_mmap=1
1029				rm -f a.ous
1030				mv $aout a.ous
1031				$PREPARE_CC || exit
1032				if ($CL a.c $L && $aout <a.c) >&2
1033				then has_madvise=1; rm -f a.ous
1034				else rm -f $aout && mv a.ous $aout
1035				fi || exit
1036			esac
1037		fi
1038	fi
1039	case $has_map_fd$has_mmap in
1040	*1*)
1041		# Find out what signal is sent to RCS
1042		# when someone unexpectedly truncates a file
1043		# while RCS has it mmapped.
1044		rm -f a.e && cp a.c a.e &&
1045		mmap_signal=`$aout a.e <a.e` || exit
1046	esac
1047esac
1048echo >&3 $has_map_fd, $has_mmap, $has_madvise, $mmap_signal
1049case $has_map_fd in
1050'?') a='/* ' z='*/ ';;
1051*) a= z=;;
1052esac
1053echo "$a#define has_map_fd $has_map_fd $z/* Does map_fd() work?  */"
1054case $has_mmap in
1055'?') a='/* ' z='*/ ';;
1056*) a= z=;;
1057esac
1058echo "$a#define has_mmap $has_mmap $z/* Does mmap() work on regular files?  */"
1059echo "$a#define has_madvise $has_madvise $z/* Does madvise() work?  */"
1060case $mmap_signal in
1061?*) a= z=;;
1062'') a='/* ' z='*/ ' mmap_signal='?'
1063esac
1064echo "$a#define mmap_signal $mmap_signal $z/* signal received if you reference nonexistent part of mmapped file */"
1065
1066$ech >&3 "$0: configuring has_rename, bad_a_rename, bad_b_rename $dots"
1067cat >a.c <<EOF
1068#include "$A_H"
1069int main() { exitmain(rename("a.a","a.b") != 0); }
1070EOF
1071echo a >a.a && $PREPARE_CC a.b || exit
1072if ($CL a.c $L && $aout && test -f a.b) >&2
1073then
1074	h=1
1075	rm -f a.a a.b &&
1076	echo a >a.a && chmod -w a.a || exit
1077	if $aout && test ! -f a.a && test -f a.b
1078	then a=0
1079	else a=1
1080	fi
1081	rm -f a.a a.b &&
1082	echo a >a.a && echo b >a.b && chmod -w a.b || exit
1083	if $aout && test ! -f a.a && test -f a.b
1084	then b=0
1085	else b=1
1086	fi
1087	rm -f a.a a.b || exit
1088else h=0 a=0 b=0
1089fi
1090echo >&3 $h, $a, $b
1091echo "#define has_rename $h /* Does rename() work?  */"
1092echo "#define bad_a_rename $a /* Does rename(A,B) fail if A is unwritable?  */"
1093echo "#define bad_b_rename $b /* Does rename(A,B) fail if B is unwritable?  */"
1094echo "#define bad_NFS_rename 0 /* Can rename(A,B) falsely report success?  */"
1095
1096$ech >&3 "$0: configuring void, VOID $dots"
1097cat >a.c <<EOF
1098#include "$A_H"
1099void f() {}
1100int main() {f(); exitmain(0);}
1101EOF
1102$PREPARE_CC || exit
1103if $CS a.c $LS >&2 && $CS_OK
1104then
1105	v='(void) '
1106	echo '/* typedef int void; */ /* Some ancient compilers need this.  */'
1107	ok=OK
1108else
1109	v=
1110	echo 'typedef int void;'
1111	ok='your compiler is a museum piece'
1112fi
1113echo >&3 $ok
1114echo "#define VOID $v/* 'VOID e;' discards the value of an expression 'e'.  */"
1115
1116case $has_getuid in
11170)
1118	a='/* ' z='*/ ' has_seteuid=?;;
1119*)
1120	$ech >&3 "$0: configuring has_seteuid $dots"
1121	a= z=
1122	cat >a.c <<EOF
1123#include "$A_H"
1124int
1125main() {
1126/* Guess, don't test.  Ugh.  Testing would require running conf.sh setuid.  */
1127/* If the guess is wrong, a setuid RCS will detect the problem at runtime.  */
1128#if !_POSIX_VERSION
1129	exitmain(1);
1130#else
1131	exitmain(seteuid(geteuid()) != 0);
1132#endif
1133}
1134EOF
1135	$PREPARE_CC || exit
1136	if ($CL a.c $L && $aout) >&2
1137	then has_seteuid=1 ok='OK, I guess'
1138	else has_seteuid=0 ok='does not work'
1139	fi
1140	echo >&3 $ok
1141esac
1142echo "$a#define has_seteuid $has_seteuid $z/* Does seteuid() work?  See ../INSTALL.RCS.  */"
1143
1144echo "#define has_setreuid 0 /* Does setreuid() work?  See ../INSTALL.RCS.  */"
1145
1146$ech >&3 "$0: configuring has_setuid $dots"
1147h=$has_seteuid
1148case $h in
11490)
1150	cat >a.c <<EOF
1151#include "$A_H"
1152int main() { exitmain(setuid(getuid()) != 0); }
1153EOF
1154	$PREPARE_CC || exit
1155	($CL a.c $L && $aout) >&2 && h=1 ok='OK, I guess'
1156esac
1157echo >&3 $ok
1158echo "$a#define has_setuid $h $z/* Does setuid() exist?  */"
1159
1160$ech >&3 "$0: configuring has_sigaction $dots"
1161cat >a.c <<EOF
1162#include "$A_H"
1163static sig_atomic_t volatile gotsig;
1164#ifdef SA_SIGINFO
1165  static void catchsig(i, s, v) int i; siginfo_t *s; void *v; { gotsig = 1; }
1166#else
1167  static void catchsig(i) int i; { gotsig = 1; }
1168#endif
1169int
1170main(argc, argv) int argc; char **argv; {
1171	struct sigaction s;
1172	if (sigaction(SIGINT, (struct sigaction*)0, &s) != 0)
1173		exitmain(1);
1174#	if has_sa_sigaction
1175		s.sa_sigaction = catchsig;
1176#	else
1177		s.sa_handler = catchsig;
1178#	endif
1179#	ifdef SA_SIGINFO
1180		s.sa_flags |= SA_SIGINFO;
1181#	endif
1182	if (sigaddset(&s.sa_mask, SIGINT) != 0)
1183		exitmain(1);
1184	if (sigaction(SIGINT, &s, (struct sigaction*)0) != 0)
1185		exitmain(1);
1186#	if has_kill
1187		kill(getpid(), SIGINT);
1188#	else
1189		raise(SIGINT);
1190#	endif
1191	exitmain(gotsig != 1);
1192}
1193EOF
1194$PREPARE_CC || exit
1195if ($CL -Dhas_kill=$has_kill a.c $L && $aout) >&2
1196then has_sigaction=1 ok=OK
1197else has_sigaction=0 ok='does not work'
1198fi
1199echo >&3 $ok
1200echo "#define has_sigaction $has_sigaction /* Does struct sigaction work?  */"
1201$ech >&3 "$0: configuring has_sa_sigaction $dots"
1202has_sa_sigaction=0 ok='does not work'
1203case $has_sigaction in
12041)
1205	$PREPARE_CC || exit
1206	if ($CL -Dhas_kill=$has_kill -Dhas_sa_sigaction=1 a.c $L && $aout) >&2
1207	then has_sa_sigaction=1 ok=OK
1208	fi
1209esac
1210echo >&3 $ok
1211echo "#define has_sa_sigaction $has_sa_sigaction /* Does struct sigaction have sa_sigaction?  */"
1212
1213$ech >&3 "$0: configuring has_signal, signal_type, sig_zaps_handler $dots"
1214case $has_signal,$has_sigaction in
12151,0)
1216	cat >a.c <<EOF
1217#include "$A_H"
1218#if !defined(signal) && declare_signal
1219	signal_type (*signal P((int,signal_type(*)signal_args)))signal_args;
1220#endif
1221static signal_type nothing(i) int i; {}
1222int
1223main(argc, argv) int argc; char **argv; {
1224	signal(SIGINT, nothing);
1225#	if has_kill
1226		while (--argc)
1227			kill(getpid(), SIGINT);
1228		exitmain(0);
1229#	else
1230		/* Pretend that sig_zaps_handler; better safe than sorry.  */
1231		exitmain(2 < argc);
1232#	endif
1233}
1234EOF
1235	for declare_signal in 1 0
1236	do
1237		for signal_type in void int
1238		do
1239			for signal_args in 'P((int))' '()'
1240			do
1241				$PREPARE_CC || exit
1242				($CL \
1243					-Ddeclare_signal=$declare_signal \
1244					-Dhas_kill=$has_kill \
1245					-Dsignal_args="$signal_args" \
1246					-Dsignal_type=$signal_type \
1247						a.c $L && $aout 1) >&2 && break
1248			done && break
1249		done && break
1250	done || {
1251		echo >&3 $0: cannot deduce signal_type
1252		exit 1
1253	}
1254	if $aout 1 2 >&2
1255	then sig_zaps_handler=0
1256	else sig_zaps_handler=1
1257	fi;;
1258*)
1259	signal_type=void
1260	sig_zaps_handler=0
1261esac
1262echo >&3 $has_signal, $signal_type, $sig_zaps_handler
1263cat <<EOF
1264#define has_signal $has_signal /* Does signal() work?  */
1265#define signal_type $signal_type /* type returned by signal handlers */
1266#define sig_zaps_handler $sig_zaps_handler /* Must a signal handler reinvoke signal()?  */
1267EOF
1268
1269a='/* ' z='*/ '
1270b='/* ' y='*/ '
1271case $has_sigaction in
12721)
1273	h=?;;
1274*)
1275	$ech >&3 "$0: configuring has_sigblock, sigmask $dots"
1276	ok=OK
1277	a= z=
1278	cat >a.c <<EOF
1279#include "$A_H"
1280#include <signal.h>
1281#if define_sigmask
1282#	define sigmask(s) (1 << ((s)-1))
1283#endif
1284int
1285main() {
1286	sigblock(sigmask(SIGHUP));
1287#	if has_kill
1288		exitmain(kill(getpid(), SIGHUP) != 0);
1289#	else
1290		exitmain(raise(SIGHUP) != 0);
1291#	endif
1292}
1293EOF
1294	if
1295		$PREPARE_CC || exit
1296		($CL -Dhas_kill=$has_kill a.c $L && $aout) >&2
1297	then h=1
1298	elif
1299		$PREPARE_CC || exit
1300		($CL -Dhas_kill=$has_kill -Ddefine_sigmask=1 a.c $L && $aout) >&2
1301	then h=1 b= y= ok='definition needed'
1302	else h=0
1303	fi
1304	echo >&3 "$h, $ok"
1305esac
1306echo "$a#define has_sigblock $h $z/* Does sigblock() work?  */"
1307echo "$b#define sigmask(s) (1 << ((s)-1)) $y/* Yield mask for signal number.  */"
1308
1309$ech >&3 "$0: configuring fread_type, freadarg_type $dots"
1310cat >a.c <<EOF
1311#define CHAR1 '#' /* the first character in this file */
1312#include "$A_H"
1313#if !defined(fread) && declare_fread
1314	fread_type fread P((void*,freadarg_type,freadarg_type,FILE*));
1315#endif
1316int
1317main() {
1318	char b;
1319	exitmain(!(
1320		fread(&b, (freadarg_type)1, (freadarg_type)1, stdin) == 1  &&
1321		b==CHAR1
1322	));
1323}
1324EOF
1325for declare_fread in 1 0
1326do
1327	for fread_type in ssize_t size_t int unsigned
1328	do
1329		for freadarg_type in size_t ssize_t unsigned int
1330		do
1331			$PREPARE_CC || exit
1332			(
1333				$CL \
1334					-Ddeclare_fread=$declare_fread \
1335					-Dfreadarg_type=$freadarg_type \
1336					-Dfread_type=$fread_type \
1337					a.c $L &&
1338				$aout <a.c
1339			) >&2 && break
1340		done && break
1341	done && break
1342done || {
1343	echo >&3 $0: cannot deduce fread types
1344	exit 1
1345}
1346echo >&3 $fread_type, $freadarg_type
1347cat <<EOF
1348typedef $fread_type fread_type; /* type returned by fread() and fwrite() */
1349typedef $freadarg_type freadarg_type; /* type of their size arguments */
1350EOF
1351
1352$ech >&3 "$0: configuring malloc_type $dots"
1353cat >a.c <<EOF
1354#include "$A_H"
1355typedef void *malloc_type;
1356#ifndef malloc
1357	malloc_type malloc();
1358#endif
1359static malloc_type identity P((malloc_type));
1360static malloc_type identity(x) malloc_type x; { return x; }
1361int main() { exitmain(!identity(malloc(1))); }
1362EOF
1363$PREPARE_CC || exit
1364if $CS a.c $LS >&2 && $CS_OK
1365then t=void
1366else t=char
1367fi
1368echo >&3 $t
1369echo "typedef $t *malloc_type; /* type returned by malloc() */"
1370
1371$ech >&3 "$0: configuring has_getcwd $dots"
1372cat >a.c <<EOF
1373#include "$A_H"
1374#ifndef getcwd
1375	char *getcwd();
1376#endif
1377static char buf[10000];
1378int main() { exitmain(!getcwd(buf,10000)); }
1379EOF
1380$PREPARE_CC || exit
1381if ($CL a.c $L && $aout) >&2
1382then has_getcwd=1 ok=OK
1383else has_getcwd=0 ok='does not work'
1384fi
1385echo >&3 $ok
1386echo "#define has_getcwd $has_getcwd /* Does getcwd() work?  */"
1387
1388case $has_getcwd in
13891)
1390	a='/* ' z='*/ ' h=?;;
1391*)
1392	a= z=
1393	$ech >&3 "$0: configuring has_getwd $dots"
1394	cat >a.c <<EOF
1395#include "$A_H"
1396#include <sys/param.h>
1397#ifndef getwd
1398	char *getwd();
1399#endif
1400static char buf[MAXPATHLEN];
1401int main() { exitmain(!getwd(buf)); }
1402EOF
1403	$PREPARE_CC || exit
1404	if ($CL a.c $L && $aout) >&2
1405	then h=1 ok=OK
1406	else h=0 ok='does not work'
1407	fi
1408	echo >&3 $ok
1409esac
1410echo "$a#define has_getwd $h $z/* Does getwd() work?  */"
1411echo "#define needs_getabsname 0 /* Must we define getabsname?  */"
1412
1413$ech >&3 "$0: configuring has_mktemp $dots"
1414cat >a.c <<EOF
1415#include "$A_H"
1416#ifndef mktemp
1417	char *mktemp();
1418#endif
1419int
1420main() {
1421	char b[9];
1422	strcpy(b, "a.XXXXXX");
1423	exitmain(!mktemp(b));
1424}
1425EOF
1426$PREPARE_CC || exit
1427if ($CL a.c $L && $aout) >&2
1428then h=1 ok=OK
1429else h=0 ok=absent
1430fi
1431echo >&3 $ok
1432echo "#define has_mktemp $h /* Does mktemp() work?  */"
1433
1434: configuring has_NFS
1435echo "#define has_NFS 1 /* Might NFS be used?  */"
1436
1437case $has_signal,$has_sigaction in
14381,0)
1439	has_psiginfo=0;;
1440*)
1441	$ech >&3 "$0: configuring has_psiginfo $dots"
1442	cat >a.c <<EOF
1443#include "$A_H"
1444static signal_type
1445catchsig(s, i, c) int s; siginfo_t *i; void *c; {
1446	if (i)
1447		psiginfo(i, "test");
1448	exit(0);
1449}
1450int
1451main() {
1452	struct sigaction s;
1453	if (sigaction(SIGINT, (struct sigaction*)0, &s) != 0)
1454		exitmain(1);
1455#	if has_sa_sigaction
1456		s.sa_sigaction = catchsig;
1457#	else
1458		s.sa_handler = catchsig;
1459#	endif
1460	if (sigaddset(&s.sa_mask, SIGINT) != 0)
1461		exitmain(1);
1462	s.sa_flags |= SA_SIGINFO;
1463	if (sigaction(SIGINT, &s, (struct sigaction*)0) != 0)
1464		exitmain(1);
1465#	if has_kill
1466		kill(getpid(), SIGINT);
1467#	else
1468		raise(SIGINT);
1469#	endif
1470	exitmain(1);
1471}
1472EOF
1473	$PREPARE_CC || exit
1474	if ($CL a.c $L && $aout) >&2
1475	then has_psiginfo=1 ok=OK
1476	else has_psiginfo=0 ok=absent
1477	fi
1478	echo >&3 $ok
1479esac
1480echo "#define has_psiginfo $has_psiginfo /* Does psiginfo() work?  */"
1481
1482case $has_signal in
14831)
1484	$ech >&3 "$0: configuring has_psignal $dots"
1485	cat >a.c <<EOF
1486#include "$A_H"
1487int main() { psignal(SIGINT, ""); exitmain(0); }
1488EOF
1489	$PREPARE_CC || exit
1490	if ($CL a.c $L && $aout) >&2
1491	then has_psignal=1 ok=OK
1492	else has_psignal=0 ok=absent
1493	fi
1494	echo >&3 $ok;;
1495*)	has_psignal=0
1496esac
1497echo "#define has_psignal $has_psignal /* Does psignal() work?  */"
1498
1499case $has_psiginfo in
15001)
1501	$ech >&3 "$0: configuring has_si_errno $dots"
1502	cat >a.c <<EOF
1503#include "$A_H"
1504siginfo_t a;
1505int main() { exitmain(a.si_errno); }
1506EOF
1507	$PREPARE_CC || exit
1508	if $CS a.c $LS >&2 && $CS_OK
1509	then h=1 ok=OK
1510	else h=0 ok=absent
1511	fi
1512	echo >&3 $ok
1513	a= z=;;
1514*)	h=? a='/* ' z='*/ '
1515esac
1516echo "$a#define has_si_errno $h $z/* Does siginfo_t have si_errno?  */"
1517
1518case $has_signal,$has_psignal in
15191,0)
1520	$ech >&3 "$0: configuring has_sys_siglist $dots"
1521	cat >a.c <<EOF
1522#include "$A_H"
1523#if !defined(sys_siglist) && declare_sys_siglist
1524	extern char const * const sys_siglist[];
1525#endif
1526int main() { exitmain(!sys_siglist[1][0]); }
1527EOF
1528	$PREPARE_CC || exit
1529	h=0 ok=absent
1530	for d in 1 0
1531	do ($CL -Ddeclare_sys_siglist=$d a.c $L && $aout) >&2 &&
1532		h=1 && ok=OK && break
1533	done
1534	echo >&3 $ok
1535	a= z=;;
1536*)	h=? a='/* ' z='*/ '
1537esac
1538echo "$a#define has_sys_siglist $h $z/* Does sys_siglist[] work?  */"
1539
1540$ech >&3 "$0: configuring strchr $dots"
1541cat >a.c <<EOF
1542#include "$A_H"
1543#ifndef strchr
1544	char *strchr();
1545#endif
1546int main() {exitmain(!strchr("abc", 'c'));}
1547EOF
1548$PREPARE_CC || exit
1549if ($CL a.c $L && $aout) >&2
1550then a='/* ' z='*/ ' ok=OK
1551else a= z= ok='does not work'
1552fi
1553echo >&3 $ok
1554echo "$a#define strchr index $z/* Use old-fashioned name for strchr()?  */"
1555
1556$ech >&3 "$0: configuring strrchr $dots"
1557cat >a.c <<EOF
1558#include "$A_H"
1559#ifndef strrchr
1560	char *strrchr();
1561#endif
1562int main() {exitmain(!strrchr("abc", 'c'));}
1563EOF
1564$PREPARE_CC || exit
1565if ($CL a.c $L && $aout) >&2
1566then a='/* ' z='*/ ' ok=OK
1567else a= z= ok='does not work'
1568fi
1569echo >&3 $ok
1570echo "$a#define strrchr rindex $z/* Use old-fashioned name for strrchr()?  */"
1571
1572$ech >&3 "$0: configuring bad_unlink $dots"
1573cat >a.c <<EOF
1574#include "$A_H"
1575int main() { exitmain(unlink("a.c") != 0); }
1576EOF
1577$PREPARE_CC && chmod -w a.c || exit
1578if ($CL a.c $L && $aout) >&2 && test ! -f a.c
1579then b=0 ok=OK
1580else b=1 ok='will work around bug'
1581fi
1582rm -f a.c || exit
1583echo >&3 $ok
1584echo "#define bad_unlink $b /* Does unlink() fail on unwritable files?  */"
1585
1586$ech >&3 "$0: configuring has_vfork $dots"
1587cat >a.c <<EOF
1588#include "$A_H"
1589#ifndef STDOUT_FILENO
1590#define STDOUT_FILENO 1
1591#endif
1592#if !TRY_VFORK
1593#	undef vfork
1594#	define vfork fork
1595#endif
1596#if !TRY_WAITPID
1597#	undef waitpid
1598#	define waitpid(p,s,o) wait(s)
1599#endif
1600
1601int
1602main() {
1603	pid_t parent = getpid();
1604	pid_t child = vfork();
1605
1606	if (child == 0) {
1607		/*
1608		 * On sparc systems, changes by the child to local and incoming
1609		 * argument registers are propagated back to the parent.
1610		 * The compiler is told about this with #include <vfork.h>,
1611		 * but some compilers (e.g. gcc -O) don't grok <vfork.h>.
1612		 * Test for this by using lots of local variables, at least
1613		 * as many local variables as 'main' has allocated so far
1614		 * including compiler temporaries.  4 locals are enough for
1615		 * gcc 1.40.3 on a sparc, but we use 8 to be safe.
1616		 * A buggy compiler should reuse the register of 'parent'
1617		 * for one of the local variables, since it will think that
1618		 * 'parent' can't possibly be used any more in this routine.
1619		 * Assigning to the local variable will thus munge 'parent'
1620		 * in the parent process.
1621		 */
1622		pid_t
1623			p = getpid(),
1624			p1 = getpid(), p2 = getpid(),
1625			p3 = getpid(), p4 = getpid(),
1626			p5 = getpid(), p6 = getpid(),
1627			p7 = getpid();
1628		/*
1629		 * Convince the compiler that p..p7 are live; otherwise, it might
1630		 * use the same hardware register for all 8 local variables.
1631		 */
1632		if (p!=p1 || p!=p2 || p!=p3 || p!=p4 || p!=p5 || p!=p6 || p!=p7)
1633			_exit(1);
1634
1635		/*
1636		 * On some systems (e.g. IRIX 3.3),
1637		 * vfork doesn't separate parent from child file descriptors.
1638		 * If the child closes a descriptor before it execs or exits,
1639		 * this munges the parent's descriptor as well.
1640		 * Test for this by closing stdout in the child.
1641		 */
1642		_exit(close(STDOUT_FILENO) != 0);
1643
1644	} else {
1645		int status;
1646		struct stat st;
1647		exit(
1648			/* Was there some problem with vforking?  */
1649			child < 0
1650
1651			/* Was there some problem in waiting for the child?  */
1652			|| waitpid(child, &status, 0) != child
1653
1654			/* Did the child fail?  (This shouldn't happen.)  */
1655			|| status
1656
1657			/* Did the vfork/compiler bug occur?  */
1658			|| parent != getpid()
1659
1660			/* Did the file descriptor bug occur?  */
1661			|| fstat(STDOUT_FILENO, &st) != 0
1662		);
1663	}
1664}
1665EOF
1666$PREPARE_CC || exit
1667if ($CL -DTRY_VFORK=1 a.c $L && $aout) >&2
1668then has_vfork=1 ok=OK
1669else has_vfork=0 ok='absent or broken'
1670fi
1671echo >&3 $ok
1672echo "#define has_vfork $has_vfork /* Does vfork() work?  */"
1673h=$has_vfork
1674case $h in
16750)
1676	$ech >&3 "$0: configuring has_fork $dots"
1677	$PREPARE_CC || exit
1678	ok='does not work'
1679	($CL a.c $L && $aout) >&2 && h=1 ok=OK
1680	echo >&3 $ok
1681esac
1682echo "#define has_fork $h /* Does fork() work?  */"
1683$PREPARE_CC || exit
1684$ech >&3 "$0: configuring has_waitpid $dots"
1685if ($CL -DTRY_VFORK=$has_vfork -DTRY_WAITPID=1 a.c $L && $aout) >&2
1686then h=1 ok=OK
1687else h=0 ok='does not work'
1688fi
1689echo >&3 $ok
1690echo "#define has_spawn 0 /* Does spawn*() work?  */"
1691echo "#define has_waitpid $h /* Does waitpid() work?  */"
1692
1693$ech >&3 "$0: configuring bad_wait_if_SIGCHLD_ignored $dots"
1694cat >a.c <<EOF
1695#include "$A_H"
1696#ifndef SIGCHLD
1697#define SIGCHLD SIGCLD
1698#endif
1699int main() {
1700	signal(SIGCHLD, SIG_IGN);
1701	{
1702#	if has_fork
1703		int status;
1704		pid_t p = fork();
1705		if (p < 0) {
1706			perror("fork");
1707			exitmain(2);
1708		}
1709		if (p == 0)
1710			_exit(0);
1711		while (wait(&status) != p) {
1712			if (errno == ECHILD)
1713				exitmain(1);
1714			if (errno != EINTR) {
1715				perror("wait");
1716				exitmain(2);
1717			}
1718		}
1719#	else
1720#		if has_system
1721			if (system("true") != 0)
1722				exitmain(1);
1723#		endif
1724#	endif
1725	}
1726	exitmain(0);
1727}
1728EOF
1729$PREPARE_CC || exit
1730b=0 ok=OK
1731if $CL a.c $L >&2
1732then
1733	$aout >&2
1734	case $? in
1735	0) ;;
1736	1) b=1 ok='will work around bug';;
1737	*) exit
1738	esac
1739fi
1740rm -f a.c || exit
1741echo >&3 $ok
1742echo "#define bad_wait_if_SIGCHLD_ignored $b /* Does ignoring SIGCHLD break wait()?  */"
1743
1744
1745echo '#define RCS_SHELL "/bin/sh" /* shell to run RCS subprograms */'
1746
1747$ech >&3 "$0: configuring has_printf_dot $dots"
1748cat >a.c <<EOF
1749#include "$A_H"
1750int main() { printf("%.2d", 1); exitmain(ferror(stdout) || fclose(stdout)!=0); }
1751EOF
1752$PREPARE_CC && $CL a.c $L >&2 && r=`$aout` || exit
1753case $r in
175401)	h=1 ok=OK;;
1755*)	h=0 ok='does not work'
1756esac
1757echo >&3 $ok
1758echo "#define has_printf_dot $h /* Does \"%.2d\" print leading 0?  */"
1759
1760$ech >&3 "$0: configuring has_vfprintf, has_attribute_format_printf $dots"
1761cat >a.c <<EOF
1762#include "$A_H"
1763#if has_attribute_format_printf
1764#	define printf_string(m, n) __attribute__((format(printf, m, n)))
1765#else
1766#	define printf_string(m, n)
1767#endif
1768int p P((char const*,...)) printf_string(1, 2);
1769#if has_prototypes
1770int p(char const*format,...)
1771#else
1772/*VARARGS1*/ int p(format, va_alist) char *format; va_dcl
1773#endif
1774{
1775	int r;
1776	va_list args;
1777	vararg_start(args, format);
1778	r = vfprintf(stderr, format, args);
1779	va_end(args);
1780	return r;
1781}
1782int main() { exitmain(p("hello") != 5); }
1783EOF
1784$PREPARE_CC || exit
1785h=0 p=0
1786if ($CL a.c $L && sh -c 'pid=$$; (sleep 3; kill $pid)& exec '$aout) >&2
1787then
1788	h=1
1789	$PREPARE_CC || exit
1790	$CS -Dhas_attribute_format_printf=1 a.c >&2 && $CS_OK && p=1
1791else
1792	status=$?
1793	sh -c 'pid=$$; (sleep 3; kill $pid)& exec sleep 6' >&2
1794	if test $? = $status
1795	then
1796		echo >&3 "$0: stdio library loops forever.  Giving up.
1797$0: (Perhaps you are using Solaris 2.x /usr/ucb/cc?)
1798$0: Please use a working stdio library instead."
1799		exit 1
1800	fi
1801fi
1802echo >&3 $h, $p
1803cat <<EOF
1804#define has_vfprintf $h /* Does vfprintf() work?  */
1805#define has_attribute_format_printf $p /* Does __attribute__((format(printf,N,N+1))) work?  */
1806#if has_attribute_format_printf
1807#	define printf_string(m, n) __attribute__((format(printf, m, n)))
1808#else
1809#	define printf_string(m, n)
1810#endif
1811#if has_attribute_format_printf && has_attribute_noreturn
1812	/* Work around a bug in GCC 2.5.x.  */
1813#	define printf_string_exiting(m, n) __attribute__((format(printf, m, n), noreturn))
1814#else
1815#	define printf_string_exiting(m, n) printf_string(m, n) exiting
1816#endif
1817EOF
1818
1819case $h in
18201)
1821	h=? a='/* ' z='*/ ';;
1822*)
1823	$ech >&3 "$0: configuring has__doprintf $dots"
1824	a= z=
1825	cat >a.c <<EOF
1826#include "$A_H"
1827#if has_prototypes
1828static int
1829p(char const*format,...)
1830#else
1831/*VARARGS1*/ static int p(format, va_alist) char *format; va_dcl
1832#endif
1833{
1834	va_list args;
1835	vararg_start(args, format);
1836#	if TRY__DOPRINTF
1837		_doprintf(stderr, format, args);
1838#	else
1839		_doprnt(format, args, stderr);
1840#	endif
1841	va_end(args);
1842}
1843int main() { p(""); exitmain(0); }
1844EOF
1845	$PREPARE_CC || exit
1846	if ($CL -DTRY__DOPRINTF=1 a.c $L && $aout) >&2
1847	then h=1 ok=OK
1848	else h=0 ok='does not work'
1849	fi
1850	echo >&3 $ok
1851esac
1852echo "$a#define has__doprintf $h $z/* Does _doprintf() work?  */"
1853case $h in
18540)
1855	$ech >&3 "$0: configuring has__doprnt $dots"
1856	$PREPARE_CC || exit
1857	if ($CL a.c $L && $aout) >&2
1858	then h=1 ok=OK
1859	else h=0 ok='does not work'
1860	fi
1861	echo >&3 $ok
1862	a= z=;;
1863*)
1864	h=? a='/* ' z='*/ '
1865esac
1866echo "$a#define has__doprnt $h $z/* Does _doprnt() work?  */"
1867
1868$ech >&3 "$0: configuring EXIT_FAILURE $dots"
1869cat >a.c <<EOF
1870#include "$A_H"
1871int main() { exitmain(EXIT_FAILURE); }
1872EOF
1873$PREPARE_CC || exit
1874if $CL a.c $L >&2 && $aout
1875then a= z= ok='will work around bug'
1876else a='/* ' z='*/ ' ok=OK
1877fi
1878echo >&3 $ok
1879echo "$a#undef EXIT_FAILURE $z/* Uncomment this if EXIT_FAILURE is broken.  */"
1880
1881: configuring large_memory
1882case "$has_map_fd$has_mmap" in
1883*1*) l=1;;
1884*) l=0
1885esac
1886echo "#define large_memory $l /* Can main memory hold entire RCS files?  */"
1887
1888$ech >&3 "$0: configuring LONG_MAX $dots"
1889cat >a.c <<EOF
1890#include "$A_H"
1891static unsigned long ulong_max;
1892static long long_max;
1893int
1894main() {
1895	ulong_max--;
1896	long_max = ulong_max >> 1;
1897	printf("#ifndef LONG_MAX\n");
1898	printf("#define LONG_MAX %ldL /* long maximum */\n", long_max);
1899	printf("#endif\n");
1900	exitmain(ferror(stdout) || fclose(stdout)!=0);
1901}
1902EOF
1903$PREPARE_CC && $CL a.c $L >&2 && $aout || exit
1904echo >&3 OK
1905
1906: configuring same_file
1907echo "/* Do struct stat s and t describe the same file?  Answer d if unknown.  */"
1908echo "#define same_file(s,t,d) ((s).st_ino==(t).st_ino && (s).st_dev==(t).st_dev)"
1909
1910$ech >&3 "$0: configuring struct utimbuf $dots"
1911cat >a.c <<EOF
1912#include "$A_H"
1913static struct utimbuf s;
1914int main() { s.actime = s.modtime = 1; exitmain(utime("a.c", &s) != 0); }
1915EOF
1916$PREPARE_CC || exit
1917if ($CL a.c $L && $aout) >&2
1918then h=1 ok=OK
1919else h=0 ok='does not work'
1920fi
1921echo >&3 $ok
1922echo "#define has_utimbuf $h /* Does struct utimbuf work?  */"
1923
1924: configuring CO
1925echo "#define CO \"${RCSPREFIX}co\" /* name of 'co' program */"
1926
1927: configuring COMPAT2
1928echo "#define COMPAT2 $COMPAT2 /* Are version 2 files supported?  */"
1929
1930: configuring DIFF
1931echo "#define DIFF \"${DIFF}\" /* name of 'diff' program */"
1932
1933: configuring DIFF3
1934echo "#define DIFF3 \"${DIFF3}\" /* name of 'diff3' program */"
1935
1936: configuring DIFF3_BIN
1937echo "#define DIFF3_BIN $DIFF3_BIN /* Is diff3 user-visible (not the /usr/lib auxiliary)?  */"
1938
1939: configuring DIFFFLAGS
1940echo "#define DIFFFLAGS \"$DIFFFLAGS\" /* Make diff output suitable for RCS.  */"
1941
1942: configuring DIFF_L
1943echo "#define DIFF_L $DIFF_L /* Does diff -L work?  */"
1944
1945: configuring DIFF_SUCCESS, DIFF_FAILURE, DIFF_TROUBLE
1946cat <<EOF
1947#define DIFF_SUCCESS $DIFF_SUCCESS /* DIFF status if no differences are found */
1948#define DIFF_FAILURE $DIFF_FAILURE /* DIFF status if differences are found */
1949#define DIFF_TROUBLE $DIFF_TROUBLE /* DIFF status if trouble */
1950EOF
1951
1952: configuring ED
1953echo "#define ED \"${ED}\" /* name of 'ed' program (used only if !DIFF3_BIN) */"
1954
1955: configuring MERGE
1956echo "#define MERGE \"${RCSPREFIX}merge\" /* name of 'merge' program */"
1957
1958: configuring '*SLASH*', ROOTPATH, TMPDIR, X_DEFAULT
1959case ${PWD-`pwd`} in
1960/*) # Posix
1961	SLASH=/
1962	qSLASH="'/'"
1963	SLASHes=$qSLASH
1964	isSLASH='#define isSLASH(c) ((c) == SLASH)'
1965	ROOTPATH='isSLASH((p)[0])'
1966	X_DEFAULT=",v$SLASH";;
1967?:[/\\\\]*) # MS-DOS # \\\\ instead of \\ doesn't hurt, and avoids common bugs
1968	SLASH='\'
1969	qSLASH="'\\\\'"
1970	SLASHes="$qSLASH: case '/': case ':'"
1971	isSLASH='int isSLASH P((int));'
1972	ROOTPATH="(isSLASH((p)[0]) || (p)[0] && (p)[1]==':')"
1973	X_DEFAULT="$SLASH,v";;
1974*)
1975	echo >&3 $0: cannot deduce SLASH
1976	exit 1
1977esac
1978cat <<EOF
1979#define TMPDIR "${SLASH}tmp" /* default directory for temporary files */
1980#define SLASH $qSLASH /* principal filename separator */
1981#define SLASHes $SLASHes /* \`case SLASHes:' labels all filename separators */
1982$isSLASH /* Is arg a filename separator?  */
1983#define ROOTPATH(p) $ROOTPATH /* Is p an absolute pathname?  */
1984#define X_DEFAULT "$X_DEFAULT" /* default value for -x option */
1985EOF
1986
1987$ech >&3 "$0: configuring SLASHSLASH_is_SLASH $dots"
1988cat >a.c <<EOF
1989#include "$A_H"
1990static struct stat s, ss;
1991static char f[3];
1992int
1993main() {
1994	f[0] = SLASH; if (stat(f, &s ) != 0) exitmain(1);
1995	f[1] = SLASH; if (stat(f, &ss) != 0) exitmain(1);
1996	exitmain(!same_file(s, ss, 0));
1997}
1998EOF
1999$PREPARE_CC || exit
2000if ($CL a.c $L && $aout) >&2
2001then eq=1 ok=OK
2002else eq=0 ok=no
2003fi
2004echo >&3 $ok
2005echo "#define SLASHSLASH_is_SLASH $eq /* Are // and / the same directory?  */"
2006
2007$ech >&3 "$0: configuring ALL_ABSOLUTE, DIFF_ABSOLUTE $dots"
2008cat >a.c <<EOF
2009#include "$A_H"
2010#ifndef isSLASH
2011static int
2012isSLASH(c) int c; {
2013	switch (c) { case SLASHes: return 1; } return 0;
2014}
2015#endif
2016int
2017main(argc, argv) int argc; char **argv; {
2018	exitmain(1<argc && !ROOTPATH(argv[1]));
2019}
2020EOF
2021$PREPARE_CC && ($CL a.c $L && $aout) >&2 || exit
2022a=1
2023for i in "$DIFF" "$DIFF3" "$ED" "$RCSPREFIX" "$SENDMAIL"
2024do
2025	case $i in
2026	\"*\") i=`expr "$i" : '"\(.*\)"'`
2027	esac
2028	case $i in
2029	?*) $aout "$i" || { a=0; break; }
2030	esac
2031done
2032echo "#define ALL_ABSOLUTE $a /* Do all subprograms satisfy ROOTPATH?  */"
2033if $aout "$DIFF"
2034then a=1
2035else a=0
2036fi
2037echo "#define DIFF_ABSOLUTE $a /* Is ROOTPATH(DIFF) true?  */"
2038echo >&3 OK
2039
2040: configuring SENDMAIL
2041case $SENDMAIL in
2042'') a='/* ' z='*/ ';;
2043*) a= z=
2044esac
2045echo "$a#define SENDMAIL $SENDMAIL $z/* how to send mail */"
2046
2047: configuring TZ_must_be_set
2048echo "#define TZ_must_be_set 0 /* Must TZ be set for gmtime() to work?  */"
2049
2050
2051$ech >&3 "$0: configuring standard library declarations $dots"
2052
2053cat <<'EOF'
2054
2055
2056
2057/* Adjust the following declarations as needed.  */
2058EOF
2059
2060cat >a.ha <<EOF
2061
2062
2063/* The rest is for the benefit of non-standard, traditional hosts.  */
2064/* Don't bother to declare functions that in traditional hosts do not appear, */
2065/* or are declared in .h files, or return int or void.  */
2066
2067
2068/* traditional BSD */
2069
2070#if has_sys_siglist && !defined(sys_siglist)
2071	extern char const * const sys_siglist[];
2072#endif
2073
2074
2075/* Posix (ISO/IEC 9945-1: 1990 / IEEE Std 1003.1-1990) */
2076
2077/* <fcntl.h> */
2078#ifdef O_CREAT
2079#	define open_can_creat 1
2080#else
2081#	define open_can_creat 0
2082#	define O_RDONLY 0
2083#	define O_WRONLY 1
2084#	define O_RDWR 2
2085#	define O_CREAT 01000
2086#	define O_TRUNC 02000
2087#endif
2088#ifndef O_EXCL
2089#define O_EXCL 0
2090#endif
2091
2092/* <sys/stat.h> */
2093#ifndef S_IRUSR
2094#	ifdef S_IREAD
2095#		define S_IRUSR S_IREAD
2096#	else
2097#		define S_IRUSR 0400
2098#	endif
2099#	ifdef S_IWRITE
2100#		define S_IWUSR S_IWRITE
2101#	else
2102#		define S_IWUSR (S_IRUSR/2)
2103#	endif
2104#endif
2105#ifndef S_IRGRP
2106#	if has_getuid
2107#		define S_IRGRP (S_IRUSR / 0010)
2108#		define S_IWGRP (S_IWUSR / 0010)
2109#		define S_IROTH (S_IRUSR / 0100)
2110#		define S_IWOTH (S_IWUSR / 0100)
2111#	else
2112		/* single user OS -- not Posix or Unix */
2113#		define S_IRGRP 0
2114#		define S_IWGRP 0
2115#		define S_IROTH 0
2116#		define S_IWOTH 0
2117#	endif
2118#endif
2119#ifndef S_ISREG
2120#define S_ISREG(n) (((n) & S_IFMT) == S_IFREG)
2121#endif
2122
2123/* <sys/wait.h> */
2124#ifndef WEXITSTATUS
2125#define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
2126#undef WIFEXITED /* Avoid 4.3BSD incompatibility with Posix.  */
2127#endif
2128#ifndef WIFEXITED
2129#define WIFEXITED(stat_val) (((stat_val)  &  0377) == 0)
2130#endif
2131#ifndef WTERMSIG
2132#define WTERMSIG(stat_val) ((stat_val) & 0177)
2133#undef WIFSIGNALED /* Avoid 4.3BSD incompatibility with Posix.  */
2134#endif
2135#ifndef WIFSIGNALED
2136#define WIFSIGNALED(stat_val) ((unsigned)(stat_val) - 1  <  0377)
2137#endif
2138
2139/* <unistd.h> */
2140char *getlogin P((void));
2141#ifndef STDIN_FILENO
2142#	define STDIN_FILENO 0
2143#	define STDOUT_FILENO 1
2144#	define STDERR_FILENO 2
2145#endif
2146#if has_fork && !has_vfork
2147#	undef vfork
2148#	define vfork fork
2149#endif
2150#if has_getcwd || !has_getwd
2151	char *getcwd P((char*,size_t));
2152#else
2153	char *getwd P((char*));
2154#endif
2155#if has_setuid && !has_seteuid
2156#	undef seteuid
2157#	define seteuid setuid
2158#endif
2159#if has_spawn
2160#	if ALL_ABSOLUTE
2161#		define spawn_RCS spawnv
2162#	else
2163#		define spawn_RCS spawnvp
2164#	endif
2165#else
2166#	if ALL_ABSOLUTE
2167#		define exec_RCS execv
2168#	else
2169#		define exec_RCS execvp
2170#	endif
2171#endif
2172
2173/* utime.h */
2174#if !has_utimbuf
2175	struct utimbuf { time_t actime, modtime; };
2176#endif
2177
2178
2179/* Standard C library */
2180
2181/* <stdio.h> */
2182#ifndef L_tmpnam
2183#define L_tmpnam 32 /* power of 2 > sizeof("/usr/tmp/xxxxxxxxxxxxxxx") */
2184#endif
2185#ifndef SEEK_SET
2186#define SEEK_SET 0
2187#endif
2188#ifndef SEEK_CUR
2189#define SEEK_CUR 1
2190#endif
2191#if has_mktemp
2192	char *mktemp P((char*)); /* traditional */
2193#else
2194	char *tmpnam P((char*));
2195#endif
2196
2197/* <stdlib.h> */
2198char *getenv P((char const*));
2199void _exit P((int)) exiting;
2200void exit P((int)) exiting;
2201malloc_type malloc P((size_t));
2202malloc_type realloc P((malloc_type,size_t));
2203#ifndef EXIT_FAILURE
2204#define EXIT_FAILURE 1
2205#endif
2206#ifndef EXIT_SUCCESS
2207#define EXIT_SUCCESS 0
2208#endif
2209
2210/* <string.h> */
2211char *strcpy P((char*,char const*));
2212char *strchr P((char const*,int));
2213char *strrchr P((char const*,int));
2214void *memcpy P((void*,void const*,size_t));
2215#if has_memmove
2216	void *memmove P((void*,void const*,size_t));
2217#endif
2218
2219/* <time.h> */
2220time_t time P((time_t*));
2221EOF
2222
2223cat >a.c <<EOF
2224#include "$A_H"
2225#define a 0
2226#define b 1
2227#if H==a
2228#	include "a.ha"
2229#else
2230#	include "a.hb"
2231#endif
2232int main() { exitmain(0); }
2233EOF
2234
2235# Comment out lines in a.ha that the compiler rejects.
2236# a.ha may not contain comments that cross line boundaries.
2237# Leave the result in a.h$H.
2238H=a L=1
2239U=`wc -l <a.ha | sed 's| ||g'`
2240commentOut='s|^[^#/][^/]*|/* & */|'
2241
2242until
2243	test $U -lt $L  ||
2244	{ $PREPARE_CC || exit;  $CS -DH=$H a.c $LS >&2 && $CS_OK; }
2245do
2246	case $H in
2247	a) I=b;;
2248	*) I=a
2249	esac
2250
2251	# The compiler rejects some line in L..U.
2252	# Use binary search to set L to be the index of the first bad line in L..U.
2253	u=$U
2254	while test $L -lt $u
2255	do
2256		M=`expr '(' $L + $u ')' / 2`
2257		M1=`expr $M + 1`
2258		sed "$M1,\$$commentOut" a.h$H >a.h$I || exit
2259		$PREPARE_CC || exit
2260		if $CS -DH=$I a.c $LS >&2 && $CS_OK
2261		then L=$M1
2262		else u=$M
2263		fi
2264	done
2265
2266	# Comment out the bad line.
2267	badline=`sed -n "$L{p;q;}" a.h$H`
2268	echo >&3 "$n$0: commenting out \`$badline' $dots"
2269	sed "$L$commentOut" a.h$H >a.h$I || exit
2270
2271	H=$I
2272	L=`expr $L + 1`
2273done
2274
2275cat a.h$H
2276
2277echo >&3 OK
2278