From 041e6eb1c5c9c50ec82679cd3e04d028997ecc4f Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Sat, 18 Aug 2018 20:55:20 +0000 Subject: [PATCH] ls(1): Support other aliases for --color arguments used by GNU ls(1) These aliases are supported and documented in the man page. For now, they will not be mentioned in the error when an invalid argument is encountered, instead keeping that list to the shorter 'preferred' names of each argument. Reported by: rgrimes --- bin/ls/ls.1 | 22 +++++++++++++++++++++- bin/ls/ls.c | 30 +++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/bin/ls/ls.1 b/bin/ls/ls.1 index 6f4303a349a..dde947be987 100644 --- a/bin/ls/ls.1 +++ b/bin/ls/ls.1 @@ -32,7 +32,7 @@ .\" @(#)ls.1 8.7 (Berkeley) 7/29/94 .\" $FreeBSD$ .\" -.Dd August 16, 2018 +.Dd August 18, 2018 .Dt LS 1 .Os .Sh NAME @@ -252,6 +252,26 @@ environment variable is set and not empty. .Pp .Cm never will disable color regardless of environment variables. +.Pp +For compatibility with GNU coreutils, +.Nm +supports +.Cm yes +or +.Cm force +as equivalent to +.Cm always , +.Cm no +or +.Cm none +as equivalent to +.Cm never , +and +.Cm tty +or +.Cm if-tty +as equivalent to +.Cm auto . .It Fl d Directories are listed as plain files (not searched recursively). .It Fl f diff --git a/bin/ls/ls.c b/bin/ls/ls.c index 9a8e855176e..19effce3d19 100644 --- a/bin/ls/ls.c +++ b/bin/ls/ls.c @@ -200,6 +200,30 @@ do_color(void) return (do_color_from_env()); } +static bool +do_color_always(const char *term) +{ + + return (strcmp(term, "always") == 0 || strcmp(term, "yes") == 0 || + strcmp(term, "force") == 0); +} + +static bool +do_color_never(const char *term) +{ + + return (strcmp(term, "never") == 0 || strcmp(term, "no") == 0 || + strcmp(term, "none") == 0); +} + +static bool +do_color_auto(const char *term) +{ + + return (strcmp(term, "auto") == 0 || strcmp(term, "tty") == 0 || + strcmp(term, "if-tty") == 0); +} + int main(int argc, char *argv[]) { @@ -406,11 +430,11 @@ main(int argc, char *argv[]) break; #ifdef COLORLS case COLOR_OPT: - if (optarg == NULL || strcmp(optarg, "always") == 0) + if (optarg == NULL || do_color_always(optarg)) colorflag = COLORFLAG_ALWAYS; - else if (strcmp(optarg, "auto") == 0) + else if (do_color_auto(optarg)) colorflag = COLORFLAG_AUTO; - else if (strcmp(optarg, "never") == 0) + else if (do_color_never(optarg)) colorflag = COLORFLAG_NEVER; else errx(2, "unsupported --color value '%s' (must be always, auto, or never)",