#!/bin/ksh -p
# Shell script to return current permissions of a file
# in numerical "chmod" form.

if (($# == 1)) ; then

  (ls -ladL $1 2>&1) > /dev/null ; st=$?
  if (($st > 0)) ; then

    echo $(basename $0): $1 not found >&2
    exit $st

  fi

  set -A w $( ls -ladL $1 | awk '{print $1}' | sed 's/./& /g'|\
  tr "[a-z]-" "111111111111111111111111110" )

  if (($? != 0)) ; then exit 1 ; fi

  a=$(( ${w[1]} * 4 + ${w[2]} * 2 + ${w[3]} ))
  b=$(( ${w[4]} * 4 + ${w[5]} * 2 + ${w[6]} ))
  c=$(( ${w[7]} * 4 + ${w[8]} * 2 + ${w[9]} ))

  echo "$a$b$c"

elif (($# == 2)) ; then

  \chmod $1 $2
  exit $?

else

  exit 1

fi
