#!/bin/sh
dirname=`echo "$0" | sed 's,^[^/]*$,.,;s,//*[^/]*$,,'`
progname=`echo "$0" | sed 's,.*/,,'`
original_dir=`${PWDCMD-pwd}`
FIX_HEADER=${FIX_HEADER-$original_dir/fix-header}
DEFINES="-D__STDC__=0 -D__cplusplus ${FIXPROTO_DEFINES}"
if mkdir -p . 2> /dev/null; then
mkinstalldirs="mkdir -p"
else
mkinstalldirs=${mkinstalldirs-"/bin/sh $dirname/mkinstalldirs"}
if $mkinstalldirs . 2> /dev/null; then
:
else
mkinstalldirs=mkdir
fi
fi
if [ `echo $1 | wc -w` = 0 ] ; then
echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
exit 1
fi
std_files="ctype.h dirent.h errno.h curses.h fcntl.h grp.h locale.h math.h pwd.h setjmp.h signal.h stdio.h stdlib.h string.h sys/socket.h sys/stat.h sys/times.h sys/resource.h sys/utsname.h sys/wait.h tar.h termios.h time.h unistd.h utime.h"
rel_target_dir=$1
src_dir_all=$2
src_dir_std=$3
case $rel_target_dir in
/* | [A-Za-z]:[\\/]*)
abs_target_dir=$rel_target_dir
;;
*)
abs_target_dir=$original_dir/$rel_target_dir
;;
esac
if ln -s X $rel_target_dir/ShouldNotExist 2>/dev/null; then
rm -f $rel_target_dir/ShouldNotExist
LINKS=true
elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
rm -f /tmp/ShouldNotExist
LINKS=true
else
LINKS=false
fi
if [ \! -d $abs_target_dir ] ; then
echo $progname\: creating directory $rel_target_dir
$mkinstalldirs $abs_target_dir
fi
echo $progname\: populating \`$rel_target_dir\'
include_path=""
if [ `echo $* | wc -w` != 0 ] ; then
for rel_source_dir in $src_dir_all $src_dir_std; do
case $rel_source_dir in
/* | [A-Za-z]:[\\/]*)
abs_source_dir=$rel_source_dir
;;
*)
abs_source_dir=$original_dir/$rel_source_dir
;;
esac
include_path="$include_path -I$abs_source_dir"
done
fi
done_dirs=""
subdirs_made=""
echo "" >fixproto.list
for code in ALL STD ; do
subdirs="."
case $code in
ALL)
rel_source_dir=$src_dir_all
dirs="."
levels=2
while $LINKS && test -n "$dirs" -a $levels -gt 0
do
levels=`expr $levels - 1`
newdirs=
for d in $dirs ; do
subdirs="$subdirs "`cd $rel_source_dir/$d; find . -type d -print | \
sed -e '/^\.$/d' -e "s|^\./|${d}/|" -e 's|^\./||' \
-e '/CC$/d' -e '/[+][+]/d'`
links=
links=`cd $rel_source_dir; find $d/. -type l -print | \
sed -e "s|$d/./|$d/|" -e 's|^\./||'`
for link in $links --dummy-- ; do
test -d $rel_source_dir/$link/. && newdirs="$newdirs $link"
done
done
dirs="$newdirs"
subdirs="$subdirs $newdirs"
done
;;
STD)
rel_source_dir=$src_dir_std
;;
esac
case $rel_source_dir in
/* | [A-Za-z]:[\\/]*)
abs_source_dir=$rel_source_dir
;;
*)
abs_source_dir=$original_dir/$rel_source_dir
;;
esac
if [ \! -d $abs_source_dir ] ; then
echo $progname\: warning\: no such directory\: \`$rel_source_dir\'
continue
fi
for rel_source_subdir in $subdirs; do
abs_target_subdir=${abs_target_dir}/${rel_source_subdir}
if [ \! -d $abs_target_subdir ] ; then
if $mkinstalldirs $abs_target_subdir ; then
subdirs_made="$abs_target_subdir $subdirs_made"
fi
fi
rel_source_prefix=`echo $rel_source_subdir | sed -e 's|$|/|' -e 's|^./||'`
case $code in
ALL)
rel_source_files=`cd ${abs_source_dir}/${rel_source_subdir}; echo *.h | sed -e 's|[*].h|NONE|'`
;;
STD)
files_to_check="$std_files"
rel_source_files=""
while [ -n "${files_to_check}" ]
do
new_files_to_check=""
for file in $files_to_check ; do
xxfile=`echo $file | sed -e 's|/\([^/\.][^/\.]*\)/\.\./|/|'`
xxdir=`echo ./$file | sed -e 's|/[^/]*$||'`
if [ \! -d $abs_target_subdir/$xxdir ] ; then
if $mkinstalldirs $abs_target_subdir/$xxdir ; then
subdirs_made="$abs_target_subdir/$xxdir $subdirs_made"
fi
fi
if [ -f $src_dir_std/$file -a -f $src_dir_std/$xxfile ] ; then
file=$xxfile
fi
case " $rel_source_files " in
*" ${file} "*)
;;
*)
if test -f $src_dir_std/$file ; then
rel_dir=`echo $file | sed -n -e 's|^\(.*/\)[^/]*$|\1|p'`
new_files_to_check="$new_files_to_check "`sed -n \
-e 's@ @ @g' \
-e 's@^ * 's@^ * <$src_dir_std/$file`
rel_source_files="$rel_source_files $file"
fi
;;
esac
done
files_to_check="$new_files_to_check"
done
rel_source_files="$rel_source_files"
;;
esac
for filename in $rel_source_files ; do
rel_source_file=${rel_source_prefix}${filename}
abs_source_file=$abs_source_dir/$rel_source_file
abs_target_file=$abs_target_dir/$rel_source_file
if test "$filename" = 'NONE' ; then
echo "(No *.h files in $abs_source_dir/$rel_source_subdir)"
elif test -f $abs_target_file -a -n "$done_dirs" \
&& grep "$rel_source_file" fixproto.list >/dev/null
then true
else
$FIX_HEADER $rel_source_file $abs_source_file $abs_target_file ${DEFINES} $include_path
if test $? != 0 ; then exit 1 ; fi
echo "${rel_source_file}" >>fixproto.list
fi
done
done
done_dirs="$done_dir $rel_source_dir"
done
for rel_source_file in unistd.h stdlib.h string.h time.h ; do
if grep "$rel_source_file" fixproto.list >/dev/null ; then
: else
echo Adding missing $rel_source_file
rel_source_ident=`echo $rel_source_file | tr ./ __`
cat >tmp.h <<EOF
/* Fake ${rel_source_file}, created by GCC.
The functions declared in this file do not necessarily exist in
your C library. */
EOF
case ${rel_source_file} in
time.h)
if test -f $src_dir_std/sys/time.h \
|| grep "sys/time.h" fixproto.list >/dev/null ; then
cat >>tmp.h <<EOF
EOF
else
cat >>tmp.h <<EOF
typedef long time_t;
typedef long clock_t;
struct tm;
EOF
fi ;;
esac
cat >>tmp.h <<EOF
EOF
${FIX_HEADER} $rel_source_file tmp.h $abs_target_dir/$rel_source_file ${DEFINES} $include_path
if test $? != 0 ; then exit 1 ; fi
if test -f $abs_target_dir/$rel_source_file ; then
rm tmp.h
else
mv tmp.h $abs_target_dir/$rel_source_file
fi
fi
done
rmdir $subdirs_made 2>/dev/null
exit 0