e58b73d40d6c743b51a2927af845c0f40f1c3469
[platform/upstream/coreutils.git] / tests / rwx-to-mode
1 #!/bin/sh
2 # Convert an ls-style permission string, like drwxr----x and -rw-r-x-wx
3 # to the equivalent chmod --mode (-m) argument, (=,u=rwx,g=r,o=x and
4 # =,u=rw,g=rx,o=wx).  Ignore ACLs.
5
6 case $# in
7   1) rwx=$1;;
8   *) echo "$0: wrong number of arguments" 1>&2
9     echo "Usage: $0 ls-style-mode-string" 1>&2
10     exit 1;;
11 esac
12
13 case $rwx in
14   [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]) ;;
15   [ld-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxsS-][rwx-][rwx-][rwxtT-]+) ;;
16   *) echo "$0: invalid mode string: $rwx" 1>&2; exit 1;;
17 esac
18
19 # Perform these conversions:
20 # S  s
21 # s  xs
22 # T  t
23 # t  xt
24 # The `T' and `t' ones are only valid for `other'.
25 s='s/S/@/;s/s/x@/;s/@/s/'
26 t='s/T/@/;s/t/x@/;s/@/t/'
27
28 u=`echo $rwx|sed 's/^.\(...\).*/,u=\1/;s/-//g;s/^,u=$//;'$s`
29 g=`echo $rwx|sed 's/^....\(...\).*/,g=\1/;s/-//g;s/^,g=$//;'$s`
30 o=`echo $rwx|sed 's/^.......\(...\).*/,o=\1/;s/-//g;s/^,o=$//;'$s';'$t`
31 echo "=$u$g$o"
32 exit 0