OSDN Git Service

Fix #endif foo; don't alter sh options; clean up all DONE files
[pf3gnuchains/gcc-fork.git] / gcc / fixincludes
1 #! /bin/sh
2 # Install modified versions of certain ANSI-incompatible system header files
3 # which are fixed to work correctly with ANSI C
4 # and placed in a directory that GNU C will search.
5
6 # See README-fixinc for more information.
7
8 # Directory where gcc sources (and sometimes special include files) live.
9 # fixincludes doesn't use this, but fixinc.svr4 does, and I want to make
10 # sure somebody doesn't try to use arg3 for something incompatible. -- gumby
11 SRCDIR=${3-${SRCDIR-.}}
12
13 # Directory containing the original header files.
14 # (This was named INCLUDES, but that conflicts with a name in Makefile.in.)
15 INPUT=${2-${INPUT-/usr/include}}
16
17 # Directory in which to store the results.
18 LIB=${1?"fixincludes: output directory not specified"}
19
20 # Define PWDCMD as a command to use to get the working dir
21 # in the form that we want.
22 PWDCMD=pwd
23 case "`pwd`" in
24 //*)
25         # On an Apollo, discard everything before `/usr'.
26         PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'"
27         ;;
28 esac
29
30 # Original directory.
31 ORIGDIR=`${PWDCMD}`
32
33 # Make sure it exists.
34 if [ ! -d $LIB ]; then
35   mkdir $LIB || exit 1
36 fi
37
38 # Make LIB absolute only if needed to avoid problems with the amd.
39 case $LIB in
40 /*)
41         ;;
42 *)
43         cd $LIB; LIB=`${PWDCMD}`
44         ;;
45 esac
46
47 # Make SRCDIR absolute only if needed to avoid problems with the amd.
48 cd $ORIGDIR
49 case $SRCDIR in
50 /*)
51         ;;
52 *)
53         cd $SRCDIR; SRCDIR=`${PWDCMD}`
54         ;;
55 esac
56
57 # Fail if no arg to specify a directory for the output.
58 if [ x$1 = x ]
59 then echo fixincludes: no output directory specified
60 exit 1
61 fi
62
63 echo Building fixed headers in ${LIB}
64
65 # Determine whether this system has symbolic links.
66 if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
67   rm -f $LIB/ShouldNotExist
68   LINKS=true
69 else
70   LINKS=false
71 fi
72
73 echo Finding directories and links to directories
74 cd ${INPUT}
75 # Find all directories and all symlinks that point to directories.
76 # Put the list in $files.
77 # Each time we find a symlink, add it to newdirs
78 # so that we do another find within the dir the link points to.
79 # Note that $files may have duplicates in it;
80 # later parts of this file are supposed to ignore them.
81 dirs="."
82 levels=2
83 while [ -n "$dirs" ] && [ $levels -gt 0 ]
84 do
85     levels=`expr $levels - 1`
86     newdirs=
87     for d in $dirs
88     do
89         echo " Searching $INPUT/$d"
90         if [ "$d" != . ]
91         then
92             d=$d/.
93         fi
94
95         # Find all directories under $d, relative to $d, excluding $d itself.
96         files="$files `find $d -type d -print | \
97                        sed -e '/\/\.$/d' -e '/^\.$/d'`"
98         # Find all links to directories.
99         # Using `-exec test -d' in find fails on some systems,
100         # and trying to run test via sh fails on others,
101         # so this is the simplest alternative left.
102         # First find all the links, then test each one.
103         theselinks=
104         $LINKS && \
105           theselinks=`find $d -type l -print`
106         for d1 in $theselinks --dummy--
107         do
108             # If the link points to a directory,
109             # add that dir to $newdirs
110             if [ -d $d1 ]
111             then
112                 newdirs="$newdirs $d1"
113             fi
114         done
115     done
116
117     files="$files $newdirs"
118     dirs="$newdirs"
119 done
120
121 dirs=
122 echo "All directories (including links to directories):"
123 echo $files
124
125 for file in $files; do
126   rm -rf $LIB/$file
127   if [ ! -d $LIB/$file ]
128   then mkdir $LIB/$file
129   fi
130 done
131 mkdir $LIB/root
132
133 # treetops gets an alternating list
134 # of old directories to copy
135 # and the new directories to copy to.
136 treetops="${INPUT} ${LIB}"
137
138 if $LINKS; then
139   echo 'Making symbolic directory links'
140   for file in $files; do
141     dest=`ls -ld $file | sed -n 's/.*-> //p'`
142     if [ "$dest" ]; then    
143       cwd=`${PWDCMD}`
144       # In case $dest is relative, get to $file's dir first.
145       cd ${INPUT}
146       cd `echo ./$file | sed -n 's&[^/]*$&&p'`
147       # Check that the target directory exists.
148       # Redirections changed to avoid bug in sh on Ultrix.
149       (cd $dest) > /dev/null 2>&1
150       if [ $? = 0 ]; then
151         cd $dest
152         # X gets the dir that the link actually leads to.
153         x=`${PWDCMD}`
154         # If a link points to ., make a similar link to .
155         if [ $x = $INPUT ]; then
156           echo $file '->' . ': Making link'
157           rm -fr ${LIB}/$file > /dev/null 2>&1
158           ln -s . ${LIB}/$file > /dev/null 2>&1
159         # If link leads back into ${INPUT},
160         # make a similar link here.
161         elif expr $x : "${INPUT}/.*" > /dev/null; then
162           # Y gets the actual target dir name, relative to ${INPUT}.
163           y=`echo $x | sed -n "s&${INPUT}/&&p"`
164           echo $file '->' $y ': Making link'
165           rm -fr ${LIB}/$file > /dev/null 2>&1
166           ln -s ${LIB}/$y ${LIB}/$file > /dev/null 2>&1
167         else
168           # If the link is to a dir $target outside ${INPUT},
169           # repoint the link at ${INPUT}/root$target
170           # and process $target into ${INPUT}/root$target
171           # treat this directory as if it actually contained the files.
172           echo $file '->' root$x ': Making link'
173           if [ -d $LIB/root$x ]
174           then true
175           else
176             dirname=root$x/
177             dirmade=.
178             cd $LIB
179             while [ x$dirname != x ]; do
180               component=`echo $dirname | sed -e 's|/.*$||'`
181               mkdir $component >/dev/null 2>&1
182               cd $component
183               dirmade=$dirmade/$component
184               dirname=`echo $dirname | sed -e 's|[^/]*/||'`
185             done
186           fi
187           # Duplicate directory structure created in ${LIB}/$file in new
188           # root area.
189           for file2 in $files; do
190             case $file2 in
191               $file/./*)
192                 dupdir=${LIB}/root$x/`echo $file2 | sed -n "s|^${file}/||p"`
193                 echo "Duplicating ${file}'s ${dupdir}"
194                 if [ -d ${dupdir} ]
195                 then true
196                 else
197                   mkdir ${dupdir}
198                 fi
199                 ;;
200               *)
201                 ;;
202             esac
203           done
204           rm -fr ${LIB}/$file > /dev/null 2>&1
205           ln -s ${LIB}/root$x ${LIB}/$file > /dev/null 2>&1
206           treetops="$treetops $x ${LIB}/root$x"
207         fi
208       fi
209       cd $cwd
210     fi
211   done
212 fi
213
214 set x $treetops
215 shift
216 while [ $# != 0 ]; do
217   # $1 is an old directory to copy, and $2 is the new directory to copy to.
218   cd ${INPUT}
219   cd $1
220 # The same dir can appear more than once in treetops.
221 # There's no need to scan it more than once.
222   if [ -f $2/DONE ]
223   then
224     files=
225   else
226     touch $2/DONE
227     echo Fixing directory $1 into $2
228 # Check .h files which are symlinks as well as those which are files.
229 # A link to a header file will not be processed by anything but this.
230     if $LINKS; then
231       files=`find . -name '*.h' \( -type f -o -type l \) -print`
232     else
233       files=`find . -name '*.h' -type f -print`
234     fi
235     echo Checking header files
236   fi
237 # Note that BSD43_* are used on recent MIPS systems.
238   for file in $files; do
239 # This call to egrep is essential, since checking a file with egrep
240 # is much faster than actually trying to fix it.
241 # It is also essential that most files *not* match!
242 # Thus, matching every #endif is unacceptable.
243 # But the argument to egrep must be kept small, or many versions of egrep
244 # won't be able to handle it.
245 #
246 # We use the pattern [!-.0-~] instead of [^/    ] to match a noncomment
247 # following #else or #endif because some buggy egreps think [^/] matches
248 # newline, and they thus think `#else ' matches `#e[ndiflse]*[  ]+[^/   ]'.
249 #
250 # We use the pattern [^a-zA-Z0-9_][_a-ce-km-z][a-z0-9] to match an identifier
251 # following #if or #elif that is not surrounded by __.  The `a-ce-km-z'
252 # in this pattern lacks `d' and `l'; this means we don't worry about
253 # identifiers starting with `d' or `l'.  This is OK, since none of the
254 # identifiers below start with `d' or `l'.  It also greatly improves
255 # performance, since many files contain lines of the form `#if ... defined ...'
256 # or `#if lint'.
257     if egrep '//|[      _]_IO|CTRL|^#define.NULL|^#e[nl][ds][ief]*[     ]+[!-.0-~]|^#[el]*if.*[^a-zA-Z0-9_][_a-ce-km-z][a-z0-9]' $file >/dev/null; then
258       if [ -r $file ]; then
259         cp $file $2/$file >/dev/null 2>&1       \
260         || echo "Can't copy $file"
261         chmod +w $2/$file
262         chmod a+r $2/$file
263         sed -e '
264                                    :loop
265           /\\$/                 N
266           /\\$/                 b loop
267           s%^\([        ]*#[    ]*endif[        ]*\)\([^/       ].*\)$%\1/* \2 */%
268           s%^\([        ]*#[    ]*else[         ]*\)\([^/       ].*\)$%\1/* \2 */%
269           /\/\/[^*]/                    s|//\(.*\)$|/*\1*/|
270           /[    ]_IO[A-Z]*[     ]*(/    s/(\(.\),/('\''\1'\'',/
271           /[    ]BSD43__IO[A-Z]*[       ]*(/    s/(\(.\),/('\''\1'\'',/
272           /#define._IO/                 s/'\''\([cgx]\)'\''/\1/g
273           /#define.BSD43__IO/           s/'\''\([cgx]\)'\''/\1/g
274           /[^A-Z]CTRL[  ]*(/            s/\([^'\'']\))/'\''\1'\'')/
275           /#define.CTRL/                s/'\''\([cgx]\)'\''/\1/g
276           /#define._CTRL/               s/'\''\([cgx]\)'\''/\1/g
277           /#define.BSD43_CTRL/          s/'\''\([cgx]\)'\''/\1/g
278           /#[el]*if/{
279                 # Surround each word with spaces, to simplify matching below.
280                 s/[a-zA-Z0-9_][a-zA-Z0-9_]*/ & /g
281
282                 # ANSIfy each pre-ANSI machine-dependent symbol
283                 # by surrounding it with __ __.
284                 s/ bsd4\([0-9]\) / __bsd4\1__ /g
285                 s/ _*i386 / __i386__ /g
286                 s/ is68k / __is68k__ /g
287                 s/ m68k / __m68k__ /g
288                 s/ mc680\([0-9]\)0 / __mc680\10__ /g
289                 s/ news\([0-9]*\) / __news\1__ /g
290                 s/ ns32000 / __ns32000__ /g
291                 s/ pyr / __pyr__ /g
292                 s/ sony_news / __sony_news__ /g
293                 s/ sparc / __sparc__ /g
294                 s/ sun\([a-z0-9]*\) / __sun\1__ /g
295                 s/ unix / __unix__ /g
296                 s/ vax / __vax__ /g
297
298                 # Remove the spaces that we inserted around each word.
299                 s/ \([a-zA-Z0-9_][a-zA-Z0-9_]*\) /\1/g
300           }
301           /^#define.NULL[       ]/      i\
302                 #undef NULL
303         ' $2/$file > $2/$file.sed
304         mv $2/$file.sed $2/$file
305         if cmp $file $2/$file >/dev/null 2>&1; then
306            rm $2/$file
307         else
308            echo Fixed $file
309         fi
310       fi
311     fi
312   done
313   shift; shift
314 done
315
316 cd ${INPUT}
317
318 # Install the proper definition of size_t in header files that it comes from.
319 for file in sys/types.h sys/stdtypes.h;
320 do
321   if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
322     cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
323     chmod +w ${LIB}/$file 2>/dev/null
324     chmod a+r ${LIB}/$file 2>/dev/null
325   fi
326
327   if [ -r ${LIB}/$file ]; then
328     echo Fixing $file comment
329     # Extract the definition of SIZE_TYPE, if any.
330     # (This file must be called something.c).
331     echo "#include \"tm.h\"
332 gobblegobble SIZE_TYPE" > ${LIB}/types.c
333     foo=`cc -E -I${ORIGDIR} -I${SRCDIR} -I${SRCDIR}/config ${LIB}/types.c | grep gobblegobble | sed -e "s/gobblegobble[         ]*//"`
334     rm -f ${LIB}/types.c
335     # Default to our preferred type.
336     if [ "$foo" = SIZE_TYPE ]; then foo="unsigned long int"; else foo=`echo $foo | sed -e 's/^.*"\(.*\)".*$/\1/'`; fi
337     sed -e "s/typedef[  a-z_]*[         ]size_t/typedef $foo size_t/" ${LIB}/$file > ${LIB}/${file}.sed
338     rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
339     if cmp $file ${LIB}/$file >/dev/null 2>&1; then
340       rm ${LIB}/$file
341     fi
342   fi
343 done
344
345 # Fix one other error in this file: a mismatched quote not inside a C comment.
346 file=sundev/vuid_event.h
347 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
348   mkdir ${LIB}/sundev 2>/dev/null
349   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
350   chmod +w ${LIB}/$file 2>/dev/null
351   chmod a+r ${LIB}/$file 2>/dev/null
352 fi
353
354 if [ -r ${LIB}/$file ]; then
355   echo Fixing $file comment
356   sed -e "s/doesn't/does not/" ${LIB}/$file > ${LIB}/${file}.sed
357   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
358   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
359     rm ${LIB}/$file
360   fi
361 fi
362
363 # Fix these Sun OS files to avoid an invalid identifier in an #ifdef.
364 file=sunwindow/win_cursor.h
365 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
366 #  mkdir ${LIB}/sunwindow 2>/dev/null
367   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
368   chmod +w ${LIB}/$file 2>/dev/null
369 fi
370 if [ -r ${LIB}/$file ]; then
371   echo Fixing $file
372   sed -e "s/ecd.cursor/ecd_cursor/" ${LIB}/$file > ${LIB}/${file}.sed
373   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
374   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
375     rm ${LIB}/$file
376   fi
377 fi
378 file=sunwindow/win_lock.h
379 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
380 #  mkdir ${LIB}/sunwindow 2>/dev/null
381   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
382   chmod +w ${LIB}/$file 2>/dev/null
383 fi
384 if [ -r ${LIB}/$file ]; then
385   echo Fixing $file
386   sed -e "s/ecd.cursor/ecd_cursor/" ${LIB}/$file > ${LIB}/${file}.sed
387   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
388   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
389     rm ${LIB}/$file
390   fi
391 fi
392
393 # Fix this Sun file to avoid interfering with stddef.h.
394 file=sys/stdtypes.h
395 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
396   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
397   chmod +w ${LIB}/$file 2>/dev/null
398   chmod a+r ${LIB}/$file 2>/dev/null
399 fi
400
401 if [ -r ${LIB}/$file ]; then
402   echo Fixing $file
403 sed -e '/size_t.*;/i\
404 #ifndef _GCC_SIZE_T\
405 #define _GCC_SIZE_T' \
406     -e '/size_t.*;/a\
407 #endif' \
408     -e '/ptrdiff_t.*;/i\
409 #ifndef _GCC_PTRDIFF_T\
410 #define _GCC_PTRDIFF_T' \
411     -e '/ptrdiff_t.*;/a\
412 #endif' \
413     -e '/wchar_t.*;/i\
414 #ifndef _GCC_WCHAR_T\
415 #define _GCC_WCHAR_T' \
416     -e '/wchar_t.*;/a\
417 #endif' ${LIB}/$file > ${LIB}/${file}.sed
418   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
419   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
420     rm ${LIB}/$file
421   fi
422 fi
423
424 # Fix this file to avoid interfering with stddef.h, but don't mistakenly
425 # match e.g. ssize_t present in AIX for the ps/2.
426 file=sys/types.h
427 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
428   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
429   chmod +w ${LIB}/$file 2>/dev/null
430   chmod a+r ${LIB}/$file 2>/dev/null
431 fi
432
433 if [ -r ${LIB}/$file ]; then
434   echo Fixing $file
435 sed -e '/[      ]size_t.*;/i\
436 #ifndef _GCC_SIZE_T\
437 #define _GCC_SIZE_T' \
438     -e '/[      ]size_t.*;/a\
439 #endif' ${LIB}/$file > ${LIB}/${file}.sed
440   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
441   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
442     rm ${LIB}/$file
443   fi
444 fi
445
446 # Fix an error in this file: the #if says _cplusplus, not the double
447 # underscore __cplusplus that it should be
448 file=tinfo.h
449 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
450   mkdir ${LIB}/rpcsvc 2>/dev/null
451   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
452   chmod +w ${LIB}/$file 2>/dev/null
453   chmod a+r ${LIB}/$file 2>/dev/null
454 fi
455
456 if [ -r ${LIB}/$file ]; then
457   echo Fixing $file, __cplusplus macro
458   sed -e 's/[   ]_cplusplus/ __cplusplus/' ${LIB}/$file > ${LIB}/${file}.sed
459   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
460   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
461     rm ${LIB}/$file
462   fi
463 fi
464
465 # Fix an error in this file: a missing semi-colon at the end of the statsswtch
466 # structure definition.
467 file=rpcsvc/rstat.h
468 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
469   mkdir ${LIB}/rpcsvc 2>/dev/null
470   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
471   chmod +w ${LIB}/$file 2>/dev/null
472   chmod a+r ${LIB}/$file 2>/dev/null
473 fi
474
475 if [ -r ${LIB}/$file ]; then
476   echo Fixing $file, definition of statsswtch
477   sed -e 's/boottime$/boottime;/' ${LIB}/$file > ${LIB}/${file}.sed
478   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
479   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
480     rm ${LIB}/$file
481   fi
482 fi
483
484 # Fix an error in this file: a missing semi-colon at the end of the nodeent
485 # structure definition.
486 file=netdnet/dnetdb.h
487 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
488   mkdir ${LIB}/netdnet 2>/dev/null
489   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
490   chmod +w ${LIB}/$file 2>/dev/null
491   chmod a+r ${LIB}/$file 2>/dev/null
492 fi
493
494 if [ -r ${LIB}/$file ]; then
495   echo Fixing $file, definition of nodeent
496   sed -e 's/char.*na_addr *$/char *na_addr;/' ${LIB}/$file > ${LIB}/${file}.sed
497   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
498   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
499     rm ${LIB}/$file
500   fi
501 fi
502
503 # Check for bad #ifdef line (in Ultrix 4.1)
504 file=sys/file.h
505 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
506   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
507   chmod +w ${LIB}/$file 2>/dev/null
508   chmod a+r ${LIB}/$file 2>/dev/null
509 fi
510
511 if [ -r ${LIB}/$file ]; then
512   echo Fixing $file, bad \#ifdef line
513   sed -e 's/#ifdef KERNEL/#if defined(KERNEL)/' ${LIB}/$file > ${LIB}/${file}.sed
514   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
515   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
516     rm ${LIB}/$file
517   fi
518 fi
519
520 # Remove nested comments created by #endifs in a comment (Ultrix 4.1)
521 # Only needed if commenting out junk after #endif.
522 file=signal.h
523 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
524   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
525   chmod +w ${LIB}/$file 2>/dev/null
526   chmod a+r ${LIB}/$file 2>/dev/null
527 fi
528
529 if [ -r ${LIB}/$file ]; then
530   echo Fixing $file, nested comments
531   sed -e 's/#endif.*/#endif/' ${LIB}/$file > ${LIB}/${file}.sed
532   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
533   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
534     rm -f ${LIB}/$file
535   fi
536 fi
537
538 # Check for superfluous `static' (in Ultrix 4.2)
539 file=machine/cpu.h
540 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
541   mkdir ${LIB}/machine 2>/dev/null
542   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
543   chmod +w ${LIB}/$file 2>/dev/null
544   chmod a+r ${LIB}/$file 2>/dev/null
545 fi
546
547 if [ -r ${LIB}/$file ]; then
548   echo Fixing $file, superfluous static
549   sed -e 's/^static struct tlb_pid_state/struct tlb_pid_state/' ${LIB}/$file > ${LIB}/${file}.sed
550   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
551   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
552     rm ${LIB}/$file
553   else
554 # This file has an alternative name, mips/cpu.h.  Fix that name, too.
555     if cmp machine/cpu.h mips/cpu.h > /dev/null 2>& 1; then
556       mkdir ${LIB}/mips 2>&-
557       ln ${LIB}/$file ${LIB}/mips/cpu.h 
558     fi
559   fi
560 fi
561
562 # Incorrect sprintf declaration in X11/Xmu.h
563 file=X11/Xmu.h
564 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
565   mkdir ${LIB}/X11 2>/dev/null
566   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
567   chmod +w ${LIB}/$file 2>/dev/null
568   chmod a+r ${LIB}/$file 2>/dev/null
569 fi
570
571 if [ -r ${LIB}/$file ]; then
572   echo Fixing $file sprintf declaration
573   sed -e 's,^extern char \*     sprintf();$,#ifndef __STDC__\
574 extern char *   sprintf();\
575 #endif /* !defined __STDC__ */,' ${LIB}/$file > ${LIB}/${file}.sed
576   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
577   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
578     rm ${LIB}/$file
579   fi
580 fi
581
582 # Incorrect sprintf declaration in X11/Xmu/Xmu.h
583 # (It's not clear whether the right file name is this or X11/Xmu.h.)
584 file=X11/Xmu/Xmu.h
585 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
586   mkdir ${LIB}/X11/Xmu 2>/dev/null
587   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
588   chmod +w ${LIB}/$file 2>/dev/null
589   chmod a+r ${LIB}/$file 2>/dev/null
590 fi
591
592 if [ -r ${LIB}/$file ]; then
593   echo Fixing $file sprintf declaration
594   sed -e 's,^extern char \*     sprintf();$,#ifndef __STDC__\
595 extern char *   sprintf();\
596 #endif /* !defined __STDC__ */,' ${LIB}/$file > ${LIB}/${file}.sed
597   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
598   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
599     rm ${LIB}/$file
600   fi
601 fi
602
603 # Check for missing ';' in struct
604 file=netinet/ip.h
605 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
606   mkdir ${LIB}/netinet 2>/dev/null
607   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
608   chmod +w ${LIB}/$file 2>/dev/null
609   chmod a+r ${LIB}/$file 2>/dev/null
610 fi
611
612 if [ -r ${LIB}/$file ]; then
613   echo Fixing $file
614   sed -e '/^struct/,/^};/s/}$/};/' ${LIB}/$file > ${LIB}/${file}.sed
615   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
616   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
617     rm -f ${LIB}/$file
618   fi
619 fi
620
621 # Fix the CAT macro in SunOS memvar.h.
622 file=pixrect/memvar.h
623 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
624   mkdir ${LIB}/pixrect 2>/dev/null
625   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
626   chmod +w ${LIB}/$file 2>/dev/null
627   chmod a+r ${LIB}/$file 2>/dev/null
628 fi
629
630 if [ -r ${LIB}/$file ]; then
631   echo Fixing $file
632   sed -e '/^#define.CAT(a,b)/ i\
633 #ifdef __STDC__ \
634 #define CAT(a,b) a##b\
635 #else
636 /^#define.CAT(a,b)/ a\
637 #endif
638 ' ${LIB}/$file > ${LIB}/${file}.sed
639   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
640   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
641     rm -f ${LIB}/$file
642   fi
643 fi
644
645 # Check for yet more missing ';' in struct (in SunOS 4.0.x)
646 file=rpcsvc/rusers.h
647 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
648   mkdir ${LIB}/rpcsvc 2>/dev/null
649   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
650   chmod +w ${LIB}/$file 2>/dev/null
651   chmod a+r ${LIB}/$file 2>/dev/null
652 fi
653
654 if [ -r ${LIB}/$file ]; then
655   echo Fixing $file
656   sed -e '/^struct/,/^};/s/_cnt$/_cnt;/' ${LIB}/$file > ${LIB}/${file}.sed
657   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
658   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
659     rm -f ${LIB}/$file
660   fi
661 fi
662
663 # Fix return type of exit and abort in <stdlib.h> on SunOS 4.1.
664 file=stdlib.h
665 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
666   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
667   chmod +w ${LIB}/$file 2>/dev/null
668   chmod a+r ${LIB}/$file 2>/dev/null
669 fi
670
671 if [ -r ${LIB}/$file ]; then
672   echo Fixing $file
673   sed -e 's/int abort/void      abort/g' \
674   -e 's/int     free/void       free/g' \
675   -e 's/char \* calloc/void \*  calloc/g' \
676   -e 's/char \* malloc/void \*  malloc/g' \
677   -e 's/char \* realloc/void \* realloc/g' \
678   -e 's/int     exit/void       exit/g' ${LIB}/$file > ${LIB}/${file}.sed
679   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
680   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
681     rm -f ${LIB}/$file
682   fi
683 fi
684
685 # Fix return type of free and {c,m,re}alloc in <malloc.h> on SunOS 4.1.
686 file=malloc.h
687 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
688   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
689   chmod +w ${LIB}/$file 2>/dev/null
690   chmod a+r ${LIB}/$file 2>/dev/null
691 fi
692
693 if [ -r ${LIB}/$file ]; then
694   echo Fixing $file
695   sed -e 's/typedef[    ]char \*        malloc_t/typedef void \*        malloc_t/g' \
696   -e 's/int[    ][      ]*free/void     free/g' \
697   ${LIB}/$file > ${LIB}/${file}.sed
698   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
699   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
700     rm -f ${LIB}/$file
701   fi
702 fi
703
704
705 # Fix bogus comment in <locale.h> on SunOS 4.1.
706 file=locale.h
707 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
708   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
709   chmod +w ${LIB}/$file 2>/dev/null
710   chmod a+r ${LIB}/$file 2>/dev/null
711 fi
712
713 if [ -r ${LIB}/$file ]; then
714   echo Fixing $file
715   sed -e 's%#endif / \*%#endif /\* %g' ${LIB}/$file > ${LIB}/${file}.sed
716   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
717   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
718     rm -f ${LIB}/$file
719   fi
720 fi
721
722 # Fix bogus #ifdef in <hsfs/hsfs_spec.h> on SunOS 4.1.
723 file=hsfs/hsfs_spec.h
724 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
725   mkdir ${LIB}/hsfs 2>/dev/null
726   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
727   chmod +w ${LIB}/$file 2>/dev/null
728   chmod a+r ${LIB}/$file 2>/dev/null
729 fi
730
731 if [ -r ${LIB}/$file ]; then
732   echo Fixing $file
733   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
734     ${LIB}/$file > ${LIB}/${file}.sed
735   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
736   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
737     rm -f ${LIB}/$file
738   fi
739 fi
740
741 # Fix bogus #ifdef in <hsfs/hsnode.h> on SunOS 4.1.
742 file=hsfs/hsnode.h
743 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
744   mkdir ${LIB}/hsfs 2>/dev/null
745   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
746   chmod +w ${LIB}/$file 2>/dev/null
747   chmod a+r ${LIB}/$file 2>/dev/null
748 fi
749
750 if [ -r ${LIB}/$file ]; then
751   echo Fixing $file
752   sed -e 's/\#ifdef __i386__ || __sun4c__/\#if __i386__ || __sun4c__/g' \
753     ${LIB}/$file > ${LIB}/${file}.sed
754   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
755   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
756     rm -f ${LIB}/$file
757   fi
758 fi
759
760 # Fix bogus #ifdef in <hsfs/iso_spec.h> on SunOS 4.1.
761 file=hsfs/iso_spec.h
762 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
763   mkdir ${LIB}/hsfs 2>/dev/null
764   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
765   chmod +w ${LIB}/$file 2>/dev/null
766   chmod a+r ${LIB}/$file 2>/dev/null
767 fi
768
769 if [ -r ${LIB}/$file ]; then
770   echo Fixing $file
771   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
772     ${LIB}/$file > ${LIB}/${file}.sed
773   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
774   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
775     rm -f ${LIB}/$file
776   fi
777 fi
778
779 # Incorrect #include in Sony News-OS 3.2.
780 file=machine/machparam.h
781 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
782   mkdir ${LIB}/machine 2>/dev/null
783   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
784   chmod +w ${LIB}/$file 2>/dev/null
785   chmod a+r ${LIB}/$file 2>/dev/null
786 fi
787
788 if [ -r ${LIB}/$file ]; then
789   echo Fixing $file, incorrect \#include
790   sed -e 's@"../machine/endian.h"@<machine/endian.h>@' \
791     ${LIB}/$file > ${LIB}/${file}.sed
792   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
793   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
794     rm -f ${LIB}/$file
795   fi
796 fi
797
798 # Multiline comment after typedef on IRIX 4.0.1.
799 file=sys/types.h
800 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
801   mkdir ${LIB}/sys 2>/dev/null
802   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
803   chmod +w ${LIB}/$file 2>/dev/null
804   chmod a+r ${LIB}/$file 2>/dev/null
805 fi
806
807 if [ -r ${LIB}/$file ]; then
808   echo Fixing $file, comment in the middle of \#ifdef
809   sed -e 's@type of the result@type of the result */@' \
810     -e 's@of the sizeof@/* of the sizeof@' \
811     ${LIB}/$file > ${LIB}/${file}.sed
812   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
813   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
814     rm -f ${LIB}/$file
815   fi
816 fi
817
818 # Fix non-ANSI memcpy declaration that conflicts with gcc's builtin
819 # declaration on Sun OS 4.x.  We must only fix this on Sun OS 4.x, because
820 # many other systems have similar text but correct versions of the file.
821 # To ensure only Sun's is fixed, we grep for a likely unique string.
822 file=memory.h
823 if egrep '/\*   @\(#\)memory\.h 1\.[2-4] 8./../.. SMI; from S5R2 1\.2   \*/' $file > /dev/null; then
824   if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
825     cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
826     chmod +w ${LIB}/$file 2>/dev/null
827     chmod a+r ${LIB}/$file 2>/dev/null
828   fi
829   if [ -r ${LIB}/$file ]; then
830     echo Replacing $file
831     cat > ${LIB}/$file << EOF
832 /* This file was generated by fixincludes */
833 #ifndef __memory_h__
834 #define __memory_h__
835
836 #ifdef __STDC__
837 extern void *memccpy();
838 extern void *memchr();
839 extern void *memcpy();
840 extern void *memset();
841 #else
842 extern char *memccpy();
843 extern char *memchr();
844 extern char *memcpy();
845 extern char *memset();
846 #endif /* __STDC__ */
847
848 extern int memcmp();
849
850 #endif __memory_h__
851 EOF
852   fi
853 fi
854
855 # parameters not const on DECstation Ultrix V4.0.
856 file=stdio.h
857 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
858   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
859   chmod +w ${LIB}/$file 2>/dev/null
860   chmod a+r ${LIB}/$file 2>/dev/null
861 fi
862
863 if [ -r ${LIB}/$file ]; then
864   echo Fixing $file, non-const arg
865   sed -e 's@perror( char \*__s );@perror( const char *__s );@' \
866       -e 's@fputs( char \*__s,@fputs( const char *__s,@' \
867       -e 's@fopen( char \*__filename, char \*__type );@fopen( const char *__filename, const char *__type );@' \
868       -e 's@fwrite( void \*__ptr,@fwrite( const void *__ptr,@' \
869       -e 's@fscanf( FILE \*__stream, char \*__format,@fscanf( FILE *__stream, const char *__format,@' \
870       -e 's@scanf( char \*__format,@scanf( const char *__format,@' \
871       -e 's@sscanf( char \*__s, char \*__format,@sscanf( const char *__s, const char *__format,@' \
872     ${LIB}/$file > ${LIB}/${file}.sed
873   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
874   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
875     rm -f ${LIB}/$file
876   fi
877 fi
878
879 # parameters conflict with C++ new on rs/6000 
880 file=stdio.h
881 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
882   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
883   chmod +w ${LIB}/$file 2>/dev/null
884 fi
885
886 if [ -r ${LIB}/$file ]; then
887   echo Fixing $file, parameter name conflicts
888   sed -e 's@rename(const char \*old, const char \*new)@rename(const char *_old, const char *_new)@' \
889     ${LIB}/$file > ${LIB}/${file}.sed
890   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
891   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
892     rm -f ${LIB}/$file
893   fi
894 fi
895
896 # Don't use or define the name va_list in stdio.h.
897 # This is for ANSI and also to interoperate properly with gvarargs.h.
898 file=stdio.h
899 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
900   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
901   chmod +w ${LIB}/$file 2>/dev/null
902   chmod a+r ${LIB}/$file 2>/dev/null
903 fi
904
905 if [ -r ${LIB}/$file ]; then
906   echo Fixing $file, use of va_list
907   # Arrange for stdio.h to use stdarg.h to define __gnuc_va_list
908   (echo "#define __need___va_list"
909    echo "#include <stdarg.h>") > ${LIB}/${file}.sed
910   # Use __gnuc_va_list in arg types in place of va_list.
911   # On 386BSD use __gnuc_va_list instead of _VA_LIST_. We're hoping the
912   # trailing parentheses and semicolon save all other systems from this.
913   # Define __va_list__ (something harmless and unused) instead of va_list.
914   # Don't claim to have defined va_list.
915   sed -e 's@ va_list @ __gnuc_va_list @' \
916       -e 's@ va_list)@ __gnuc_va_list)@' \
917       -e 's@ _VA_LIST_));@ __gnuc_va_list));@' \
918       -e 's@ va_list@ __va_list__@' \
919       -e 's@\*va_list@*__va_list__@' \
920       -e 's@VA_LIST@DUMMY_VA_LIST@' \
921     ${LIB}/$file >> ${LIB}/${file}.sed
922   
923   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
924   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
925     rm -f ${LIB}/$file
926   fi
927 fi
928
929 # Cancel out ansi_compat.h on Ultrix.  Replace it with empty file.
930 file=ansi_compat.h
931 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
932   if grep -s ULTRIX $file; then
933     echo "/* This file intentionally left blank.  */" > $LIB/$file
934   fi
935 fi
936
937 # parameter to atof not const on DECstation Ultrix V4.0.
938 # also get rid of bogus inline definitions in HP-UX 8.0
939 file=math.h
940 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
941   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
942   chmod +w ${LIB}/$file 2>/dev/null
943   chmod a+r ${LIB}/$file 2>/dev/null
944 fi
945
946 if [ -r ${LIB}/$file ]; then
947   echo Fixing $file, non-const arg
948   sed -e 's@atof( char \*__nptr );@atof( const char *__nptr );@' \
949       -e 's@inline int abs(int d) { return (d>0)?d:-d; }@@' \
950       -e 's@inline double abs(double d) { return fabs(d); }@@' \
951     ${LIB}/$file > ${LIB}/${file}.sed
952   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
953   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
954     rm -f ${LIB}/$file
955   fi
956 fi
957
958 # In limits.h, put #ifndefs around things that are supposed to be defined
959 # in float.h to avoid redefinition errors if float.h is included first.
960 file=limits.h
961 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
962   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
963   chmod +w ${LIB}/$file 2>/dev/null
964   chmod a+r ${LIB}/$file 2>/dev/null
965 fi
966
967 if [ -r ${LIB}/$file ]; then
968   echo Fixing $file
969   sed -e '/[    ]FLT_MIN[       ]/i\
970 #ifndef FLT_MIN'\
971       -e '/[    ]FLT_MIN[       ]/a\
972 #endif'\
973       -e '/[    ]FLT_MAX[       ]/i\
974 #ifndef FLT_MAX'\
975       -e '/[    ]FLT_MAX[       ]/a\
976 #endif'\
977       -e '/[    ]FLT_DIG[       ]/i\
978 #ifndef FLT_DIG'\
979       -e '/[    ]FLT_DIG[       ]/a\
980 #endif'\
981       -e '/[    ]DBL_MIN[       ]/i\
982 #ifndef DBL_MIN'\
983       -e '/[    ]DBL_MIN[       ]/a\
984 #endif'\
985       -e '/[    ]DBL_MAX[       ]/i\
986 #ifndef DBL_MAX'\
987       -e '/[    ]DBL_MAX[       ]/a\
988 #endif'\
989       -e '/[    ]DBL_DIG[       ]/i\
990 #ifndef DBL_DIG'\
991       -e '/[    ]DBL_DIG[       ]/a\
992 #endif'\
993     ${LIB}/$file > ${LIB}/${file}.sed
994   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
995   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
996     echo Deleting ${LIB}/$file\; no fixes were needed.
997     rm -f ${LIB}/$file
998   fi
999 fi
1000
1001 # These two files on SunOS 4 are included by other files
1002 # in the same directory, using "...".  So we must make sure they exist
1003 # in the same directory as the other fixed files.
1004 if [ -r ${INPUT}/multimedia/audio_errno.h ]
1005 then
1006   ln -s ${INPUT}/multimedia/audio_errno.h ${LIB}/multimedia 2>/dev/null
1007 fi
1008 if [ -r ${INPUT}/multimedia/audio_hdr.h ]
1009 then
1010   ln -s ${INPUT}/multimedia/audio_hdr.h ${LIB}/multimedia 2>/dev/null
1011 fi
1012
1013 echo 'Removing unneeded directories:'
1014 cd $LIB
1015 files=`find . -type d -print | sort -r`
1016 for file in $files; do
1017   rmdir $LIB/$file > /dev/null 2>&1
1018 done
1019
1020 if $LINKS; then
1021   echo 'Making internal symbolic non-directory links'
1022   cd ${INPUT}
1023   files=`find . -type l -print`
1024   for file in $files; do
1025     dest=`ls -ld $file | sed -n 's/.*-> //p'`
1026     if expr "$dest" : '[^/].*' > /dev/null; then    
1027       target=${LIB}/`echo file | sed "s|[^/]*\$|$dest|"`
1028       if [ -f $target ]; then
1029         ln -s $dest ${LIB}/$file >/dev/null 2>&1
1030       fi
1031     fi
1032   done
1033 fi
1034
1035 echo 'Cleaning up DONE files.'
1036 cd $LIB
1037 find . -name DONE -exec rm -f {} ';'
1038
1039 exit 0