fix possible crash on user deletion
[srvx.git] / rx / compile
1 #! /bin/sh
2
3 # Wrapper for compilers which do not understand `-c -o'.
4
5 # Copyright 1999, 2000 Free Software Foundation, Inc.
6 # Written by Tom Tromey <tromey@cygnus.com>.
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2, or (at your option)
11 # any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
22 # Usage:
23 # compile PROGRAM [ARGS]...
24 # `-o FOO.o' is removed from the args passed to the actual compile.
25
26 prog=$1
27 shift
28
29 ofile=
30 cfile=
31 args=
32 while test $# -gt 0; do
33    case "$1" in
34     -o)
35        ofile=$2
36        shift
37        ;;
38     *.c)
39        cfile=$1
40        args="$args $1"
41        ;;
42     *)
43        args="$args $1"
44        ;;
45    esac
46    shift
47 done
48
49 test -z "$ofile" && {
50    echo "compile: no \`-o' option seen" 1>&2
51    exit 1
52 }
53
54 test -z "$cfile" && {
55    echo "compile: no \`.c' file seen" 1>&2
56    exit 1
57 }
58
59 # Name of file we expect compiler to create.
60 cofile=`echo $cfile | sed -e 's|^.*/||' -e 's/\.c$/.o/'`
61
62 # Create the lock directory.
63 lockdir=`echo $ofile | sed -e 's|/|_|g'`
64 while true; do
65    if mkdir $lockdir > /dev/null 2>&1; then
66       break
67    fi
68    sleep 1
69 done
70 # FIXME: race condition here if user kills between mkdir and trap.
71 trap "rmdir $lockdir; exit 1" 1 2 15
72
73 # Run the compile.
74 "$prog" $args
75 status=$?
76
77 if test -f "$cofile"; then
78    mv "$cofile" "$ofile"
79 fi
80
81 rmdir $lockdir
82 exit $status