1#!/bin/sh
2# Show that --color need not use stat, as long as we have d_type support.
3
4# Copyright (C) 2011-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_ ls
21require_strace_ stat
22require_dirent_d_type_
23
24stats='stat'
25# List of other _file name_ stat functions to increase coverage.
26other_stats='statx lstat stat64 lstat64 newfstatat fstatat64'
27for stat in $other_stats; do
28  strace -qe "$stat" true > /dev/null 2>&1 &&
29    stats="$stats,$stat"
30done
31
32# Disable enough features via LS_COLORS so that ls --color
33# can do its job without calling stat (other than the obligatory
34# one-call-per-command-line argument).
35cat <<EOF > color-without-stat || framework_failure_
36RESET 0
37DIR 01;34
38LINK 01;36
39FIFO 40;33
40SOCK 01;35
41DOOR 01;35
42BLK 40;33;01
43CHR 40;33;01
44ORPHAN 00
45SETUID 00
46SETGID 00
47CAPABILITY 00
48STICKY_OTHER_WRITABLE 00
49OTHER_WRITABLE 00
50STICKY 00
51EXEC 00
52MULTIHARDLINK 00
53EOF
54eval $(dircolors -b color-without-stat)
55
56# The system may perform additional stat-like calls before main.
57# Furthermore, underlying library functions may also implicitly
58# add an extra stat call, e.g. opendir since glibc-2.21-360-g46f894d.
59# Finally, ls(1) makes a stat call for stdout, but only in the case
60# when there is something to output.
61# To get the comparison right, first get a baseline count for running
62# 'ls -a' with one empty directory argument.  Then, compare that with
63# the invocation under test.
64mkdir d || framework_failure_
65
66count_stats() { grep -vE '\+\+\+|ENOSYS|NOTSUP' "$1" | wc -l; }
67strace -q -o log1 -e $stats ls -a --color=always d || fail=1
68n_stat1=$(count_stats log1) || framework_failure_
69
70test $n_stat1 = 0 \
71  && skip_ 'No stat calls recognized on this platform'
72
73# Populate the test directory.
74mkdir d/subdir \
75  && touch d/regf \
76  && ln d/regf d/hlink \
77  && ln -s regf d/slink \
78  && ln -s nowhere d/dangle \
79  || framework_failure_
80
81# Invocation under test.
82strace -q -o log2 -e $stats ls --color=always d || fail=1
83n_stat2=$(count_stats log2) || framework_failure_
84
85# Expect the same number of stat calls.
86test $n_stat1 = $n_stat2 \
87  || { fail=1; head -n30 log*; }
88
89Exit $fail
90