1#!/bin/sh
2# Exercise df's --output option.
3
4# Copyright (C) 2012-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_ df
21
22# Ensure that --output is mutually exclusive with -i, -P, and -T.
23# Ensure that this limitation is not depending on the order of options.
24cat <<\EOF > exp || framework_failure_
25df: options OPT and --output are mutually exclusive
26Try 'df --help' for more information.
27EOF
28
29df -i --output '.' 2>out && fail=1
30sed 's/ -i / OPT /' out > out2
31compare exp out2 || fail=1
32
33df --output -i '.' 2>out && fail=1
34sed 's/ -i / OPT /' out > out2
35compare exp out2 || fail=1
36
37df -P --output '.' 2>out && fail=1
38sed 's/ -P / OPT /' out > out2
39compare exp out2 || fail=1
40
41df --output -P '.' 2>out && fail=1
42sed 's/ -P / OPT /' out > out2
43compare exp out2 || fail=1
44
45df -T --output '.' 2>out && fail=1
46sed 's/ -T / OPT /' out > out2
47compare exp out2 || fail=1
48
49df --output -T '.' 2>out && fail=1
50sed 's/ -T / OPT /' out > out2
51compare exp out2 || fail=1
52
53# Ensure that each field is only used once for the --output argument.
54cat <<\EOF > exp || framework_failure_
55df: option --output: field 'target' used more than once
56Try 'df --help' for more information.
57EOF
58
59df --output=target,source,target '.' 2>out && fail=1
60compare exp out || fail=1
61
62# Ensure that this limitation also works for split --output options.
63df --out=target,source --out=target '.' 2>out && fail=1
64compare exp out || fail=1
65
66# Ensure that the full output includes all fields, and
67# that --o (without argument) is identical to the full list.
68
69cat <<\EOF > exp || framework_failure_
70Filesystem Type Inodes IUsed IFree IUse% Size Used Avail Use% File Mounted on
71EOF
72
73df -h --o=source,fstype,itotal,iused,iavail,ipcent \
74 --o=size,used,avail,pcent,file,target '.' >out || fail=1
75sed -e '1 {
76          s/ [ ]*/ /g
77          q
78        }' out > out2
79compare exp out2 || fail=1
80
81df -h --output '.' >out || fail=1
82sed -e '1 {
83          s/ [ ]*/ /g
84          q
85        }' out > out2
86compare exp out2 || fail=1
87
88# Ensure that --output indicates the block size
89# when not using --human-readable
90cat <<\EOF > exp || framework_failure_
911K-blocks
92EOF
93
94df -B1K --output=size '.' >out || fail=1
95sed -e '1 {
96          s/ *//
97          q
98        }' out > out2
99compare exp out2 || fail=1
100
101# Ensure that the grand total line now contains a "-" in the TARGET field ...
102cat <<\EOF > exp || framework_failure_
103-
104EOF
105
106df --output=source,target --total '.' >out || fail=1
107sed -n -e '3 {
108             s/^total[ ]*//
109             p
110             q
111           }' out > out2
112compare exp out2 || fail=1
113
114# ... but it should read "total" if there is no SOURCE field.
115cat <<\EOF > exp || framework_failure_
116total
117EOF
118
119df --output=target --total '.' >out || fail=1
120sed -n -e '3 {
121             p
122             q
123           }' out > out2
124compare exp out2 || fail=1
125
126# Ensure that --output is mentioned in the usage.
127df --help > out || fail=1
128grep ' --output' out >/dev/null || { fail=1; cat out; }
129
130# Ensure that the FILE field contains the argument.
131cat <<\EOF > exp || framework_failure_
132.
133exp
134EOF
135
136df --output=file '.' exp >out || fail=1
137sed '1d' out > out2
138compare exp out2 || fail=1
139
140Exit $fail
141