Merge from vendor branch DIFFUTILS:
[dragonfly.git] / contrib / libio / gen-params
1 #!/bin/sh
2 # Copyright (C) 1992, 1993, 1994, 1997, 1998, 1999 Free Software Foundation
3
4 # This file is part of the GNU IO Library.  This library is free
5 # software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 2, or (at your option)
8 # any later version.
9
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this library; see the file COPYING.  If not, write to the Free
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 #    Written by Per Bothner (bothner@cygnus.com)
20
21 # This is a shell-script that figures out various things about a
22 # system, and writes (to stdout) a C-style include files with
23 # suitable definitions, including all the standard Posix types.
24 # It works by compiling various test programs -- some are run through
25 # the C pre-processor, and the output examined.
26 # The test programs are only compiled, not executed, so the script
27 # should even if you're cross-compiling.
28 # It uses $CC (which defaults to cc) to compile C programs (extension .c),
29 # and $CXX (which defaults to gcc) to compile C++ programs (extension .C).
30 # The shell-script is written for libg++.a.
31
32 # Usage: gen-params [NAME1=name1 ...]
33 # - where an assignment (such as size_t="unsigned int" means to
34 # use that value, instead of trying to figure it out.
35
36 # Uncomment following line for debugging
37 # set -x
38
39 SED=sed
40
41 # Evaluate the arguments (which should be assignments):
42 for arg in "$@"; do
43   # Quote arg (i.e. FOO=bar => FOO='bar'), then eval it.
44   eval `echo "$arg" | ${SED} -e "s|^\(.*\)=\(.*\)|\1='\2'|"`
45 done
46
47 macro_prefix=${macro_prefix-"_G_"}
48 rootdir=`pwd`/..
49 gccdir=${gccdir-${rootdir}/gcc}
50 binutilsdir=${binutilsdir-${rootdir}/binutils}
51 CC=${CC-`if [ -f ${gccdir}/xgcc ] ; \
52         then echo ${gccdir}/xgcc -B${gccdir}/ ; \
53         else echo cc ; fi`}
54 CXX=${CXX-`if [ -f ${gccdir}/xgcc ] ; \
55         then echo ${gccdir}/xgcc -B${gccdir}/ ; \
56         else echo gcc ; fi`}
57 CPP=${CPP-`echo ${CC} -E`}
58 CONFIG_NM=${CONFIG_NM-`if [ -f ${binutilsdir}/nm.new ] ; \
59         then echo ${binutilsdir}/nm.new ; \
60         else echo nm ; fi`}
61
62 cat <<!EOF!
63 /* AUTOMATICALLY GENERATED; DO NOT EDIT! */ 
64 #ifndef ${macro_prefix}config_h
65 #define ${macro_prefix}config_h
66 !EOF!
67
68 if [ x"${LIB_VERSION}" != "x" ] ; then
69   echo "#define ${macro_prefix}LIB_VERSION" '"'${LIB_VERSION}'"'
70 fi
71
72 # This program is used to test if the compiler prepends '_' before identifiers.
73 # It is also used to check the g++ uses '$' or '.' various places.
74
75 if test -z "${NAMES_HAVE_UNDERSCORE}" \
76    || test -z "${DOLLAR_IN_LABEL}" \
77    || test -z "${VTABLE_LABEL_PREFIX}"; then
78   cat >dummy.h <<!EOF!
79 #ifdef __GNUG__
80 #pragma interface
81 #endif
82   struct filebuf {
83       virtual int foo();
84   };
85 !EOF!
86   cat >dummy.C <<!EOF!
87 #ifdef __GNUG__
88 #pragma implementation
89 #endif
90 #include "dummy.h"
91   int filebuf::foo() { return 0; }
92   extern "C" int FUNC(int);
93   int FUNC(int i) { return i+10; }
94 !EOF!
95
96   if ${CXX} -O -c dummy.C ; then
97     if test -z "${NAMES_HAVE_UNDERSCORE}" ; then
98       if test "`${CONFIG_NM} dummy.o | grep _FUNC`" != ""; then
99         NAMES_HAVE_UNDERSCORE=1
100       elif test "`${CONFIG_NM} dummy.o | grep FUNC`" != ""; then
101         NAMES_HAVE_UNDERSCORE=0
102       else
103         echo "${CONFIG_NM} failed to find FUNC in dummy.o!" 1>&2; exit -1;
104       fi
105     fi
106     echo "#define ${macro_prefix}NAMES_HAVE_UNDERSCORE ${NAMES_HAVE_UNDERSCORE}"
107
108     if test -z "${VTABLE_LABEL_PREFIX}" ; then
109       # Determine how virtual function tables are named.  This is fragile,
110       # because different nm's produce output in different formats.
111       ${CONFIG_NM} dummy.o >TMP
112       if [ -n "`${SED} -n -e 's/ virtual table/nope/p' <TMP`" ] ; then
113         ${CONFIG_NM} --no-cplus dummy.o >TMP 2>/dev/null ||
114           ${CONFIG_NM} --no-demangle dummy.o >TMP 2>/dev/null ||
115           ${CONFIG_NM} dummy.o >TMP 2>/dev/null
116       fi
117       # First we look for a pattern that matches historical output from g++.
118       # We surround the actual label name by <> to separate it from
119       # other nm junk. 
120       ${SED} -n -e 's/_*vt[$_.]7*filebuf/<&>/p' <TMP >dummy.out
121       # For paranoia's sake (e.g. if we're using some other C++ compiler)
122       # we try a more general pattern, and append the result.
123       grep -v foo <TMP \
124         | ${SED} -n -e 's/[a-zA-Z0-9_.$]*filebuf[a-zA-Z0-9_.$]*/<&>/p' \
125         >>dummy.out
126       # Now we get rid of the <>, and any other junk on the nm output line.
127       # (We get rid of <filebuf> in case nm included debugging output for
128       # class filebuf itself.)  On windows32, we also need to delete the 
129       # unique sections (.data$_vt$*), otherwise we get the wrong result.
130       # Finally, we select the first line of the result, and hope that's 
131       # what we wanted!
132       vtab_name=`${SED} -n -e '/<filebuf>/d' \
133         -e '/\.data[$_.]<_vt\$7filebuf>/d' \
134         -e 's/^.*<\(.*\)>.*$/\1/p' \
135         <dummy.out | ${SED} -n -e '1p'`
136       case "${vtab_name}" in
137         *7filebuf) echo "#define ${macro_prefix}VTABLE_LABEL_HAS_LENGTH 1" ;;
138         *) echo "#define ${macro_prefix}VTABLE_LABEL_HAS_LENGTH 0" ;;
139       esac
140       VTABLE_LABEL_PREFIX=`echo $vtab_name | ${SED} -e 's/7*filebuf//'`
141     fi
142     echo "#define ${macro_prefix}VTABLE_LABEL_PREFIX" '"'"${VTABLE_LABEL_PREFIX}"'"'
143     if [ "${VTABLE_LABEL_PREFIX}" = "__vt_" -o \
144         "${VTABLE_LABEL_PREFIX}" = "___vt_" ] ; then
145       echo "#define ${macro_prefix}USING_THUNKS"
146     fi
147
148     # VTABLE_LABEL_PREFIX_ID is the same as VTABLE_LABEL_PREFIX,
149     # but the former is a C identifier, while the latter is a quoted
150     # st
151     if [ -z ""`echo ${VTABLE_LABEL_PREFIX} | ${SED} -e 's/[a-zA-Z0-9_]//g'` ] ; then
152       if [ "${NAMES_HAVE_UNDERSCORE}" = "1" ] ; then
153         VTABLE_LABEL_PREFIX=`echo ${VTABLE_LABEL_PREFIX} | ${SED} -e 's/^_//'`
154       fi
155       echo "#define ${macro_prefix}VTABLE_LABEL_PREFIX_ID ${VTABLE_LABEL_PREFIX}"
156     fi
157
158 #    if test -n "${DOLLAR_IN_LABEL}" ; then
159 #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL ${DOLLAR_IN_LABEL}"
160 #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[$$]7filebuf'`" != ""; then
161 #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 1"
162 #    elif test "`${CONFIG_NM} dummy.o | grep 'vt[.]7filebuf'`" != ""; then
163 #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
164 #    elif test "`${CONFIG_NM} dummy.o | grep 'vtbl__7filebuf'`" != ""; then
165 #      echo "#define ${macro_prefix}DOLLAR_IN_LABEL 0"
166 #    else
167 #      echo "gen-params: ${CONFIG_NM} failed to find vt[.\$]filebuf in dummy.o!" 1>&2; exit 1
168 #    fi
169   else
170     # The compile failed for some reason (no C++?)
171     echo "gen-params: could not compile dummy.C with ${CXX}" 1>&2; exit 1;
172   fi
173 fi
174
175 # A little test program to check if struct stat has st_blksize.
176 cat >dummy.c <<!EOF!
177 #include <sys/types.h>
178 #include <sys/stat.h>
179 int BLKSIZE(struct stat *st)
180 {
181     return st->st_blksize;
182 }
183 !EOF!
184
185 if ${CC} -c dummy.c >/dev/null 2>&1 ; then
186   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 1"
187 else
188   echo "#define ${macro_prefix}HAVE_ST_BLKSIZE 0"
189 fi
190
191 # A little test program to check if the name 'clog' is defined in libm,
192 # as it is under DEC UNIX.
193 cat >dummy.c <<!EOF!
194 int clog;
195 main () {}
196 !EOF!
197
198 if ${CC} dummy.c -lm 2>&1 >/dev/null | grep clog >/dev/null; then
199   echo "#define ${macro_prefix}CLOG_CONFLICT 1"
200 fi
201
202 echo ""
203
204 # Next, generate definitions for the standard types (such as mode_t)
205 # compatible with those in the standard C header files.
206 # It works by a dummy program through the C pre-processor, and then
207 # using sed to search for typedefs in the output.
208
209 for hdr in wchar wctype; do
210   eval $hdr=0
211   cat >dummy.c <<EOF
212 #include <${hdr}.h>
213 EOF
214   if ${CPP} dummy.c >/dev/null 2>&1 ; then eval $hdr=1; fi
215 done
216
217 cat >dummy.c <<!EOF!
218 #include <sys/types.h>
219 #include <stddef.h>
220 #ifdef __STDC__
221 #include <stdarg.h>
222 #else /* !__STDC__ */
223 #include <varargs.h>
224 #endif /* __STDC__ */
225 #include <stdio.h>
226 #include <time.h>
227 #include <signal.h>
228 #ifdef __STDC__
229 #include <limits.h>
230 #endif
231 #if WCHAR == 1
232 #include <wchar.h>
233 #endif
234 #if WCTYPE == 1
235 #include <wctype.h>
236 #endif
237 #ifdef size_t
238 typedef size_t Xsize_t;
239 #elif defined(__SIZE_TYPE__)
240 typedef __SIZE_TYPE__ Xsize_t;
241 #endif
242 #ifdef ptrdiff_t
243 typedef ptrdiff_t Xptrdiff_t;
244 #elif defined(__PTRDIFF_TYPE__)
245 typedef __PTRDIFF_TYPE__ Xptrdiff_t;
246 #endif
247 #ifdef wchar_t
248 typedef wchar_t Xwchar_t;
249 #elif defined(__WCHAR_TYPE__)
250 typedef __WCHAR_TYPE__ Xwchar_t;
251 #endif
252 #ifdef va_list
253 typedef va_list XXXva_list;
254 #endif
255 #ifdef BUFSIZ
256 long XBUFSIZ=BUFSIZ;
257 #endif
258 #ifdef FOPEN_MAX
259 long XFOPEN_MAX=FOPEN_MAX;
260 #endif
261 #ifdef FILENAME_MAX
262 long XFILENAME_MAX=FILENAME_MAX;
263 #endif
264 #ifdef SHRT_MAX
265 long XSHRT_MAX=SHRT_MAX;
266 #endif
267 #ifdef INT_MAX
268 long XINT_MAX=INT_MAX;
269 #endif
270 #ifdef LONG_MAX
271 long XLONG_MAX=LONG_MAX;
272 #endif
273 #ifdef LONG_LONG_MAX
274 long XLONG_LONG_MAX=LONG_LONG_MAX;
275 #endif
276 !EOF!
277
278 if ${CPP} dummy.c -DWCHAR=$wchar -DWCTYPE=$wctype >TMP ; then true
279 else
280   echo "gen-params: could not invoke ${CPP} on dummy.c" 1>&2 ; exit 1
281 fi
282 tr '    ' ' ' <TMP >dummy.out
283
284 for TYPE in dev_t clock_t fpos_t gid_t ino_t mode_t nlink_t off_t pid_t ptrdiff_t sigset_t size_t ssize_t time_t uid_t va_list wchar_t wint_t int16_t uint16_t int32_t uint_32_t u_int16_t u_int32_t; do
285     eval IMPORTED=\$$TYPE
286     if [ -n "${IMPORTED}" ] ; then
287         eval "$TYPE='$IMPORTED'"
288     else
289         t=$TYPE
290         VALUE=''
291
292         # Follow `typedef VALUE TYPE' chains, but don't loop indefinitely.
293         for iteration in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20; do
294             # Search dummy.out for a typedef for X*$t.
295             sed_script="
296                 s/unsigned long long int/_G_ullong/g
297                 s/long long int/_G_llong/g
298                 s/unsigned long long/_G_ullong/g
299                 s/long long/_G_llong/g
300                 /.*typedef  *\\(.*[^ ]\\)  *X*$t *;.*/{s||\1|;p;q;}
301                 /.*typedef  *\\(.*[^ a-zA-Z0-9_]\\)X*$t *;.*/{s||\1|;p;q;}
302             "
303             t=`${SED} -n "$sed_script" <dummy.out`
304             case "$t" in
305               '')
306                 break;;
307               *)
308                 # Found a type $t; save it in VALUE.
309                 VALUE=$t
310                 # If it won't cause problems in matching,
311                 # look for a typedef for it in turn.
312                 case "$VALUE" in
313                   *.* | */* | *\ * | *\** | *\[* | *\\*) break;;
314                 esac;;
315             esac
316         done
317
318         case "$VALUE" in
319           ?*) eval "$TYPE=\"$VALUE\""
320         esac
321     fi
322 done
323
324 # Look for some standard macros.
325 for NAME in BUFSIZ FOPEN_MAX FILENAME_MAX NULL; do
326     eval IMPORTED=\$$NAME
327     if [ -n "${IMPORTED}" ] ; then
328         eval "$NAME='$IMPORTED /* specified */'"
329     else
330         rm -f TMP
331         ${SED} -n -e 's| *;|;|g' -e "s|long X${NAME}= *\(.*\);|\1|w TMP" \
332           <dummy.out>/dev/null
333         # Now select the first definition.
334         if [ -s TMP ]; then
335             eval "$NAME='"`${SED} -e '2,$d' <TMP`"'"
336         fi
337     fi
338 done
339
340 # These macros must be numerical constants; strip any trailing 'L's.
341 for NAME in SHRT_MAX INT_MAX LONG_MAX LONG_LONG_MAX; do
342     eval IMPORTED=\$$NAME
343     if [ -n "${IMPORTED}" ] ; then
344         eval "$NAME='$IMPORTED /* specified */'"
345     else
346         rm -f TMP
347         ${SED} -n -e 's| *;|;|g' -e "s|long X${NAME}= *\([0-9]*\)L* *;|\1|w TMP" \
348           <dummy.out>/dev/null
349         # Now select the first definition.
350         if [ -s TMP ]; then
351             eval "$NAME='"`${SED} -e '2,$d' <TMP`"'"
352         fi
353     fi
354 done
355
356 # Figure out integral type sizes.
357
358 default_int16='short /* deduction failed */'
359 default_int32='long /* deduction failed */'
360 INT16=32767
361 INT32=2147483647
362
363 if [ "${SHRT_MAX}" = $INT16 ] ; then
364   default_int16='short /* deduced */'
365   if [ "${LONG_MAX}" = $INT32 ] ; then
366     default_int32='long /* deduced */'
367   elif [ "${INT_MAX}" = $INT32 ] ; then
368     default_int32='int /* deduced */'
369   fi
370 fi
371
372 [ -n "$u_int16_t" ] && uint16_t="$u_int16_t"
373 [ -n "$u_int32_t" ] && uint32_t="$u_int32_t"
374
375 [ -z  "$int16_t" ] &&  int16_t="$default_int16"
376 [ -z "$uint16_t" ] && uint16_t="unsigned $int16_t"
377 [ -z  "$int32_t" ] &&  int32_t="$default_int32"
378 [ -z "$uint32_t" ] && uint32_t="unsigned $int32_t"
379
380 cat <<!EOF!
381 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
382 typedef          int   ${macro_prefix}int8_t __attribute__((__mode__(__QI__)));
383 typedef unsigned int  ${macro_prefix}uint8_t __attribute__((__mode__(__QI__)));
384 typedef          int  ${macro_prefix}int16_t __attribute__((__mode__(__HI__)));
385 typedef unsigned int ${macro_prefix}uint16_t __attribute__((__mode__(__HI__)));
386 typedef          int  ${macro_prefix}int32_t __attribute__((__mode__(__SI__)));
387 typedef unsigned int ${macro_prefix}uint32_t __attribute__((__mode__(__SI__)));
388 typedef          int  ${macro_prefix}int64_t __attribute__((__mode__(__DI__)));
389 typedef unsigned int ${macro_prefix}uint64_t __attribute__((__mode__(__DI__)));
390 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 8
391 __extension__ typedef long long ${macro_prefix}llong;
392 __extension__ typedef unsigned long long ${macro_prefix}ullong;
393 #endif
394 #else
395 typedef  $int16_t  ${macro_prefix}int16_t;
396 typedef $uint16_t ${macro_prefix}uint16_t;
397 typedef  $int32_t  ${macro_prefix}int32_t;
398 typedef $uint32_t ${macro_prefix}uint32_t;
399 #endif
400
401 typedef ${clock_t-int /* default */} ${macro_prefix}clock_t;
402 typedef ${dev_t-int /* default */} ${macro_prefix}dev_t;
403 typedef ${fpos_t-long /* default */} ${macro_prefix}fpos_t;
404 typedef ${gid_t-int /* default */} ${macro_prefix}gid_t;
405 typedef ${ino_t-int /* default */} ${macro_prefix}ino_t;
406 typedef ${mode_t-int /* default */} ${macro_prefix}mode_t;
407 typedef ${nlink_t-int /* default */} ${macro_prefix}nlink_t;
408 typedef ${off_t-long /* default */} ${macro_prefix}off_t;
409 typedef ${pid_t-int /* default */} ${macro_prefix}pid_t;
410 #ifndef __PTRDIFF_TYPE__
411 #define __PTRDIFF_TYPE__ ${ptrdiff_t-long int /* default */}
412 #endif
413 typedef __PTRDIFF_TYPE__ ${macro_prefix}ptrdiff_t;
414 typedef ${sigset_t-int /* default */} ${macro_prefix}sigset_t;
415 #ifndef __SIZE_TYPE__
416 #define __SIZE_TYPE__ ${size_t-unsigned long /* default */}
417 #endif
418 typedef __SIZE_TYPE__ ${macro_prefix}size_t;
419 typedef ${time_t-int /* default */} ${macro_prefix}time_t;
420 typedef ${uid_t-int /* default */} ${macro_prefix}uid_t;
421 typedef ${wchar_t-int /* default */} ${macro_prefix}wchar_t;
422
423 #define ${macro_prefix}BUFSIZ ${BUFSIZ-1024 /* default */}
424 #define ${macro_prefix}FOPEN_MAX ${FOPEN_MAX-32 /* default */}
425 #define ${macro_prefix}FILENAME_MAX ${FILENAME_MAX-1024 /* default */}
426 #if defined (__cplusplus) || defined (__STDC__)
427 #define ${macro_prefix}ARGS(ARGLIST) ARGLIST
428 #else
429 #define ${macro_prefix}ARGS(ARGLIST) ()
430 #endif
431 #if !defined (__GNUG__) || defined (__STRICT_ANSI__)
432 #define ${macro_prefix}NO_NRV
433 #endif
434 #if !defined (__GNUG__)
435 #define _G_NO_EXTERN_TEMPLATES
436 #endif
437 !EOF!
438
439 # ssize_t is the signed version of size_t
440 if [ -n "${ssize_t}" ] ; then
441     echo "typedef ${ssize_t} ${macro_prefix}ssize_t;"
442 elif [ -z "${size_t}" ] ; then
443     echo "typedef long ${macro_prefix}ssize_t;"
444 else
445     # Remove "unsigned" from ${size_t} to get ${ssize_t}.
446     tmp="`echo ${size_t} | ${SED} -e 's|unsigned||g' -e 's|  | |g'`"
447     if [ -z "$tmp" ] ; then
448         tmp=int
449     else
450         # check $tmp doesn't conflict with <unistd.h>
451         echo "#include <unistd.h>
452         extern $tmp read();" >dummy.c
453         ${CC} -c dummy.c >/dev/null 2>&1 || tmp=int
454     fi
455     echo "typedef $tmp /* default */ ${macro_prefix}ssize_t;"
456 fi
457
458 # wint_t is often the integral type to which wchar_t promotes.
459 if [ -z "${wint_t}" ] ; then
460   for TYPE in int 'unsigned int' 'long int' 'long unsigned int'; do
461     cat >dummy.C <<!EOF!
462 #ifndef __WCHAR_TYPE__
463 #define __WCHAR_TYPE__ ${wchar_t-int /* default */}
464 #endif
465 typedef __WCHAR_TYPE__ ${macro_prefix}wchar_t;
466 void foo ($TYPE);
467 void foo (double);
468 void bar (${macro_prefix}wchar_t w)
469 {
470   foo (w);
471 }
472 !EOF!
473     if ${CXX} -c dummy.C >/dev/null 2>&1 ; then  
474       wint_t="$TYPE /* default */"
475       break
476     fi
477   done
478 fi
479 echo "typedef ${wint_t-int /* wchar_t is broken */} ${macro_prefix}wint_t;"
480
481 # va_list can cause problems (e.g. some systems have va_list as a struct).
482 # Check to see if ${va_list-char*} really is compatible with stdarg.h.
483 cat >dummy.C <<!EOF!
484 #define X_va_list ${va_list-char* /* default */}
485 extern long foo(X_va_list ap); /* Check that X_va_list compiles on its own */
486 extern "C" {
487 #include <stdarg.h>
488 }
489 long foo(X_va_list ap) { return va_arg(ap, long); }
490 long bar(int i, ...)
491 { va_list ap; long j; va_start(ap, i); j = foo(ap); va_end(ap); return j; }
492 !EOF!
493 if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
494   # Ok: We have something that works.
495   echo "typedef ${va_list-char* /* default */} ${macro_prefix}va_list;"
496 else
497   echo "#define ${macro_prefix}NEED_STDARG_H"
498   # Check and see if we have __gnuc_va_list, as we might set up define
499   # loops if we use va_list.
500   cat >dummy.C <<!EOF!
501 #include <stdarg.h>
502 long foo(__gnuc_va_list ap) { return va_arg(ap, long); }
503 !EOF!
504   if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
505     echo "#define ${macro_prefix}va_list __gnuc_va_list"
506   else
507     echo "#define ${macro_prefix}va_list va_list"
508   fi
509 fi
510
511 cat >dummy.c <<!EOF!
512 #include <signal.h>
513 extern int (*signal())();
514 extern int dummy (int);
515 main()
516 {
517     int (*oldsig)(int) = signal (1, dummy);
518     (void) signal (2, oldsig);
519     return 0;
520 }
521 !EOF!
522 if ${CC} -c dummy.c >/dev/null 2>&1 ; then
523   echo "#define ${macro_prefix}signal_return_type int"
524 else
525   echo "#define ${macro_prefix}signal_return_type void"
526 fi
527
528 # check sprintf return type
529
530 cat >dummy.c <<!EOF!
531 #include <stdio.h>
532 extern int sprintf(); char buf[100];
533 int main() { return sprintf(buf, "%d", 34); }
534 !EOF!
535 if ${CC} -c dummy.c >/dev/null 2>&1 ; then
536   echo "#define ${macro_prefix}sprintf_return_type int"
537 else
538   echo "#define ${macro_prefix}sprintf_return_type char*"
539 fi
540
541 if test -n "${HAVE_ATEXIT}" ; then
542  echo "#define ${macro_prefix}HAVE_ATEXIT ${HAVE_ATEXIT}"
543 else
544   cat >dummy.c <<!EOF!
545 #include <stdlib.h>
546 int main()
547 {
548   atexit (0);
549 }
550 !EOF!
551   if ${CC} dummy.c >/dev/null 2>&1 ; then
552     echo "#define ${macro_prefix}HAVE_ATEXIT 1"
553   else
554     echo "#define ${macro_prefix}HAVE_ATEXIT 0"
555   fi
556 fi
557
558
559 # *** Check for presence of certain include files ***
560
561 # check for sys/resource.h
562
563 if test -n "${HAVE_SYS_RESOURCE}" ; then
564  echo "#define ${macro_prefix}HAVE_SYS_RESOURCE ${HAVE_SYS_RESOURCE}"
565 else
566   cat >dummy.c <<!EOF!
567 #include <sys/types.h>
568 #include <sys/time.h>
569 #include <sys/resource.h>
570   int main()
571   {
572     struct rusage res;
573     getrusage(RUSAGE_SELF, &res);
574     return (int)(res.ru_utime.tv_sec + (res.ru_utime.tv_usec / 1000000.0));
575   }
576 !EOF!
577   # Note: We link because some systems have sys/resource, but not getrusage().
578   if ${CC} dummy.c >/dev/null 2>&1 ; then
579     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 1"
580   else
581     echo "#define ${macro_prefix}HAVE_SYS_RESOURCE 0"
582   fi
583 fi
584
585 # check for struct tms in sys/times.h
586
587 if test -n "${HAVE_SYS_TIMES}" ; then
588  echo "#define ${macro_prefix}HAVE_SYS_TIMES ${HAVE_SYS_TIMES}"
589 else
590  cat >dummy.c <<!EOF!
591 #include <sys/types.h>
592 #include <sys/times.h>
593   int main()
594   {
595     struct tms s;
596     return s.tms_utime;
597   }
598 !EOF!
599   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
600     echo "#define ${macro_prefix}HAVE_SYS_TIMES 1"
601   else
602     echo "#define ${macro_prefix}HAVE_SYS_TIMES 0"
603   fi
604 fi
605
606 # check for sys/socket.h
607
608 if test -n "${HAVE_SYS_SOCKET}" ; then
609  echo "#define ${macro_prefix}HAVE_SYS_SOCKET ${HAVE_SYS_SOCKET}"
610 else
611   echo '#include <sys/types.h>' >dummy.c
612   echo '#include <sys/socket.h>' >>dummy.c
613   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
614     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 1"
615   else
616     echo "#define ${macro_prefix}HAVE_SYS_SOCKET 0"
617   fi
618 fi
619
620 # check for sys/cdefs.h
621
622 if test -n "${HAVE_SYS_CDEFS}" ; then
623  echo "#define ${macro_prefix}HAVE_SYS_CDEFS ${HAVE_SYS_CDEFS}"
624 else
625   echo '#include <sys/cdefs.h>' >dummy.c
626   echo 'extern int myfunc __P((int, int));' >>dummy.c
627   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
628     echo "#define ${macro_prefix}HAVE_SYS_CDEFS 1"
629   else
630     echo "#define ${macro_prefix}HAVE_SYS_CDEFS 0"
631   fi
632 fi
633
634 # Check for a (Posix-compatible) sys/wait.h */
635
636 if test -n "${HAVE_SYS_WAIT}" ; then
637  echo "#define ${macro_prefix}HAVE_SYS_WAIT ${HAVE_SYS_WAIT}"
638 else
639   cat >dummy.c <<!EOF!
640 #include <sys/types.h>
641 #include <sys/wait.h>
642   int f() { int i; wait(&i); return i; }
643 !EOF!
644   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
645     echo "#define ${macro_prefix}HAVE_SYS_WAIT 1"
646   else
647     echo "#define ${macro_prefix}HAVE_SYS_WAIT 0"
648   fi
649 fi
650
651 if test -n "${HAVE_UNISTD}" ; then
652  echo "#define ${macro_prefix}HAVE_UNISTD ${HAVE_UNISTD}"
653 else
654   echo '#include <unistd.h>' >dummy.c
655   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
656     echo "#define ${macro_prefix}HAVE_UNISTD 1"
657   else
658     echo "#define ${macro_prefix}HAVE_UNISTD 0"
659   fi
660 fi
661
662 if test -n "${HAVE_DIRENT}" ; then
663  echo "#define ${macro_prefix}HAVE_DIRENT ${HAVE_DIRENT}"
664 else
665   echo '#include <sys/types.h>
666 #include <dirent.h>' >dummy.c
667   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
668     echo "#define ${macro_prefix}HAVE_DIRENT 1"
669   else
670     echo "#define ${macro_prefix}HAVE_DIRENT 0"
671   fi
672 fi
673
674 if test -n "${HAVE_CURSES}" ; then
675  echo "#define ${macro_prefix}HAVE_CURSES ${HAVE_CURSES}"
676 else
677   echo '#include <curses.h>' >dummy.c
678   if ${CC} -c dummy.c >/dev/null 2>&1 ; then
679     echo "#define ${macro_prefix}HAVE_CURSES 1"
680   else
681     echo "#define ${macro_prefix}HAVE_CURSES 0"
682   fi
683 fi
684
685 # There is no test for this at the moment; it is just set by the
686 # configuration files.
687 if test -n "${MATH_H_INLINES}" ; then
688   echo "#define ${macro_prefix}MATH_H_INLINES ${MATH_H_INLINES}"
689 else
690   echo "#define ${macro_prefix}MATH_H_INLINES 0"
691 fi
692
693 if test -n "${HAVE_BOOL}" ; then
694  echo "#define ${macro_prefix}HAVE_BOOL ${HAVE_BOOL}"
695 else
696   echo 'bool i=true,j=false;' >dummy.C
697   if ${CXX} -c dummy.C >/dev/null 2>&1 ; then
698     echo "#define ${macro_prefix}HAVE_BOOL 1"
699   else
700     echo "#define ${macro_prefix}HAVE_BOOL 0"
701   fi
702 fi
703
704 if test -n "${NO_USE_DTOA}" ; then
705     echo "#define ${macro_prefix}NO_USE_DTOA 1"
706 fi
707 if test -n "${USE_INT32_FLAGS}" ; then
708     echo "#define ${macro_prefix}USE_INT32_FLAGS 1"
709 fi
710
711 # A little test program to check if __printf_fp is available.
712 cat >dummy.c <<EOF
713 int main()
714 {
715     return __printf_fp ();
716 }
717 EOF
718
719 if ${CC} dummy.c >/dev/null 2>&1 ; then
720   echo "#define ${macro_prefix}HAVE_PRINTF_FP 1"
721   echo "#define ${macro_prefix}HAVE_LONG_DOUBLE_IO 1"
722 else
723   echo "#define ${macro_prefix}HAVE_PRINTF_FP 0"
724   echo "#define ${macro_prefix}HAVE_LONG_DOUBLE_IO 0"
725 fi
726
727 # Uncomment the following line if you don't have working templates.
728 # echo "#define ${macro_prefix}NO_TEMPLATES"
729
730 # Override bogus definitions of NULL in system headers.
731 cat <<EOF
732 #undef NULL
733 #define __need_NULL
734 #include <stddef.h>
735 EOF
736
737 rm -f dummy.C dummy.o dummy.c dummy.out TMP core a.out
738
739 echo "#endif /* !${macro_prefix}config_h */"