OSDN Git Service

(sys/types.h): Replace definition of size_t based on SIZE_TYPE.
[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                 dupdir=${LIB}/root$x/`echo $file2 | sed -n "s|^${file}/||p"`
180                 echo "Duplicating ${file}'s ${dupdir}"
181                 if [ -d ${dupdir} ]
182                 then true
183                 else
184                   mkdir ${dupdir}
185                 fi
186                 ;;
187               *)
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       if [ -r $file ]; then
234         cp $file $2/$file >/dev/null 2>&1       \
235         || echo "Can't copy $file"
236         chmod +w $2/$file
237         chmod a+r $2/$file
238 # Following two lines removed.
239 #         s%^\([        ]*#[    ]*endif[        ]*\)\([^/       ].*\)$%\1/* \2 */%
240 #         s%^\([        ]*#[    ]*else[         ]*\)\([^/       ].*\)$%\1/* \2 */%
241
242         sed -e '
243                                    :loop
244           /\\$/                 N
245           /\\$/                 b loop
246           /\/\/[^*]/                    s|//\(.*\)$|/*\1*/|
247           /[    ]_IO[A-Z]*[     ]*(/    s/(\(.\),/('\''\1'\'',/
248           /[    ]BSD43__IO[A-Z]*[       ]*(/    s/(\(.\),/('\''\1'\'',/
249           /#define._IO/                 s/'\''\([cgx]\)'\''/\1/g
250           /#define.BSD43__IO/           s/'\''\(]cgx]\)'\''/\1/g
251           /[^A-Z]CTRL[  ]*(/            s/\([^'\'']\))/'\''\1'\'')/
252           /#define.CTRL/                s/'\''\([cgx]\)'\''/\1/g
253           /#define._CTRL/               s/'\''\([cgx]\)'\''/\1/g
254           /#define.BSD43_CTRL/          s/'\''\([cgx]\)'\''/\1/g
255           /#[a-z]*if.*[  (]m68k/        s/\([^_]\)m68k/\1__m68k__/g
256           /#[a-z]*if.*[  (]__i386/      s/__i386/__i386__/g
257           /#[a-z]*if.*[  (]i386/        s/\([^_]\)i386/\1__i386__/g
258           /#[a-z]*if.*[  (]sparc/       s/\([^_]\)sparc/\1__sparc__/g
259           /#[a-z]*if.*[  (]mc68000/     s/\([^_]\)mc68000/\1__mc68000__/g
260           /#[a-z]*if.*[  (]vax/         s/\([^_]\)vax/\1__vax__/g
261           /#[a-z]*if.*[  (]sun/         s/\([^_]\)\(sun[a-z0-9]*\)\([^a-z0-9_]\)/\1__\2__\3/g
262           /#[a-z]*if.*[  (]sun/         s/\([^_]\)\(sun[a-z0-9]*\)$/\1__\2__/g
263           /#[a-z]*if.*[  (]ns32000/     s/\([^_]\)ns32000/\1__ns32000__/g
264           /#[a-z]*if.*[  (]pyr/         s/\([^_]\)pyr/\1__pyr__/g
265           /#[a-z]*if.*[  (]is68k/       s/\([^_]\)is68k/\1__is68k__/g
266           /^#define.NULL[       ]/      i\
267                 #undef NULL
268         ' $2/$file > $2/$file.sed
269         mv $2/$file.sed $2/$file
270         if cmp $file $2/$file >/dev/null 2>&1; then
271            rm $2/$file
272         else
273            echo Fixed $file
274         fi
275       fi
276     fi
277   done
278   shift; shift
279 done
280
281 cd ${INPUT}
282
283 # Install the proper definition of size_t in header files that it comes from.
284 for file in sys/types.h sys/stdtypes.h;
285 do
286   if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
287     cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
288     chmod +w ${LIB}/$file 2>/dev/null
289     chmod a+r ${LIB}/$file 2>/dev/null
290   fi
291
292   if [ -r ${LIB}/$file ]; then
293     echo Fixing $file comment
294     # Extract the definition of SIZE_TYPE, if any.
295     # (This file must be called something.c).
296     echo "#include \"tm.h\"
297 gobblegobble SIZE_TYPE" > types.c
298     foo=`cc -E -I. -I$SRCDIR -I$SRCDIR/config types.c | grep gobblegobble | sed -e "s/gobblegobble[     ]*//"`
299     rm -f types.c
300     # Default to our preferred type.
301     if [ $foo = SIZE_TYPE ]; then foo="unsigned long int"; else true; fi
302     sed -e "s/typedef[  a-z_]*[         ]size_t/typedef $foo size_t/" ${LIB}/$file > ${LIB}/${file}.sed
303     rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
304     if cmp $file ${LIB}/$file >/dev/null 2>&1; then
305       rm ${LIB}/$file
306     fi
307   fi
308 done
309
310 # Fix one other error in this file: a mismatched quote not inside a C comment.
311 file=sundev/vuid_event.h
312 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
313   mkdir ${LIB}/sundev 2>/dev/null
314   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
315   chmod +w ${LIB}/$file 2>/dev/null
316   chmod a+r ${LIB}/$file 2>/dev/null
317 fi
318
319 if [ -r ${LIB}/$file ]; then
320   echo Fixing $file comment
321   sed -e "s/doesn't/does not/" ${LIB}/$file > ${LIB}/${file}.sed
322   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
323   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
324     rm ${LIB}/$file
325   fi
326 fi
327
328 # Fix these Sun OS files to avoid an invalid identifier in an #ifdef.
329 file=sunwindow/win_cursor.h
330 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
331 #  mkdir ${LIB}/sunwindow 2>/dev/null
332   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
333   chmod +w ${LIB}/$file 2>/dev/null
334 fi
335 if [ -r ${LIB}/$file ]; then
336   echo Fixing $file
337   sed -e "s/ecd.cursor/ecd_cursor/" ${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 file=sunwindow/win_lock.h
344 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
345 #  mkdir ${LIB}/sunwindow 2>/dev/null
346   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
347   chmod +w ${LIB}/$file 2>/dev/null
348 fi
349 if [ -r ${LIB}/$file ]; then
350   echo Fixing $file
351   sed -e "s/ecd.cursor/ecd_cursor/" ${LIB}/$file > ${LIB}/${file}.sed
352   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
353   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
354     rm ${LIB}/$file
355   fi
356 fi
357
358 # Fix this Sun file to avoid interfering with stddef.h.
359 file=sys/stdtypes.h
360 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
361   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
362   chmod +w ${LIB}/$file 2>/dev/null
363   chmod a+r ${LIB}/$file 2>/dev/null
364 fi
365
366 if [ -r ${LIB}/$file ]; then
367   echo Fixing $file
368 sed -e '/size_t.*;/i\
369 #ifndef _GCC_SIZE_T\
370 #define _GCC_SIZE_T' \
371     -e '/size_t.*;/a\
372 #endif' \
373     -e '/ptrdiff_t.*;/i\
374 #ifndef _GCC_PTRDIFF_T\
375 #define _GCC_PTRDIFF_T' \
376     -e '/ptrdiff_t.*;/a\
377 #endif' \
378     -e '/wchar_t.*;/i\
379 #ifndef _GCC_WCHAR_T\
380 #define _GCC_WCHAR_T' \
381     -e '/wchar_t.*;/a\
382 #endif' ${LIB}/$file > ${LIB}/${file}.sed
383   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
384   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
385     rm ${LIB}/$file
386   fi
387 fi
388
389 # Fix this file to avoid interfering with stddef.h, but don't mistakenly
390 # match e.g. ssize_t present in AIX for the ps/2.
391 file=sys/types.h
392 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
393   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
394   chmod +w ${LIB}/$file 2>/dev/null
395   chmod a+r ${LIB}/$file 2>/dev/null
396 fi
397
398 if [ -r ${LIB}/$file ]; then
399   echo Fixing $file
400 sed -e '/[      ]size_t.*;/i\
401 #ifndef _GCC_SIZE_T\
402 #define _GCC_SIZE_T' \
403     -e '/[      ]size_t.*;/a\
404 #endif' ${LIB}/$file > ${LIB}/${file}.sed
405   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
406   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
407     rm ${LIB}/$file
408   fi
409 fi
410
411 # Fix an error in this file: the #if says _cplusplus, not the double
412 # underscore __cplusplus that it should be
413 file=tinfo.h
414 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
415   mkdir ${LIB}/rpcsvc 2>/dev/null
416   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
417   chmod +w ${LIB}/$file 2>/dev/null
418   chmod a+r ${LIB}/$file 2>/dev/null
419 fi
420
421 if [ -r ${LIB}/$file ]; then
422   echo Fixing $file, __cplusplus macro
423   sed -e 's/[   ]_cplusplus/ __cplusplus/' ${LIB}/$file > ${LIB}/${file}.sed
424   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
425   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
426     rm ${LIB}/$file
427   fi
428 fi
429
430 # Fix an error in this file: a missing semi-colon at the end of the statsswtch
431 # structure definition.
432 file=rpcsvc/rstat.h
433 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
434   mkdir ${LIB}/rpcsvc 2>/dev/null
435   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
436   chmod +w ${LIB}/$file 2>/dev/null
437   chmod a+r ${LIB}/$file 2>/dev/null
438 fi
439
440 if [ -r ${LIB}/$file ]; then
441   echo Fixing $file, definition of statsswtch
442   sed -e 's/boottime$/boottime;/' ${LIB}/$file > ${LIB}/${file}.sed
443   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
444   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
445     rm ${LIB}/$file
446   fi
447 fi
448
449 # Fix an error in this file: a missing semi-colon at the end of the nodeent
450 # structure definition.
451 file=netdnet/dnetdb.h
452 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
453   mkdir ${LIB}/netdnet 2>/dev/null
454   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
455   chmod +w ${LIB}/$file 2>/dev/null
456   chmod a+r ${LIB}/$file 2>/dev/null
457 fi
458
459 if [ -r ${LIB}/$file ]; then
460   echo Fixing $file, definition of nodeent
461   sed -e 's/char.*na_addr *$/char *na_addr;/' ${LIB}/$file > ${LIB}/${file}.sed
462   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
463   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
464     rm ${LIB}/$file
465   fi
466 fi
467
468 # Check for bad #ifdef line (in Ultrix 4.1)
469 file=sys/file.h
470 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
471   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
472   chmod +w ${LIB}/$file 2>/dev/null
473   chmod a+r ${LIB}/$file 2>/dev/null
474 fi
475
476 if [ -r ${LIB}/$file ]; then
477   echo Fixing $file, bad \#ifdef line
478   sed -e 's/#ifdef KERNEL/#if defined(KERNEL)/' ${LIB}/$file > ${LIB}/${file}.sed
479   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
480   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
481     rm ${LIB}/$file
482   fi
483 fi
484
485 # Remove nested comments created by #endifs in a comment (Ultrix 4.1)
486 # Only needed if commenting out junk after #endif.
487 #file=signal.h
488 #if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
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, nested comments
496 #  sed -e 's/#endif.*/#endif/' ${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 -f ${LIB}/$file
500 #  fi
501 #fi
502
503 # Check for superfluous `static' (in Ultrix 4.2)
504 file=machine/cpu.h
505 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
506   mkdir ${LIB}/machine 2>/dev/null
507   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
508   chmod +w ${LIB}/$file 2>/dev/null
509   chmod a+r ${LIB}/$file 2>/dev/null
510 fi
511
512 if [ -r ${LIB}/$file ]; then
513   echo Fixing $file, superfluous static
514   sed -e 's/^static struct tlb_pid_state/struct tlb_pid_state/' ${LIB}/$file > ${LIB}/${file}.sed
515   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
516   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
517     rm ${LIB}/$file
518   else
519 # This file has an alternative name, mips/cpu.h.  Fix that name, too.
520     if cmp machine/cpu.h mips/cpu.h > /dev/null 2>& 1; then
521       mkdir ${LIB}/mips 2>&-
522       ln ${LIB}/$file ${LIB}/mips/cpu.h 
523     fi
524   fi
525 fi
526
527 # Incorrect sprintf declaration in X11/Xmu.h
528 file=X11/Xmu.h
529 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
530   mkdir ${LIB}/X11 2>/dev/null
531   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
532   chmod +w ${LIB}/$file 2>/dev/null
533   chmod a+r ${LIB}/$file 2>/dev/null
534 fi
535
536 if [ -r ${LIB}/$file ]; then
537   echo Fixing $file sprintf declaration
538   sed -e 's,^extern char \*     sprintf();$,#ifndef __STDC__\
539 extern char *   sprintf();\
540 #endif /* !defined __STDC__ */,' ${LIB}/$file > ${LIB}/${file}.sed
541   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
542   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
543     rm ${LIB}/$file
544   fi
545 fi
546
547 # Incorrect sprintf declaration in X11/Xmu/Xmu.h
548 # (It's not clear whether the right file name is this or X11/Xmu.h.)
549 file=X11/Xmu/Xmu.h
550 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
551   mkdir ${LIB}/X11/Xmu 2>/dev/null
552   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
553   chmod +w ${LIB}/$file 2>/dev/null
554   chmod a+r ${LIB}/$file 2>/dev/null
555 fi
556
557 if [ -r ${LIB}/$file ]; then
558   echo Fixing $file sprintf declaration
559   sed -e 's,^extern char \*     sprintf();$,#ifndef __STDC__\
560 extern char *   sprintf();\
561 #endif /* !defined __STDC__ */,' ${LIB}/$file > ${LIB}/${file}.sed
562   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
563   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
564     rm ${LIB}/$file
565   fi
566 fi
567
568 # Check for missing ';' in struct
569 file=netinet/ip.h
570 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
571   mkdir ${LIB}/netinet 2>/dev/null
572   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
573   chmod +w ${LIB}/$file 2>/dev/null
574   chmod a+r ${LIB}/$file 2>/dev/null
575 fi
576
577 if [ -r ${LIB}/$file ]; then
578   echo Fixing $file
579   sed -e '/^struct/,/^};/s/}$/};/' ${LIB}/$file > ${LIB}/${file}.sed
580   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
581   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
582     rm -f ${LIB}/$file
583   fi
584 fi
585
586 # Fix the CAT macro in SunOS memvar.h.
587 file=pixrect/memvar.h
588 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
589   mkdir ${LIB}/pixrect 2>/dev/null
590   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
591   chmod +w ${LIB}/$file 2>/dev/null
592   chmod a+r ${LIB}/$file 2>/dev/null
593 fi
594
595 if [ -r ${LIB}/$file ]; then
596   echo Fixing $file
597   sed -e '/^#define.CAT(a,b)/ i\
598 #ifdef __STDC__ \
599 #define CAT(a,b) a##b\
600 #else
601 /^#define.CAT(a,b)/ a\
602 #endif
603 ' ${LIB}/$file > ${LIB}/${file}.sed
604   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
605   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
606     rm -f ${LIB}/$file
607   fi
608 fi
609
610 # Check for yet more missing ';' in struct (in SunOS 4.0.x)
611 file=rpcsvc/rusers.h
612 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
613   mkdir ${LIB}/rpcsvc 2>/dev/null
614   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
615   chmod +w ${LIB}/$file 2>/dev/null
616   chmod a+r ${LIB}/$file 2>/dev/null
617 fi
618
619 if [ -r ${LIB}/$file ]; then
620   echo Fixing $file
621   sed -e '/^struct/,/^};/s/_cnt$/_cnt;/' ${LIB}/$file > ${LIB}/${file}.sed
622   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
623   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
624     rm -f ${LIB}/$file
625   fi
626 fi
627
628 # Fix return type of exit and abort in <stdlib.h> on SunOS 4.1.
629 file=stdlib.h
630 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
631   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
632   chmod +w ${LIB}/$file 2>/dev/null
633   chmod a+r ${LIB}/$file 2>/dev/null
634 fi
635
636 if [ -r ${LIB}/$file ]; then
637   echo Fixing $file
638   sed -e 's/int abort/void      abort/g' \
639   -e 's/int     free/void       free/g' \
640   -e 's/char \* calloc/void \*  calloc/g' \
641   -e 's/char \* malloc/void \*  malloc/g' \
642   -e 's/char \* realloc/void \* realloc/g' \
643   -e 's/int     exit/void       exit/g' ${LIB}/$file > ${LIB}/${file}.sed
644   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
645   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
646     rm -f ${LIB}/$file
647   fi
648 fi
649
650 # Fix return type of free and {c,m,re}alloc in <malloc.h> on SunOS 4.1.
651 file=malloc.h
652 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
653   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
654   chmod +w ${LIB}/$file 2>/dev/null
655   chmod a+r ${LIB}/$file 2>/dev/null
656 fi
657
658 if [ -r ${LIB}/$file ]; then
659   echo Fixing $file
660   sed -e 's/typedef[    ]char \*        malloc_t/typedef void \*        malloc_t/g' \
661   -e 's/int[    ][      ]*free/void     free/g' \
662   ${LIB}/$file > ${LIB}/${file}.sed
663   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
664   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
665     rm -f ${LIB}/$file
666   fi
667 fi
668
669
670 # Fix bogus comment in <locale.h> on SunOS 4.1.
671 file=locale.h
672 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
673   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
674   chmod +w ${LIB}/$file 2>/dev/null
675   chmod a+r ${LIB}/$file 2>/dev/null
676 fi
677
678 if [ -r ${LIB}/$file ]; then
679   echo Fixing $file
680   sed -e 's%#endif / \*%#endif /\* %g' ${LIB}/$file > ${LIB}/${file}.sed
681   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
682   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
683     rm -f ${LIB}/$file
684   fi
685 fi
686
687 # Fix bogus #ifdef in <hsfs/hsfs_spec.h> on SunOS 4.1.
688 file=hsfs/hsfs_spec.h
689 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
690   mkdir ${LIB}/hsfs 2>/dev/null
691   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
692   chmod +w ${LIB}/$file 2>/dev/null
693   chmod a+r ${LIB}/$file 2>/dev/null
694 fi
695
696 if [ -r ${LIB}/$file ]; then
697   echo Fixing $file
698   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
699     ${LIB}/$file > ${LIB}/${file}.sed
700   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
701   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
702     rm -f ${LIB}/$file
703   fi
704 fi
705
706 # Fix bogus #ifdef in <hsfs/hsnode.h> on SunOS 4.1.
707 file=hsfs/hsnode.h
708 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
709   mkdir ${LIB}/hsfs 2>/dev/null
710   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
711   chmod +w ${LIB}/$file 2>/dev/null
712   chmod a+r ${LIB}/$file 2>/dev/null
713 fi
714
715 if [ -r ${LIB}/$file ]; then
716   echo Fixing $file
717   sed -e 's/\#ifdef __i386__ || __sun4c__/\#if __i386__ || __sun4c__/g' \
718     ${LIB}/$file > ${LIB}/${file}.sed
719   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
720   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
721     rm -f ${LIB}/$file
722   fi
723 fi
724
725 # Fix bogus #ifdef in <hsfs/iso_spec.h> on SunOS 4.1.
726 file=hsfs/iso_spec.h
727 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
728   mkdir ${LIB}/hsfs 2>/dev/null
729   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
730   chmod +w ${LIB}/$file 2>/dev/null
731   chmod a+r ${LIB}/$file 2>/dev/null
732 fi
733
734 if [ -r ${LIB}/$file ]; then
735   echo Fixing $file
736   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
737     ${LIB}/$file > ${LIB}/${file}.sed
738   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
739   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
740     rm -f ${LIB}/$file
741   fi
742 fi
743
744 # Incorrect #include in Sony News-OS 3.2.
745 file=machine/machparam.h
746 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
747   mkdir ${LIB}/machine 2>/dev/null
748   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
749   chmod +w ${LIB}/$file 2>/dev/null
750   chmod a+r ${LIB}/$file 2>/dev/null
751 fi
752
753 if [ -r ${LIB}/$file ]; then
754   echo Fixing $file, incorrect \#include
755   sed -e 's@"../machine/endian.h"@<machine/endian.h>@' \
756     ${LIB}/$file > ${LIB}/${file}.sed
757   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
758   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
759     rm -f ${LIB}/$file
760   fi
761 fi
762
763 # Multiline comment after typedef on IRIX 4.0.1.
764 file=sys/types.h
765 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
766   mkdir ${LIB}/sys 2>/dev/null
767   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
768   chmod +w ${LIB}/$file 2>/dev/null
769   chmod a+r ${LIB}/$file 2>/dev/null
770 fi
771
772 if [ -r ${LIB}/$file ]; then
773   echo Fixing $file, comment in the middle of \#ifdef
774   sed -e 's@type of the result@type of the result */@' \
775     -e 's@of the sizeof@/* of the sizeof@' \
776     ${LIB}/$file > ${LIB}/${file}.sed
777   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
778   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
779     rm -f ${LIB}/$file
780   fi
781 fi
782
783 # Fix non-ANSI memcpy declaration that conflicts with gcc's builtin
784 # declaration on Sun OS 4.x.  We must only fix this on Sun OS 4.x, because
785 # many other systems have similar text but correct versions of the file.
786 # To ensure only Sun's is fixed, we grep for a likely unique string.
787 file=memory.h
788 if egrep '/\*   @\(#\)memory\.h 1\.[2-4] 8./../.. SMI; from S5R2 1\.2   \*/' $file > /dev/null; then
789   if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
790     cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
791     chmod +w ${LIB}/$file 2>/dev/null
792     chmod a+r ${LIB}/$file 2>/dev/null
793   fi
794   if [ -r ${LIB}/$file ]; then
795     echo Replacing $file
796     cat > ${LIB}/$file << EOF
797 /* This file was generated by fixincludes */
798 #ifndef __memory_h__
799 #define __memory_h__
800
801 #ifdef __STDC__
802 extern void *memccpy();
803 extern void *memchr();
804 extern void *memcpy();
805 extern void *memset();
806 #else
807 extern char *memccpy();
808 extern char *memchr();
809 extern char *memcpy();
810 extern char *memset();
811 #endif /* __STDC__ */
812
813 extern int memcmp();
814
815 #endif __memory_h__
816 EOF
817   fi
818 fi
819
820 # parameters not const on DECstation Ultrix V4.0.
821 file=stdio.h
822 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
823   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
824   chmod +w ${LIB}/$file 2>/dev/null
825   chmod a+r ${LIB}/$file 2>/dev/null
826 fi
827
828 if [ -r ${LIB}/$file ]; then
829   echo Fixing $file, non-const arg
830   sed -e 's@perror( char \*__s );@perror( const char *__s );@' \
831       -e 's@fputs( char \*__s,@fputs( const char *__s,@' \
832       -e 's@fopen( char \*__filename, char \*__type );@fopen( const char *__filename, const char *__type );@' \
833       -e 's@fwrite( void \*__ptr,@fwrite( const void *__ptr,@' \
834       -e 's@fscanf( FILE \*__stream, char \*__format,@fscanf( FILE *__stream, const char *__format,@' \
835       -e 's@scanf( char \*__format,@scanf( const char *__format,@' \
836       -e 's@sscanf( char \*__s, char \*__format,@sscanf( const char *__s, const char *__format,@' \
837     ${LIB}/$file > ${LIB}/${file}.sed
838   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
839   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
840     rm -f ${LIB}/$file
841   fi
842 fi
843
844 # parameters conflict with C++ new on rs/6000 
845 file=stdio.h
846 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
847   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
848   chmod +w ${LIB}/$file 2>/dev/null
849 fi
850
851 if [ -r ${LIB}/$file ]; then
852   echo Fixing $file, parameter name conflicts
853   sed -e 's@rename(const char \*old, const char \*new)@rename(const char *_old, const char *_new)@' \
854     ${LIB}/$file > ${LIB}/${file}.sed
855   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
856   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
857     rm -f ${LIB}/$file
858   fi
859 fi
860
861 # Don't use or define the name va_list in stdio.h.
862 # This is for ANSI and also to interoperate properly with gvarargs.h.
863 file=stdio.h
864 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
865   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
866   chmod +w ${LIB}/$file 2>/dev/null
867   chmod a+r ${LIB}/$file 2>/dev/null
868 fi
869
870 if [ -r ${LIB}/$file ]; then
871   echo Fixing $file, use of va_list
872   # Arrange for stdio.h to use stdarg.h to define __gnuc_va_list
873   (echo "#define __need___va_list"
874    echo "#include <stdarg.h>") > ${LIB}/${file}.sed
875   # Use __gnuc_va_list in arg types in place of va_list.
876   # On 386BSD use __gnuc_va_list instead of _VA_LIST_. We're hoping the
877   # trailing parentheses and semicolon save all other systems from this.
878   # Define __va_list__ (something harmless and unused) instead of va_list.
879   # Don't claim to have defined va_list.
880   sed -e 's@ va_list @ __gnuc_va_list @' \
881       -e 's@ _VA_LIST_));@ __gnuc_va_list));@' \
882       -e 's@ va_list@ __va_list__@' \
883       -e 's@\*va_list@*__va_list__@' \
884       -e 's@VA_LIST@DUMMY_VA_LIST@' \
885     ${LIB}/$file >> ${LIB}/${file}.sed
886   
887   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
888   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
889     rm -f ${LIB}/$file
890   fi
891 fi
892
893 # Cancel out ansi_compat.h on Ultrix.  Replace it with empty file.
894 file=ansi_compat.h
895 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
896   if grep -s ULTRIX $file; then
897     echo "/* This file intentionally left blank.  */" > $LIB/$file
898   fi
899 fi
900
901 # parameter to atof not const on DECstation Ultrix V4.0.
902 # also get rid of bogus inline definitions in HP-UX 8.0
903 file=math.h
904 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
905   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
906   chmod +w ${LIB}/$file 2>/dev/null
907   chmod a+r ${LIB}/$file 2>/dev/null
908 fi
909
910 if [ -r ${LIB}/$file ]; then
911   echo Fixing $file, non-const arg
912   sed -e 's@atof( char \*__nptr );@atof( const char *__nptr );@' \
913       -e 's@inline int abs(int d) { return (d>0)?d:-d; }@@' \
914       -e 's@inline double abs(double d) { return fabs(d); }@@' \
915     ${LIB}/$file > ${LIB}/${file}.sed
916   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
917   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
918     rm -f ${LIB}/$file
919   fi
920 fi
921
922 # In limits.h, put #ifndefs around things that are supposed to be defined
923 # in float.h to avoid redefinition errors if float.h is included first.
924 file=limits.h
925 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
926   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
927   chmod +w ${LIB}/$file 2>/dev/null
928   chmod a+r ${LIB}/$file 2>/dev/null
929 fi
930
931 if [ -r ${LIB}/$file ]; then
932   echo Fixing $file
933   sed -e '/[    ]FLT_MIN[       ]/i\
934 #ifndef FLT_MIN'\
935       -e '/[    ]FLT_MIN[       ]/a\
936 #endif'\
937       -e '/[    ]FLT_MAX[       ]/i\
938 #ifndef FLT_MAX'\
939       -e '/[    ]FLT_MAX[       ]/a\
940 #endif'\
941       -e '/[    ]FLT_DIG[       ]/i\
942 #ifndef FLT_DIG'\
943       -e '/[    ]FLT_DIG[       ]/a\
944 #endif'\
945       -e '/[    ]DBL_MIN[       ]/i\
946 #ifndef DBL_MIN'\
947       -e '/[    ]DBL_MIN[       ]/a\
948 #endif'\
949       -e '/[    ]DBL_MAX[       ]/i\
950 #ifndef DBL_MAX'\
951       -e '/[    ]DBL_MAX[       ]/a\
952 #endif'\
953       -e '/[    ]DBL_DIG[       ]/i\
954 #ifndef DBL_DIG'\
955       -e '/[    ]DBL_DIG[       ]/a\
956 #endif'\
957     ${LIB}/$file > ${LIB}/${file}.sed
958   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
959   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
960     echo Deleting ${LIB}/$file\; no fixes were needed.
961     rm -f ${LIB}/$file
962   fi
963 fi
964
965 # These two files on SunOS 4 are included by other files
966 # in the same directory, using "...".  So we must make sure they exist
967 # in the same directory as the other fixed files.
968 if [ -r ${INPUT}/multimedia/audio_errno.h ]
969 then
970   ln -s ${INPUT}/multimedia/audio_errno.h ${LIB}/multimedia 2>/dev/null
971 fi
972 if [ -r ${INPUT}/multimedia/audio_hdr.h ]
973 then
974   ln -s ${INPUT}/multimedia/audio_hdr.h ${LIB}/multimedia 2>/dev/null
975 fi
976
977 echo 'Removing unneeded directories:'
978 cd $LIB
979 files=`find . -type d -print | sort -r`
980 for file in $files; do
981   rmdir $LIB/$file > /dev/null 2>&1
982 done
983
984 if $LINKS; then
985   echo 'Making internal symbolic non-directory links'
986   cd ${INPUT}
987   files=`find . -type l -print`
988   for file in $files; do
989     dest=`ls -ld $file | sed -n 's/.*-> //p'`
990     if expr "$dest" : '[^/].*' > /dev/null; then    
991       target=${LIB}/`echo file | sed "s|[^/]*\$|$dest|"`
992       if [ -f $target ]; then
993         ln -s $dest ${LIB}/$file >/dev/null 2>&1
994       fi
995     fi
996   done
997 fi
998
999 exit 0