Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 16 Aug 2018 01:27:16 +0000 (UTC)
From:      Kyle Evans <kevans@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r337885 - head/bin/ls
Message-ID:  <201808160127.w7G1RGZr011482@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: kevans
Date: Thu Aug 16 01:27:16 2018
New Revision: 337885
URL: https://svnweb.freebsd.org/changeset/base/337885

Log:
  ls(1): Fix color env var checking
  
  CLICOLOR will behavior as always- if present at all in the environment,
  allow colors.
  
  COLORTERM, recently enforced, will have to be both present and not empty.
  
  Submitted by:	imp

Modified:
  head/bin/ls/ls.c

Modified: head/bin/ls/ls.c
==============================================================================
--- head/bin/ls/ls.c	Thu Aug 16 01:18:20 2018	(r337884)
+++ head/bin/ls/ls.c	Thu Aug 16 01:27:16 2018	(r337885)
@@ -60,6 +60,7 @@ __FBSDID("$FreeBSD$");
 #include <limits.h>
 #include <locale.h>
 #include <pwd.h>
+#include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -152,6 +153,29 @@ char *enter_bold;		/* ANSI sequence to set color to bo
 
 static int rval;
 
+static bool
+do_color_from_env(void)
+{
+	const char *p;
+	bool doit;
+
+	doit = false;
+	p = getenv("CLICOLOR");
+	if (p == NULL) {
+		/*
+		 * COLORTERM is the more standard name for this variable.  We'll
+		 * honor it as long as it's both set and not empty.
+		 */
+		p = getenv("COLORTERM");
+		if (p != NULL && *p != '\0')
+			doit = true;
+	} else
+		doit = true;
+
+	return (doit &&
+	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")));
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -368,8 +392,7 @@ main(int argc, char *argv[])
 		f_listdot = 1;
 
 	/* Enabling of colours is conditional on the environment. */
-	if ((getenv("CLICOLOR") || getenv("COLORTERM")) &&
-	    (isatty(STDOUT_FILENO) || getenv("CLICOLOR_FORCE")))
+	if (do_color_from_env())
 #ifdef COLORLS
 		if (tgetent(termcapbuf, getenv("TERM")) == 1) {
 			ansi_fgcol = tgetstr("AF", &bp);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201808160127.w7G1RGZr011482>