2 # Show that --color need not use stat, as long as we have d_type support.
4 # Copyright (C) 2011-2023 Free Software Foundation, Inc.
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.
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.
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/>.
19 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
22 require_dirent_d_type_
25 # List of other _file name_ stat functions to increase coverage.
26 other_stats
='statx lstat stat64 lstat64 newfstatat fstatat64'
27 for stat
in $other_stats; do
28 strace
-qe "$stat" true
> /dev
/null
2>&1 &&
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).
35 cat <<EOF > color-without-stat || framework_failure_
48 STICKY_OTHER_WRITABLE 00
54 eval $
(dircolors
-b color-without-stat
)
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.
64 mkdir d || framework_failure_
66 count_stats
() { grep -vE '\+\+\+|ENOSYS|NOTSUP' "$1" |
wc -l; }
67 strace
-q -o log1
-e $stats ls -a --color=always d || fail
=1
68 n_stat1
=$
(count_stats log1
) || framework_failure_
71 && skip_
'No stat calls recognized on this platform'
73 # Populate the test directory.
76 && ln d
/regf d
/hlink \
77 && ln -s regf d
/slink \
78 && ln -s nowhere d
/dangle \
81 # Invocation under test.
82 strace
-q -o log2
-e $stats ls --color=always d || fail
=1
83 n_stat2
=$
(count_stats log2
) || framework_failure_
85 # Expect the same number of stat calls.
86 test $n_stat1 = $n_stat2 \
87 ||
{ fail
=1; head -n30 log
*; }