]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
du: add the short option -A corresponding to --apparent-size
authorCollin Funk <collin.funk1@gmail.com>
Fri, 28 Nov 2025 05:33:30 +0000 (21:33 -0800)
committerCollin Funk <collin.funk1@gmail.com>
Fri, 28 Nov 2025 22:08:31 +0000 (14:08 -0800)
The --apparent-size option to 'du' was added in
coreutils-4.5.8 (2003). FreeBSD 8.0 (2009) added the same functionality
under the short option -A. This long option previously had no short
option, so this patch adds -A to be compatible with FreeBSD.

* NEWS: Mention the new short option.
* doc/coreutils.texi: Document the short option.
* src/du.c (usage): Likewise.
(APPARENT_SIZE_OPTION): Remove definition.
(EXCLUDE_OPTION): Define to CHAR_MAX + 1.
(long_options): Use the -A short option for --apparent-size.
(main): Likewise.
* tests/du/apparent.sh: Test that '-b', '-A -B 1', and
'--apparent-size --block-size 1' function the same.

NEWS
doc/coreutils.texi
src/du.c
tests/du/apparent.sh

diff --git a/NEWS b/NEWS
index 019451d32e205212894b4f3033560b9fea8bf6c3..2319e45175ea0f86df1ce79163ca5a0f9c7739ae 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -33,6 +33,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   'tail' now accepts the --debug option, which is currently used to
   detail the --follow implementation being used.
 
+  'du' now supports the short option -A corresponding to the existing long
+  option --apparent-size, for compatibility with FreeBSD.
+
 ** Changes in behavior
 
   'timeout' now honors ignored signals and will not propagate them.  E.g.,
index 098fa4761ac54a175c6ca051704eaea0dc277388..58368621208232358e2a77cd7a952b4104482e29 100644 (file)
@@ -12755,7 +12755,9 @@ The program accepts the following options.  Also see @ref{Common options}.
 @opindex --all
 Show counts for all files, not just directories.
 
-@item --apparent-size
+@item -A
+@itemx --apparent-size
+@opindex -A
 @opindex --apparent-size
 Print apparent sizes, rather than file system usage.  The apparent size of a
 file is the number of bytes reported by @code{wc -c} on regular files,
index 80d0b53d165b72686d92dc49a0176e2cac380b15..14aa0d652c8d9551081d96ed4ac1f6caa04502a8 100644 (file)
--- a/src/du.c
+++ b/src/du.c
@@ -195,8 +195,7 @@ static struct duinfo tot_dui;
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
 enum
 {
-  APPARENT_SIZE_OPTION = CHAR_MAX + 1,
-  EXCLUDE_OPTION,
+  EXCLUDE_OPTION = CHAR_MAX + 1,
   FILES0_FROM_OPTION,
   HUMAN_SI_OPTION,
 #if GNULIB_FTS_DEBUG
@@ -210,7 +209,7 @@ enum
 static struct option const long_options[] =
 {
   {"all", no_argument, nullptr, 'a'},
-  {"apparent-size", no_argument, nullptr, APPARENT_SIZE_OPTION},
+  {"apparent-size", no_argument, nullptr, 'A'},
   {"block-size", required_argument, nullptr, 'B'},
   {"bytes", no_argument, nullptr, 'b'},
   {"count-links", no_argument, nullptr, 'l'},
@@ -290,7 +289,7 @@ Summarize device usage of the set of FILEs, recursively for directories.\n\
       fputs (_("\
   -0, --null            end each output line with NUL, not newline\n\
   -a, --all             write counts for all files, not just directories\n\
-      --apparent-size   print apparent sizes rather than device usage; although\
+  -A, --apparent-size   print apparent sizes rather than device usage; although\
 \n\
                           the apparent size is usually smaller, it may be\n\
                           larger due to holes in ('sparse') files, internal\n\
@@ -745,7 +744,7 @@ main (int argc, char **argv)
   while (true)
     {
       int oi = -1;
-      int c = getopt_long (argc, argv, "0abd:chHklmst:xB:DLPSX:",
+      int c = getopt_long (argc, argv, "0aAbd:chHklmst:xB:DLPSX:",
                            long_options, &oi);
       if (c == -1)
         break;
@@ -766,7 +765,7 @@ main (int argc, char **argv)
           opt_all = true;
           break;
 
-        case APPARENT_SIZE_OPTION:
+        case 'A':
           apparent_size = true;
           break;
 
index 4a1064fc66d8a05ada4a9517d8006d6add624f86..43179736f21b12511203e38fb1d7c9bf3729894d 100755 (executable)
@@ -24,10 +24,13 @@ for f in $(seq 100); do
   echo foo >d/$f || framework_failure_
 done
 
-du -b d/* >separate || fail=1
-du -b d   >together || fail=1
-separate_sum=$($AWK '{sum+=$1}END{print sum}' separate) || framework_failure_
-together_sum=$($AWK '{sum+=$1}END{print sum}' together) || framework_failure_
-test $separate_sum -eq $together_sum || fail=1
+# Check that the following options are equivalent.
+for opts in '-b' '-A -B 1' '--apparent-size --block-size 1'; do
+  du $opts d/* >separate || fail=1
+  du $opts d   >together || fail=1
+  separate_sum=$($AWK '{sum+=$1}END{print sum}' separate) || framework_failure_
+  together_sum=$($AWK '{sum+=$1}END{print sum}' together) || framework_failure_
+  test $separate_sum -eq $together_sum || fail=1
+done
 
 Exit $fail