coreutils
[Top][All Lists]
Advanced

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

Re: ls and root directory indicator


From: Sami Kerola
Subject: Re: ls and root directory indicator
Date: Mon, 25 Feb 2013 19:51:09 +0000

On Mon, Feb 25, 2013 at 1:27 PM, Eric Blake <address@hidden> wrote:
> On 02/24/2013 11:09 AM, Pádraig Brady wrote:
>> On 02/24/2013 05:39 PM, Sami Kerola wrote:
>>> Hello,
>>>
>>> When using file type indicator to a root, e.g.,
>>>
>>> $ ls -Fd /
>>> //
>>>
>>> two slashes are printed. To me that feels wrong. Patch below is an
>>> attempt to make root print out to be a single slash. Perhaps there is
>>> more elegant way to do the same. Or maybe I am mistaking, and double
>>> slash is correct, although I could not find earlier posting about
>>> that.
>>
>> I agree it looks a bit awkward, but I think it's more consistent
>> to keep the trailing /.  Consider a script that filters on and
>> strips a trailing /
>
> I kind of like the idea of omitting the trailing slash for roots (note
> the plural); since '/' and '//' are not necessarily the same root, and
> listing '//' for '/' or '///' for '//' is confusing.  However, since the
> proposed patch does not special case '//', it is not an appropriate
> patch in its current form.

Hi Eric,

Did I understood correctly that a patch such below would be more preferred?

Secondly,pardon my ignorance, I thought '/' and '//' or how ever many
slashes are the same root. Is this some non-obvious portability
gotcha? A link to education material would be great.


>From 745f1782cf02e8241487f26e6b08cc30cd8fbdb5 Mon Sep 17 00:00:00 2001
From: Sami Kerola <address@hidden>
Date: Sun, 24 Feb 2013 17:17:20 +0000
Subject: [PATCH] ls: do not print directory indicator for root
Organization: Lastminute.com

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_only().
---
 src/ls.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/ls.c b/src/ls.c
index d9876f8..70fbd23 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 int is_root_only(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_only(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_only(f->name))
     width += print_type_indicator (f->stat_ok, f->stat.st_mode, f->filetype);

   return width;
@@ -4397,6 +4398,21 @@ put_indicator (const struct bin_str *ind)
   fwrite (ind->string, ind->len, 1, stdout);
 }

+/* Find out if the output name is root, and represent it as a single
+ * slash even if file type indication is requested.  */
+static int _GL_ATTRIBUTE_PURE
+is_root_only (const char *path)
+{
+  while (*path)
+    {
+      if (*path == '/')
+        path++;
+      else
+        return 1;
+    }
+  return 0;
+}
+
 static size_t
 length_of_file_name_and_frills (const struct fileinfo *f)
 {
-- 
1.8.1.4



reply via email to

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