coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] ls: do not print directory indicator for root


From: Sami Kerola
Subject: [PATCH] ls: do not print directory indicator for root
Date: Sun, 12 May 2013 21:40:44 +0100

Previously 'ls -Fd /' printed unnecessary directory indicator.  The root
is represented by '/' character, which does not need to be repeated.

* src/ls.c: (print_long_format, print_file_name_and_frills): Check if a
  output name is root, and skip indicator when necessary.
* src/ls.c: A new function is_root().
* tests/local.mk: Add ts/ls/root-slash.sh.
* tests/ls/root-slash.sh: New file.
---
 src/ls.c               | 12 ++++++++++--
 tests/local.mk         |  1 +
 tests/ls/root-slash.sh | 31 +++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100755 tests/ls/root-slash.sh

diff --git a/src/ls.c b/src/ls.c
index d9876f8..74e038e 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -250,6 +250,7 @@ static uintmax_t gobble_file (char const *name, enum 
filetype type,
 static bool print_color_indicator (const struct fileinfo *f,
                                    bool symlink_target);
 static void put_indicator (const struct bin_str *ind);
+static bool is_root(const char *path);
 static void add_ignore_pattern (const char *pattern);
 static void attach (char *dest, const char *dirname, const char *name);
 static void clear_files (void);
@@ -3970,7 +3971,7 @@ print_long_format (const struct fileinfo *f)
             print_type_indicator (true, f->linkmode, unknown);
         }
     }
-  else if (indicator_style != none)
+  else if (indicator_style != none && !is_root(f->name))
     print_type_indicator (f->stat_ok, f->stat.st_mode, f->filetype);
 }
 
@@ -4212,7 +4213,7 @@ print_file_name_and_frills (const struct fileinfo *f, 
size_t start_col)
 
   size_t width = print_name_with_quoting (f, false, NULL, start_col);
 
-  if (indicator_style != none)
+  if (indicator_style != none && !is_root(f->name))
     width += print_type_indicator (f->stat_ok, f->stat.st_mode, f->filetype);
 
   return width;
@@ -4397,6 +4398,13 @@ put_indicator (const struct bin_str *ind)
   fwrite (ind->string, ind->len, 1, stdout);
 }
 
+/* Find out if the path is root.  */
+static bool _GL_ATTRIBUTE_PURE
+is_root (const char *path)
+{
+  return path[strspn(path, "/")] == '\0';
+}
+
 static size_t
 length_of_file_name_and_frills (const struct fileinfo *f)
 {
diff --git a/tests/local.mk b/tests/local.mk
index 5ec7d98..1f3fd70 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -542,6 +542,7 @@ all_tests =                                 \
   tests/ls/readdir-mountpoint-inode.sh         \
   tests/ls/recursive.sh                                \
   tests/ls/root-rel-symlink-color.sh           \
+  tests/ls/root-slash.sh                       \
   tests/ls/rt-1.sh                             \
   tests/ls/slink-acl.sh                                \
   tests/ls/stat-dtype.sh                       \
diff --git a/tests/ls/root-slash.sh b/tests/ls/root-slash.sh
new file mode 100755
index 0000000..6646322
--- /dev/null
+++ b/tests/ls/root-slash.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+# check -F or -p will not add extra slashes to root
+
+# Copyright (C) 2013 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 <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ ls
+
+LC_MESSAGES=C ls -pd / > out || fail=1
+LC_MESSAGES=C ls -Fd / >> out || fail=1
+cat <<EOF > exp
+/
+/
+EOF
+
+compare exp out || fail=1
+
+Exit $fail
-- 
1.8.2.3




reply via email to

[Prev in Thread] Current Thread [Next in Thread]