1#!/bin/sh
2# Ensure that mkdir-p.c's fail-to-return-to-initial-working-directory
3# causes immediate failure.  Also, ensure that we don't create
4# subsequent, relative command-line arguments in the wrong place.
5
6# Copyright (C) 2005-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_ mkdir
23skip_if_root_
24
25mkdir no-access || framework_failure_
26mkdir no-acce2s || framework_failure_
27mkdir -p no-acce3s/d || framework_failure_
28
29p=$(pwd)
30(cd no-access && chmod 0 . && mkdir -p "$p/a/b" u/v) 2> /dev/null
31test $? -eq 1 || fail=1
32test -d "$p/a/b" || fail=1
33
34# Same as above, but with a following *absolute* name, it should succeed
35(cd no-acce2s && chmod 0 . && mkdir -p "$p/b/b" "$p/z") || fail=1
36test -d "$p/b/b" && test -d "$p/z" || fail=1
37
38# Same as above, but a trailing relative name in an unreadable directory
39# whose parent is inaccessible.  coreutils 5.97 fails this test.
40# Perform this test only if "." is on a local file system.
41# Otherwise, it would fail e.g., on an NFS-mounted file system.
42if is_local_dir_ .; then
43  (cd no-acce3s/d && chmod a-r . && chmod a-rx .. &&
44      mkdir -p a/b "$p/b/c" d/e && test -d a/b && test -d d/e) || fail=1
45  test -d "$p/b/c" || fail=1
46fi
47
48b=$(ls "$p/a" | tr -d '\n')
49# With coreutils-5.3.0, this would fail with $b=bu.
50test "x$b" = xb || fail=1
51
52Exit $fail
53