From: Jason Kim Date: Sun, 19 Apr 2020 08:40:16 +0000 (-0700) Subject: ls: allow --classify to be ignored for non tty output X-Git-Tag: v9.0~250 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c8a385aa4c342a2b1af5f5a0604afb249a1cf66;p=thirdparty%2Fcoreutils.git ls: allow --classify to be ignored for non tty output 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. --- diff --git a/NEWS b/NEWS index 24179360c9..1f59a156a2 100644 --- 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] diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 24e424c541..0e1dc48bd0 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -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}), diff --git a/src/ls.c b/src/ls.c index 4acf5f44d9..a69f9d6e4e 100644 --- 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\ diff --git a/tests/local.mk b/tests/local.mk index 594a9d0b57..7992003938 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -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 index 0000000000..7dedb110fc --- /dev/null +++ b/tests/ls/classify.sh @@ -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 . + +. "${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 < 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