diff options
author | Sören Tempel <soeren+git@soeren-tempel.net> | 2021-05-23 14:14:10 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-06-04 22:39:10 +0200 |
commit | 3d9c64915810cf684d75c8697b42e30c14011324 (patch) | |
tree | 96724f31a704818382107050a1ab9fe1dfbf25d9 | |
parent | 5a3d3b8055f684539f05e00c38fdc5cefb94c883 (diff) | |
download | busybox-w32-3d9c64915810cf684d75c8697b42e30c14011324.tar.gz busybox-w32-3d9c64915810cf684d75c8697b42e30c14011324.tar.bz2 busybox-w32-3d9c64915810cf684d75c8697b42e30c14011324.zip |
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>
-rw-r--r-- | coreutils/ls.c | 12 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/xfuncs.c | 6 |
3 files changed, 15 insertions, 4 deletions
diff --git a/coreutils/ls.c b/coreutils/ls.c index 80ef92079..1f7d7f7bf 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c | |||
@@ -1145,11 +1145,15 @@ int ls_main(int argc UNUSED_PARAM, char **argv) | |||
1145 | 1145 | ||
1146 | #if ENABLE_FEATURE_LS_COLOR | 1146 | #if ENABLE_FEATURE_LS_COLOR |
1147 | /* set G_show_color = 1/0 */ | 1147 | /* set G_show_color = 1/0 */ |
1148 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && isatty(STDOUT_FILENO)) { | 1148 | if (ENABLE_FEATURE_LS_COLOR_IS_DEFAULT && !is_TERM_dumb()) { |
1149 | char *p = getenv("LS_COLORS"); | 1149 | char *p = getenv("LS_COLORS"); |
1150 | /* LS_COLORS is unset, or (not empty && not "none") ? */ | 1150 | /* LS_COLORS is unset, or (not empty && not "none") ? */ |
1151 | if (!p || (p[0] && strcmp(p, "none") != 0)) | 1151 | if (!p || (p[0] && strcmp(p, "none") != 0)) { |
1152 | G_show_color = 1; | 1152 | if (isatty(STDOUT_FILENO)) { |
1153 | /* check isatty() last because it's expensive (syscall) */ | ||
1154 | G_show_color = 1; | ||
1155 | } | ||
1156 | } | ||
1153 | } | 1157 | } |
1154 | if (opt & OPT_color) { | 1158 | if (opt & OPT_color) { |
1155 | if (color_opt[0] == 'n') | 1159 | if (color_opt[0] == 'n') |
@@ -1158,7 +1162,7 @@ int ls_main(int argc UNUSED_PARAM, char **argv) | |||
1158 | case 3: | 1162 | case 3: |
1159 | case 4: | 1163 | case 4: |
1160 | case 5: | 1164 | case 5: |
1161 | if (isatty(STDOUT_FILENO)) { | 1165 | if (!is_TERM_dumb() && isatty(STDOUT_FILENO)) { |
1162 | case 0: | 1166 | case 0: |
1163 | case 1: | 1167 | case 1: |
1164 | case 2: | 1168 | case 2: |
diff --git a/include/libbb.h b/include/libbb.h index 03f9c35f3..4c9c83bd1 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -1773,6 +1773,7 @@ extern void print_login_issue(const char *issue_file, const char *tty) FAST_FUNC | |||
1773 | extern void print_login_prompt(void) FAST_FUNC; | 1773 | extern void print_login_prompt(void) FAST_FUNC; |
1774 | 1774 | ||
1775 | char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC; | 1775 | char *xmalloc_ttyname(int fd) FAST_FUNC RETURNS_MALLOC; |
1776 | int is_TERM_dumb(void) FAST_FUNC; | ||
1776 | /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */ | 1777 | /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */ |
1777 | int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC; | 1778 | int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC; |
1778 | int get_terminal_width(int fd) FAST_FUNC; | 1779 | int get_terminal_width(int fd) FAST_FUNC; |
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index d93d8aaf5..c81ce4546 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -303,6 +303,12 @@ int FAST_FUNC get_terminal_width(int fd) | |||
303 | return width; | 303 | return width; |
304 | } | 304 | } |
305 | 305 | ||
306 | int FAST_FUNC is_dumb_term(void) | ||
307 | { | ||
308 | char *term = getenv("TERM"); | ||
309 | return term && strcmp(term, "dumb") == 0; | ||
310 | } | ||
311 | |||
306 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) | 312 | int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp) |
307 | { | 313 | { |
308 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); | 314 | return tcsetattr(STDIN_FILENO, TCSANOW, tp); |