bug-coreutils
[Top][All Lists]
Advanced

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

[patch] add option to ls to display only directories


From: Johannes Hausensteiner
Subject: [patch] add option to ls to display only directories
Date: Thu, 28 Jul 2005 19:47:47 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5) Gecko/20041221

Hi,

I added a '-z' option to ls (coreutils 5.3.0) to show only directories.
It is all done in ls.c:

--- coreutils-5.3.0/src/ls.c    2004-12-17 08:34:35.000000000 +0100
+++ coreutils-5.3.0.jo/src/ls.c 2005-07-28 19:11:32.000000000 +0200
@@ -588,7 +588,10 @@
   IGNORE_DOT_AND_DOTDOT,

   /* Ignore only files specified by --ignore.  */
-  IGNORE_MINIMAL
+  IGNORE_MINIMAL,
+
+  /* show diretories only */
+  IGNORE_ALL_BUT_DIRECTORIES
 } ignore_mode;

 /* A linked list of shell-style globbing patterns.  If a non-argument
@@ -733,6 +736,7 @@
   {"reverse", no_argument, 0, 'r'},
   {"size", no_argument, 0, 's'},
   {"width", required_argument, 0, 'w'},
+  {"directories-only", no_argument, 0, 'z'},
   {"almost-all", no_argument, 0, 'A'},
   {"ignore-backups", no_argument, 0, 'B'},
   {"classify", no_argument, 0, 'F'},
@@ -1465,7 +1469,7 @@
   }

   while ((c = getopt_long (argc, argv,
-                          "abcdfghiklmnopqrstuvw:xABCDFGHI:LNQRST:UX1",
+                          "abcdfghiklmnopqrstuvw:xzABCDFGHI:LNQRST:UX1",
                           long_options, NULL)) != -1)
     {
       switch (c)
@@ -1580,6 +1584,10 @@
          format = horizontal;
          break;

+       case 'z':
+         ignore_mode = IGNORE_ALL_BUT_DIRECTORIES;
+         break;
+
        case 'A':
          if (ignore_mode == IGNORE_DEFAULT)
            ignore_mode = IGNORE_DOT_AND_DOTDOT;
@@ -2407,12 +2415,26 @@
 static bool
 file_ignored (char const *name)
 {
-  return ((ignore_mode != IGNORE_MINIMAL
-          && name[0] == '.'
-          && (ignore_mode == IGNORE_DEFAULT || ! name[1 + (name[1] == '.')]))
-         || (ignore_mode == IGNORE_DEFAULT
-             && patterns_match (hide_patterns, name))
-         || patterns_match (ignore_patterns, name));
+  bool ret;
+
+  if (ignore_mode == IGNORE_ALL_BUT_DIRECTORIES)
+    {
+      struct stat statbuf;
+      stat (name, &statbuf);
+
+      ret = !(S_ISDIR(statbuf.st_mode)
+              && name[1] != '\0'
+              && (name[1] != '.' || name[2] != '\0'));
+    }
+  else
+    ret = (ignore_mode != IGNORE_MINIMAL
+           && name[0] == '.'
+ && (ignore_mode == IGNORE_DEFAULT || ! name[1 + (name[1] == '.')]))
+          || (ignore_mode == IGNORE_DEFAULT
+              && patterns_match (hide_patterns, name))
+          || patterns_match (ignore_patterns, name);
+
+  return ret;
 }

 /* POSIX requires that a file size be printed without a sign, even
@@ -4174,6 +4196,7 @@
-w, --width=COLS assume screen width instead of current value\n\ -x list entries by lines instead of by columns\n\
   -X                         sort alphabetically by entry extension\n\
+  -z, --directories-only     list only directories\n\
   -1                         list one file per line\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);

-------------------------------------------------------------------------

... and here goes the txei doc:

--- coreutils-5.3.0/doc/coreutils.texi  2005-01-07 21:09:47.000000000 +0100
+++ coreutils-5.3.0.jo/doc/coreutils.texi 2005-07-28 19:26:21.000000000 +0200
@@ -5433,6 +5433,12 @@
 @cindex directory listing, recursive
 List the contents of all directories recursively.

address@hidden -z
address@hidden --directories-only
address@hidden -z
address@hidden --directories-only
+Show only entries which are directories.
+
 @end table


-------------------------------------------------------------------------




reply via email to

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