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
This commit is contained in:
Kyle Evans 2018-08-18 20:55:20 +00:00
parent a06da7bafe
commit 041e6eb1c5
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=338027
2 changed files with 48 additions and 4 deletions

View file

@ -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

View file

@ -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)",