#!/bin/bash # Original... # Copyright (c) 2004 Matthias S. Benkmann
# You may do everything with this code except misrepresent its origin. # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND! # Copyright (C) 2014 Steve Youngs # Handle all possible file modes as well as chmod options and symbolic # modes. DAISY_CHAIN="" for p in $(type -ap chmod) ; do if [ ! $p -ef $0 ]; then DAISY_CHAIN=$p break fi done if [ ! -n "$DAISY_CHAIN" ]; then echo 1>&2 '***' Cannot find real ${0##*/} command exit 1 fi 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 fi # Save the original cmdline as we're gonna mess with it cmdline="$@" # Remove any options so $1 becomes the perm arg, however save the # options for later opts="" while [ -n "$1" ]; do case $1 in (-[cfvR]|--[chnpqrsv]*) opts="$opts $1" ; shift ;; (*) break ;; esac done # $1 should now be the perm arg perm=$1 # Octal or symbolic? Nuke the nasty bits (setuid etc) printf '%o' "0${perm}" &>/dev/null if [ $? -ne 0 ]; then perm=${perm//[st]/} else if [ ${perm} -gt 777 ]; then perm=${perm/?/} fi fi # If we changed the perm, report it and fix $@ if [ "${perm}" != "$1" ]; then echo 1>&2 '***' chmod ${cmdline} shift set -- $perm "$@" fi # Finally, run the chmod $DAISY_CHAIN ${opts} "$@" || exit $? exit 0 # Local variables: # sh-basic-offset: 4 # End: