Collin Funk <[email protected]> writes:

> Pádraig Brady <[email protected]> writes:
>
>> On 28/11/2025 05:49, Collin Funk wrote:
>>> This link is too long for the commit message, but here is the commit
>>> 'du -A' was added to FreeBSD:
>>> https://github.com/freebsd/freebsd-src/commit/fd543f275900cc66a0ce4725699f1e528e526792
>>
>> Fair enough.
>> A test would be good though.
>
> Well if I forgot to add it to getopt, for example, tests in tests/misc
> would fail. But I can add a check in tests/du/apparent.sh to be more
> complete.

I thought it was a bit strange that tests/du/apparent.sh didn't use
'--apparent-size'. I pushed the attached v2 patch which make sure '-b'
is equivalent to '--apparent-size --block-size 1' as documented. And
also checks the corresponding short options, so '-A' is tested.

Collin

>From 90856dd867ba7a86a0a54e987048b0250b716c3b Mon Sep 17 00:00:00 2001
Message-ID: <90856dd867ba7a86a0a54e987048b0250b716c3b.1764368114.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Thu, 27 Nov 2025 21:33:30 -0800
Subject: [PATCH v2] du: add the short option -A corresponding to
 --apparent-size

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                 |  3 +++
 doc/coreutils.texi   |  4 +++-
 src/du.c             | 11 +++++------
 tests/du/apparent.sh | 13 ++++++++-----
 4 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/NEWS b/NEWS
index 019451d32..2319e4517 100644
--- 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.,
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 098fa4761..583686212 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -12755,7 +12755,9 @@ @node du invocation
 @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,
diff --git a/src/du.c b/src/du.c
index 80d0b53d1..14aa0d652 100644
--- 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;
 
diff --git a/tests/du/apparent.sh b/tests/du/apparent.sh
index 4a1064fc6..43179736f 100755
--- a/tests/du/apparent.sh
+++ b/tests/du/apparent.sh
@@ -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
-- 
2.52.0

Reply via email to