1#!/bin/sh
2# make sure --dired option works
3
4# Copyright (C) 2001-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
21
22
23# Check with constant positions
24mkdir dir || framework_failure_
25
26cat <<EOF > exp
27  dir:
28  total 0
29//SUBDIRED// 2 5
30//DIRED-OPTIONS// --quoting-style=literal
31EOF
32for opt in '-l' '' '--hyperlink' '-x'; do
33  LC_MESSAGES=C ls $opt -R --dired dir > out || fail=1
34  compare exp out || fail=1
35done
36
37
38# Check with varying positions (due to usernames etc.)
39# Also use multibyte characters to show --dired counts bytes not characters
40touch dir/1a dir/2á || framework_failure_
41mkdir -p dir/3dir || framework_failure_
42
43ls -l --dired dir | tee /tmp/pb.ls> out  || fail=1
44
45dired_values=$(grep "//DIRED//" out| cut -d' ' -f2-)
46expected_files="1a 2á 3dir"
47
48dired_count=$(printf '%s\n' $dired_values | wc -l)
49expected_count=$(printf '%s\n' $expected_files | wc -l)
50
51if test "$expected_count" -ne $(($dired_count / 2)); then
52  echo "Mismatch in number of files!" \
53       "Expected: $expected_count, Found: $(($dired_count / 2))"
54  fail=1
55fi
56
57# Split the values into pairs and extract the filenames
58index=1
59set -- $dired_values
60while test "$#" -gt 0; do
61  extracted_filename=$(head -c "$2" out | tail -c +"$(($1 + 1))")
62  expected_file=$(echo $expected_files | cut -d' ' -f$index)
63  if test "$extracted_filename" != "$expected_file"; then
64    echo "Mismatch! Expected: $expected_file, Found: $extracted_filename"
65    fail=1
66  fi
67  shift; shift
68  index=$(($index + 1))
69done
70
71
72Exit $fail
73