1#!/bin/sh
2# Make sure GNU chmod works the same way as those of Solaris, HPUX, AIX
3# on directories with the setgid bit set.  Also, check that the GNU octal
4# notations work.
5
6# Copyright (C) 2001-2023 Free Software Foundation, Inc.
7
8# This program is free software: you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation, either version 3 of the License, or
11# (at your option) any later version.
12
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
21. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
22print_ver_ chmod
23
24umask 0
25mkdir -m 755 d || framework_failure_
26
27# "chmod g+s d" does nothing on some NFS file systems.
28chmod g+s d 2> /dev/null && env -- test -g d ||
29  {
30    # This is required because on some systems (at least NetBSD 1.4.2A),
31    # it may happen that when you create a directory, its group isn't one
32    # to which you belong.  When that happens, the above chmod fails.  So
33    # here, upon failure, we try to set the group, then rerun the chmod command.
34
35    for id_g in $(id -g) $(id -G) ''; do
36      test -n "$id_g" || break
37      chgrp "$id_g" d && chmod g+s d && env -- test -g d && break
38    done
39    test -n "$id_g"
40  } ||
41  skip_ 'cannot create setgid directories'
42
43for mode in \
44  + - g-s 00755 000755 =755 -2000 -7022 755 0755 \
45  +2000 -5022 =7777,-5022
46do
47  chmod $mode d || fail=1
48
49  case $mode in
50    g-s | 00*755 | =755 | -2000 | -7022)
51       expected_mode=drwxr-xr-x ;;
52    *) expected_mode=drwxr-sr-x ;;
53  esac
54  ls_output=$(ls -ld d)
55  case $ls_output in
56    $expected_mode*) ;;
57    *) fail=1 ;;
58  esac
59
60  chmod =2755 d || fail=1
61done
62
63Exit $fail
64