OSDN Git Service

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