#! /bin/sh
if [ x$1 = x ]
then echo fixincludes: no output directory specified
exit 1
fi
LIB=${1?"fixincludes: output directory not specified"}
if [ ! -d $LIB ]; then
mkdir $LIB || exit 1
fi
ORIG_DIR=`${PWDCMD-pwd}`
case $LIB in
/*)
;;
*)
cd $LIB; LIB=`${PWDCMD-pwd}`
;;
esac
echo 'Building fixincludes in ' ${LIB}
if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
rm -f $LIB/ShouldNotExist
LINKS=true
else
LINKS=false
fi
echo 'Making directories:'
shift
if [ $ set /usr/include
fi
INLIST="$@"
for INPUT in ${INLIST} ; do
cd ${ORIG_DIR}
cd ${INPUT}
if $LINKS; then
files=`ls -LR | sed -n s/:$//p`
else
files=`find . -type d -print | sed '/^.$/d'`
fi
for file in $files; do
rm -rf $LIB/$file
if [ ! -d $LIB/$file ]
then mkdir $LIB/$file
fi
done
treetops="${INPUT} ${LIB}"
if $LINKS; then
echo 'Making internal symbolic directory links'
for file in $files; do
dest=`ls -ld $file | sed -n 's/.*-> //p'`
if [ "$dest" ]; then
cwd=`pwd`
cd ${INPUT}
cd `echo ./$file | sed -n 's&[^/]*$&&p'`
(cd $dest) > /dev/null 2>&1
if [ $? = 0 ]; then
cd $dest
x=`pwd`
if expr $x : "${INPUT}/.*" > /dev/null; then
y=`echo $x | sed -n "s&${INPUT}/&&p"`
echo $file '->' $y ': Making link'
rm -fr ${LIB}/$file > /dev/null 2>&1
ln -s ${LIB}/$y ${LIB}/$file > /dev/null 2>&1
else
treetops="$treetops $x ${LIB}/$file"
fi
fi
cd $cwd
fi
done
fi
set - $treetops
while [ $ echo "Finding header files in $1:"
cd ${INPUT}
cd $1
files=`find . -name '*.h' -type f -print`
echo 'Checking header files:'
for file in $files; do
if egrep '!__STDC__' $file >/dev/null; then
if [ -r $file ]; then
cp $file $2/$file >/dev/null 2>&1 || echo "Can't copy $file"
chmod +w $2/$file
chmod a+r $2/$file
sed -e '
s/!__STDC__/!defined (__STRICT_ANSI__)/g
' $2/$file > $2/$file.sed
mv $2/$file.sed $2/$file
if cmp $file $2/$file >/dev/null 2>&1; then
rm $2/$file
else
echo Fixed $file
fi
fi
fi
done
shift; shift
done
cd ${INPUT}
file=stdlib.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
file_to_fix=${LIB}/$file
else
if [ -r ${INPUT}/$file ]; then
file_to_fix=${INPUT}/$file
else
file_to_fix=""
fi
fi
if [ \! -z "$file_to_fix" ]; then
echo Checking $file_to_fix
sed -e 's/getcwd(char \{0,\}\*, int)/getcwd(char *, size_t)/' $file_to_fix > /tmp/$base
if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
true
else
echo Fixed $file_to_fix
rm -f ${LIB}/$file
cp /tmp/$base ${LIB}/$file
chmod a+r ${LIB}/$file
fi
rm -f /tmp/$base
fi
file=unistd.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
file_to_fix=${LIB}/$file
else
if [ -r ${INPUT}/$file ]; then
file_to_fix=${INPUT}/$file
else
file_to_fix=""
fi
fi
if [ \! -z "$file_to_fix" ]; then
echo Checking $file_to_fix
sed -e 's/getcwd(char \*, int)/getcwd(char *, size_t)/' $file_to_fix \
| sed -e 's/profil(unsigned short \*, unsigned int, unsigned int, unsigned int)/profil(unsigned short *, size_t, int, unsigned)/' > /tmp/$base
if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
true
else
echo Fixed $file_to_fix
rm -f ${LIB}/$file
cp /tmp/$base ${LIB}/$file
chmod a+r ${LIB}/$file
fi
rm -f /tmp/$base
fi
file=prototypes.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
file_to_fix=${LIB}/$file
else
if [ -r ${INPUT}/$file ]; then
file_to_fix=${INPUT}/$file
else
file_to_fix=""
fi
fi
if [ \! -z "$file_to_fix" ]; then
echo Checking $file_to_fix
sed -e 's/getcwd(char \*, int)/getcwd(char *, size_t)/' $file_to_fix \
| sed -e 's/const int link(const char \*, char \*)/extern int link(const char *, const char *)/' > /tmp/$base
if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
true
else
echo Fixed $file_to_fix
rm -f ${LIB}/$file
cp /tmp/$base ${LIB}/$file
chmod a+r ${LIB}/$file
fi
rm -f /tmp/$base
fi
file=tinfo.h
if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
mkdir ${LIB}/rpcsvc 2>/dev/null
cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
chmod +w ${LIB}/$file 2>/dev/null
chmod a+r ${LIB}/$file 2>/dev/null
fi
if [ -r ${LIB}/$file ]; then
echo Fixing $file, __cplusplus macro
sed -e 's/[ ]_cplusplus/ __cplusplus/' ${LIB}/$file > ${LIB}/${file}.sed
rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
if cmp $file ${LIB}/$file >/dev/null 2>&1; then
rm ${LIB}/$file
fi
fi
file=sys/times.h
if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
chmod +w ${LIB}/$file 2>/dev/null
chmod a+r ${LIB}/$file 2>/dev/null
fi
if [ -r ${LIB}/$file ]; then
echo Fixing $file, utime prototype
sed -e 's/(const char \*, struct utimbuf \*);/(const char *, const struct utimbuf *);/' ${LIB}/$file > ${LIB}/${file}.sed
rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
if cmp $file ${LIB}/$file >/dev/null 2>&1; then
rm ${LIB}/$file
fi
fi
file=math.h
base=`basename $file`
if [ -r ${LIB}/$file ]; then
file_to_fix=${LIB}/$file
else
if [ -r ${INPUT}/$file ]; then
file_to_fix=${INPUT}/$file
else
file_to_fix=""
fi
fi
if [ \! -z "$file_to_fix" ]; then
echo Checking $file_to_fix
sed -e '/struct exception/i\
-e '/struct exception/a\
-e 's@inline int abs(int [a-z][a-z]*) {.*}@extern "C" int abs(int);@' \
$file_to_fix > /tmp/$base
if cmp $file_to_fix /tmp/$base >/dev/null 2>&1; then \
true
else
echo Fixed $file_to_fix
rm -f ${LIB}/$file
cp /tmp/$base ${LIB}/$file
chmod a+r ${LIB}/$file
fi
rm -f /tmp/$base
fi
file=sys/stat.h
if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
chmod +w ${LIB}/$file 2>/dev/null
chmod a+r ${LIB}/$file 2>/dev/null
fi
if [ -r ${LIB}/$file ]; then
echo Fixing $file, static definitions not C++-aware.
sed -e '/^static int[ ]*/i\
extern "C"\
{\
' \
-e '/^}$/a\
}\
' ${LIB}/$file > ${LIB}/${file}.sed
rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
if cmp $file ${LIB}/$file >/dev/null 2>&1; then
rm -f ${LIB}/$file
fi
fi
for file in term.h tinfo.h
do
if [ -r $INPUT/$file ]; then
echo Checking $INPUT/$file
w='[ ]'
if grep "typedef$w.*char$w.*bool$w*;" $INPUT/$file >/dev/null
then
echo Fixed $file
rm -f $LIB/$file
cat <<__EOF__ >$LIB/$file
__EOF__
chmod a+r $LIB/$file
fi
fi
done
echo 'Removing unneeded directories:'
cd $LIB
files=`find . -type d -print | sort -r`
for file in $files; do
rmdir $LIB/$file > /dev/null 2>&1
done
if $LINKS; then
echo 'Making internal symbolic non-directory links'
cd ${INPUT}
files=`find . -type l -print`
for file in $files; do
dest=`ls -ld $file | sed -n 's/.*-> //p'`
if expr "$dest" : '[^/].*' > /dev/null; then
target=${LIB}/`echo file | sed "s|[^/]*\$|$dest|"`
if [ -f $target ]; then
ln -s $dest ${LIB}/$file >/dev/null 2>&1
fi
fi
done
fi
done
if [ x${INSTALL_ASSERT_H} != x ]
then
cd ${ORIG_DIR}
rm -f include/assert.h
cp ${srcdir}/assert.h include/assert.h || exit 1
chmod a+r include/assert.h
fi
exit 0