]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
ls: allow --classify to be ignored for non tty output
authorJason Kim <git@jasonk.me>
Sun, 19 Apr 2020 08:40:16 +0000 (01:40 -0700)
committerPádraig Brady <P@draigBrady.com>
Mon, 27 Apr 2020 12:48:32 +0000 (13:48 +0100)
Have the `ls` `--classify` option take an optional argument for when to
classify ("always", "auto", "never"), just like the optional argument
for `--color`.  When the optional argument is not specified, default to
"always" for backwards compatibility.

* src/ls.c (usage): Update help text.
(decode_switches): Support an optional argument for --classify.
* tests/ls/classify.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the new feature.

NEWS
doc/coreutils.texi
src/ls.c
tests/local.mk
tests/ls/classify.sh [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 24179360c9d5ecc875685d20c7bb0c79e7f70794..1f59a156a24c6f97b1ef232e376e29344e0c0862 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -19,6 +19,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   directory merely because it was removed.  This reverts a change
   that was made in release 8.32.
 
+** New Features
+
+   ls --classify now supports the "always", "auto", or "never" flags,
+   to support only outputting classifier characters if connected to a tty.
+
 
 * Noteworthy changes in release 8.32 (2020-03-05) [stable]
 
index 24e424c5413642751fa80fb28d735ef0c5542137..0e1dc48bd053cab0379fe77f41d735fbf88d6e92 100644 (file)
@@ -8004,7 +8004,7 @@ and on a @code{dirent.d_type}-capable file system, @command{ls}
 will perform only one @code{stat} call per command line argument.
 
 @item -F
-@itemx --classify
+@itemx --classify [=@var{when}]
 @itemx --indicator-style=classify
 @opindex -F
 @opindex --classify
@@ -8016,6 +8016,21 @@ for regular files that are executable, append @samp{*}.  The file type
 indicators are @samp{/} for directories, @samp{@@} for symbolic links,
 @samp{|} for FIFOs, @samp{=} for sockets, @samp{>} for doors,
 and nothing for regular files.
+@var{when} may be omitted, or one of:
+@itemize @bullet
+@item none
+@vindex none @r{classify option}
+- Do not classify.  This is the default.
+@item auto
+@vindex auto @r{classify option}
+@cindex terminal, using classify iff
+- Only classify if standard output is a terminal.
+@item always
+@vindex always @r{classify option}
+- Always classify.
+@end itemize
+Specifying @option{--classify} and no @var{when} is equivalent to
+@option{--classify=always}.
 @c The following sentence is the same as the one for -d.
 Do not follow symbolic links listed on the
 command line unless the @option{--dereference-command-line} (@option{-H}),
index 4acf5f44d96925d278d19dc495146bc8e326bb65..a69f9d6e4ea6f874a2e9af52a8c39bec6a17ad76 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -859,7 +859,7 @@ static struct option const long_options[] =
   {"width", required_argument, NULL, 'w'},
   {"almost-all", no_argument, NULL, 'A'},
   {"ignore-backups", no_argument, NULL, 'B'},
-  {"classify", no_argument, NULL, 'F'},
+  {"classify", optional_argument, NULL, 'F'},
   {"file-type", no_argument, NULL, FILE_TYPE_INDICATOR_OPTION},
   {"si", no_argument, NULL, SI_OPTION},
   {"dereference-command-line", no_argument, NULL, 'H'},
@@ -2093,8 +2093,20 @@ decode_switches (int argc, char **argv)
           break;
 
         case 'F':
-          indicator_style = classify;
-          break;
+          {
+            int i;
+            if (optarg)
+              i = XARGMATCH ("--classify", optarg, when_args, when_types);
+            else
+              /* Using --classify with no argument is equivalent to using
+                 --classify=always.  */
+              i = when_always;
+
+            if (i == when_always
+                || (i == when_if_tty && isatty (STDOUT_FILENO)))
+              indicator_style = classify;
+            break;
+          }
 
         case 'G':              /* inhibit display of group info */
           print_group = false;
@@ -5353,7 +5365,9 @@ Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.\n\
 "), stdout);
       fputs (_("\
   -f                         do not sort, enable -aU, disable -ls --color\n\
-  -F, --classify             append indicator (one of */=>@|) to entries\n\
+  -F, --classify[=WHEN]      append indicator (one of */=>@|) to entries;\n\
+                               WHEN can be 'always' (default if omitted),\n\
+                               'auto', or 'never'\n\
       --file-type            likewise, except do not append '*'\n\
       --format=WORD          across -x, commas -m, horizontal -x, long -l,\n\
                                single-column -1, verbose -l, vertical -C\n\
index 594a9d0b572687dd8325f5f485bc4dfe22bb2bdb..7992003938f8a0667209dbdabd6267496550dbf5 100644 (file)
@@ -593,6 +593,7 @@ all_tests =                                 \
   tests/ls/abmon-align.sh                      \
   tests/ls/birthtime.sh                                \
   tests/ls/block-size.sh                       \
+  tests/ls/classify.sh                         \
   tests/ls/color-clear-to-eol.sh               \
   tests/ls/color-dtype-dir.sh                  \
   tests/ls/color-norm.sh                       \
diff --git a/tests/ls/classify.sh b/tests/ls/classify.sh
new file mode 100755 (executable)
index 0000000..7dedb11
--- /dev/null
@@ -0,0 +1,63 @@
+#!/bin/sh
+# Test --classify processing
+
+# Copyright (C) 2020 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ ls
+
+mkdir testdir || framework_failure_
+cd testdir || framework_failure_
+mkdir dir || framework_failure_
+touch regular executable || framework_failure_
+chmod a+x executable || framework_failure_
+ln -s regular slink-reg || framework_failure_
+ln -s dir slink-dir || framework_failure_
+ln -s nowhere slink-dangle || framework_failure_
+mknod block b 20 20 2> /dev/null && block="block
+"
+mknod char c 10 10 2> /dev/null && char="char
+"
+mkfifo_or_skip_ fifo
+cd ..
+
+cat <<EOF > exp
+$block${char}dir/
+executable*
+fifo|
+regular
+slink-dangle@
+slink-dir@
+slink-reg@
+EOF
+sed 's/[*/@|]//' exp > exp2 || framework_failure_
+
+ls --classify testdir > out || fail=1
+ls --classify=always testdir > out2 || fail=1
+ls --classify=auto testdir > out3 || fail=1
+ls --classify=never testdir > out4 || fail=1
+
+compare exp out || fail=1
+
+compare exp out2 || fail=1
+
+compare exp2 out3 || fail=1
+
+compare exp2 out4 || fail=1
+
+returns_ 1 ls --classify=invalid || fail=1
+
+Exit $fail