3 # Copyright (c) 2004 Matthias S. Benkmann <article AT winterdrache DOT de>
4 # You may do everything with this code except misrepresent its origin.
5 # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
7 # Copyright (C) 2014 Steve Youngs <steve@steveyoungs.com>
8 # Handle all possible file modes as well as chmod options and symbolic
13 for p in $(type -ap chmod) ; do
14 if [ ! $p -ef $0 ]; then
20 if [ ! -n "$DAISY_CHAIN" ]; then
21 echo 1>&2 '***' Cannot find real ${0##*/} command
25 if [ $(id -u) -eq 0 ]; then
26 echo 1>&2 '***' $(dirname $0) should not be in root\'s \$PATH
27 echo 1>&2 '***' call '"'$DAISY_CHAIN $@'"' directly.
31 # Save the original cmdline as we're gonna mess with it
34 # Remove any options so $1 becomes the perm arg, however save the
39 (-[cfvR]|--[chnpqrsv]*) opts="$opts $1" ; shift ;;
44 # $1 should now be the perm arg
47 # Octal or symbolic? If the latter, convert to the former.
48 printf '%d' "$perm" &>/dev/null
50 testfile=$(mktemp hack-o-matic-chmod.XXXXX --tmpdir)
51 exec $DAISY_CHAIN ${perm} ${testfile}
52 perm=$(stat --printf "%a" ${testfile})
56 # if it is 4 digits, they're trying to do funky shit
57 if [ ${perm} -gt 999 ]; then
58 # Chop off the 1st digit (the set{uid,gid,sticky} bit)
60 echo 1>&2 '***' chmod ${cmdline}
63 # kill off $1 and replace it with our maybe sanitised $perm
64 shift 1; set -- $perm "$@"
66 exec $DAISY_CHAIN ${opts} "$@" || exit $?