diff options
author | Sören Tempel | 2021-05-23 14:14:10 +0200 |
---|---|---|
committer | Denys Vlasenko | 2021-06-04 22:39:10 +0200 |
commit | 3d9c64915810cf684d75c8697b42e30c14011324 (patch) | |
tree | 96724f31a704818382107050a1ab9fe1dfbf25d9 /coreutils/ls.c | |
parent | 5a3d3b8055f684539f05e00c38fdc5cefb94c883 (diff) | |
download | busybox-3d9c64915810cf684d75c8697b42e30c14011324.zip busybox-3d9c64915810cf684d75c8697b42e30c14011324.tar.gz |
ls: don't output any colors with TERM=dumb
The TERM variable is usually set to "dumb" to indicate that the terminal
does not support any ANSI escape sequences. Presently, ls does not honor
this variable and outputs colors anyhow which results in unreadable
output, unless the user explicitly disables colors using `ls
--color=never`. The rational behind this change is that ls should "just
work" by default, even on dumb terminals.
For this reason, this patch adds a check which additionally consults the
TERM variable before printing any colors. This is analogous to the
existing check for ensuring that standard output is a tty. As such,
colors can still be forced with `--color=force`, even if TERM is set to
dumb.
function old new delta
is_TERM_dumb - 40 +40
ls_main 579 598 +19
.rodata 103246 103251 +5
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 64/0) Total: 64 bytes
Signed-off-by: Sören Tempel <soeren+git@soeren-tempel.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/ls.c')
-rw-r--r-- | coreutils/ls.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 80ef920..1f7d7f7 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -1145,11 +1145,15 @@ int ls_main(int argc UNUSED_PARAM, char **argv) #if ENABLE_FEATURE_LS_COLOR /* set G_show_color = 1/0 */ - if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { + if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && !is_TERM_dumb()) { char *p = getenv("LS_COLORS"); /* LS_COLORS is unset, or (not empty && not "none") ? */ - if (!p || (p[0] && strcmp(p, "none") != 0)) - G_show_color = 1; + if (!p || (p[0] && strcmp(p, "none") != 0)) { + if (isatty(STDOUT_FILENO)) { + /* check isatty() last because it's expensive (syscall) */ + G_show_color = 1; + } + } } if (opt & OPT_color) { if (color_opt[0] == 'n') @@ -1158,7 +1162,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv) case 3: case 4: case 5: - if (isatty(STDOUT_FILENO)) { + if (!is_TERM_dumb() && isatty(STDOUT_FILENO)) { case 0: case 1: case 2: |