On 17/06/18 07:23, Bernhard Voelker wrote:
> On 06/17/2018 05:51 AM, Pádraig Brady wrote:
>> Subject: [PATCH] ls: ignore case when coloring file extensions
> 
> Nice one.
> This sounds like this would deserve a NEW entry as well.

Full patch attached.

thanks,
Pádraig

>From db4a5f49d467ad1f1abfa0f88144d9cf209a62c6 Mon Sep 17 00:00:00 2001
From: Kaxandra Labat <[email protected]>
Date: Sat, 16 Jun 2018 20:45:33 -0700
Subject: [PATCH] ls: ignore case when coloring file extensions

* src/ls.c (get_color_indicator): s/STREQ_LEN/c_strncasecmp/
* src/dircolors.hin: Remove a now redundant entry.
* tests/ls/color-ext.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the change in behavior.
---
 NEWS                  |  2 ++
 src/dircolors.hin     |  1 -
 src/ls.c              |  5 +++--
 tests/local.mk        |  1 +
 tests/ls/color-ext.sh | 41 +++++++++++++++++++++++++++++++++++++++++
 5 files changed, 47 insertions(+), 3 deletions(-)
 create mode 100755 tests/ls/color-ext.sh

diff --git a/NEWS b/NEWS
index 101afc0..acee8ec 100644
--- a/NEWS
+++ b/NEWS
@@ -46,6 +46,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   'cp --force file symlink' now removes the symlink even if
   it is self referential.
 
+  ls --color now matches file extensions case insensitively.
+
 ** Improvements
 
   cut supports line lengths up to the max file size on 32 bit systems.
diff --git a/src/dircolors.hin b/src/dircolors.hin
index 18381a7..cc97e29 100644
--- a/src/dircolors.hin
+++ b/src/dircolors.hin
@@ -98,7 +98,6 @@ EXEC 01;32
 .t7z 01;31
 .zip 01;31
 .z   01;31
-.Z   01;31
 .dz  01;31
 .gz  01;31
 .lrz 01;31
diff --git a/src/ls.c b/src/ls.c
index bae2c11..bf0c594 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -86,6 +86,7 @@
 
 #include "acl.h"
 #include "argmatch.h"
+#include "c-strcase.h"
 #include "dev-ino.h"
 #include "die.h"
 #include "error.h"
@@ -4766,8 +4767,8 @@ get_color_indicator (const struct fileinfo *f, bool symlink_target)
       for (ext = color_ext_list; ext != NULL; ext = ext->next)
         {
           if (ext->ext.len <= len
-              && STREQ_LEN (name - ext->ext.len, ext->ext.string,
-                            ext->ext.len))
+              && c_strncasecmp (name - ext->ext.len, ext->ext.string,
+                                ext->ext.len) == 0)
             break;
         }
     }
diff --git a/tests/local.mk b/tests/local.mk
index 1f8b189..528f9e5 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -583,6 +583,7 @@ all_tests =					\
   tests/ls/color-dtype-dir.sh			\
   tests/ls/color-norm.sh			\
   tests/ls/color-term.sh			\
+  tests/ls/color-ext.sh				\
   tests/ls/dangle.sh				\
   tests/ls/dired.sh				\
   tests/ls/file-type.sh				\
diff --git a/tests/ls/color-ext.sh b/tests/ls/color-ext.sh
new file mode 100755
index 0000000..b3c9e80
--- /dev/null
+++ b/tests/ls/color-ext.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+# Ensure "ls --color" properly colorizes file extensions
+
+# Copyright (C) 2018 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ ls
+working_umask_or_skip_
+
+touch img1.jpg IMG2.JPG file1.z file2.Z || framework_failure_
+code_jpg='01;35'
+code_z='01;31'
+c0=$(printf '\033[0m')
+c_jpg=$(printf '\033[%sm' $code_jpg)
+c_z=$(printf '\033[%sm' $code_z)
+
+LS_COLORS="*.jpg=$code_jpg:*.Z=$code_z" ls -U1 --color=always \
+  img1.jpg IMG2.JPG file1.z file2.Z > out || fail=1
+printf "$c0\
+${c_jpg}img1.jpg$c0
+${c_jpg}IMG2.JPG$c0
+${c_z}file1.z$c0
+${c_z}file2.Z$c0
+" > out_ok || framework_failure_
+compare out out_ok || fail=1
+
+
+Exit $fail
-- 
2.9.3

Reply via email to