OSDN Git Service

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