1#!/bin/sh
2# Ensure that touch -h works.
3
4# Copyright (C) 2009-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ touch test
21
22ln -s nowhere dangling || framework_failure_
23touch file || framework_failure_
24ln -s file link || framework_failure_
25
26
27# These first tests should work on every platform.
28# -h does not create files, but it warns.  Use -c to silence warning.
29returns_ 1 touch -h no-file 2> err || fail=1
30compare /dev/null err && fail=1
31touch -h -c no-file 2> err || fail=1
32compare /dev/null err || fail=1
33
34# -h works on regular files
35touch -h file || fail=1
36
37# -h coupled with -r uses timestamp of the symlink, not the referent.
38touch -h -r dangling file || fail=1
39test -f nowhere && fail=1
40
41# The remaining tests of -h require kernel support for changing symlink times.
42grep '^#define HAVE_UTIMENSAT 1' "$CONFIG_HEADER" > /dev/null ||
43grep '^#define HAVE_LUTIMES 1' "$CONFIG_HEADER" > /dev/null ||
44  skip_ 'this system lacks the utimensat function'
45
46# Changing time of dangling symlink is okay.
47# Skip the test if this fails, but the error text corresponds to
48# ENOSYS (possible with old kernel but new glibc).
49touch -h dangling 2> err
50case $? in
51  0) test -f nowhere && fail=1
52     compare /dev/null err || fail=1;;
53  1) grep 'Function not implemented' err \
54       && skip_ 'this system lacks the utimensat function'
55     fail=1;;
56  *) fail=1;;
57esac
58
59# Change the mtime of a symlink.
60touch -m -h -d 2009-10-10 link || fail=1
61case $(stat --format=%y link) in
62  2009-10-10*) ;;
63  *) fail=1 ;;
64esac
65case $(stat --format=%y file) in
66  2009-10-10*) fail=1;;
67esac
68
69# Test interactions with -.
70touch -h - > file || fail=1
71
72# If >&- works, test "touch -ch -" etc.
73# >&- apparently does not work in HP-UX 11.23.
74# This test is ineffective unless /dev/stdout also works.
75# If stdout is open, it is not a symlink.
76if env test -w /dev/stdout >/dev/null &&
77   env test ! -w /dev/stdout >&-; then
78  returns_ 1 touch -h - >&- || fail=1
79  touch -h -c - >&- || fail=1
80fi
81
82Exit $fail
83