Fix a bug that was exposed by the SXEmacs configure script.
[pkgusr] / usr / lib / pkgusr / chmod
index e1aa49e..03d13ce 100755 (executable)
@@ -22,7 +22,7 @@ if [ ! -n "$DAISY_CHAIN" ]; then
     exit 1
 fi
 
-if [ $UID == 0 ]; then
+if [ $(id -u) -eq 0 ]; then
     echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
     echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
     exit 1
@@ -36,25 +36,25 @@ cmdline="$@"
 opts=""
 while [ -n "$1" ]; do
     case $1 in
-       (-*) opts="$opts $1" ; shift ;;
+       (-[cfvR]|--[chnpqrsv]*) opts="$opts $1" ; shift ;;
        (*) break ;;
     esac
 done
+
 # $1 should now be the perm arg
+perm=$1
 
-# Octal or symbolic?
-printf '%d' $1 &>/dev/null
-if [ $? -eq 0 ]; then
-    perm=$1
-else
-    touch /tmp/hack-o-matic-4500
-    $DAISY_CHAIN $1 /tmp/hack-o-matic-4500
-    perm=$(stat --printf "%a" /tmp/hack-o-matic-4500)
-    rm -f /tmp/hack-o-matic-4500
+# Octal or symbolic?  If the latter, convert to the former.
+printf '%d' "$perm" &>/dev/null
+if [ $? -ne 0 ]; then
+    testfile=$(mktemp hack-o-matic-chmod.XXXXX --tmpdir)
+    exec $DAISY_CHAIN ${perm} ${testfile}
+    perm=$(stat --printf "%a" ${testfile})
+    rm -f ${testfile}
 fi
 
 # if it is 4 digits, they're trying to do funky shit
-if [ $perm -gt 999 ]; then
+if [ ${perm} -gt 999 ]; then
     # Chop off the 1st digit (the set{uid,gid,sticky} bit)
     perm=${perm/?/}
     echo 1>&2 '***' chmod ${cmdline}
@@ -63,7 +63,7 @@ fi
 # kill off $1 and replace it with our maybe sanitised $perm
 shift 1; set -- $perm "$@"
 
-exec $DAISY_CHAIN ${opts} $@ || exit $?
+exec $DAISY_CHAIN ${opts} "$@" || exit $?
 exit 0
 
 # Local variables: