tech-userlevel archive

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

Re: glob(3) extension



>> I propose an addition to glob(3): GLOB_HIDE_DOTDIRS, which makes .
>> and .. vanish when matching wildcards, even if GLOB_PERIOD is set.

(I actually ended up calling it GLOB_NO_DOTDIRS.)

> It's a good idea; go for it.

Here are the diffs.  I'm not simply committing them myself because I
have no machines running -current to test them on.  With these changes,
a -current source tree builds on a 4.0 i386 machine.  If they not only
build but work in -current, someone commit them directly, or tell me
they work and I'll do the honours.

While doing this, I noticed that whoever added GLOB_PERIOD apparently
fell victim to something I also fell victim to: thinking that 0x1000,
the next bit after 0x0800, was available.  It's not; that's
GLOB_NOESCAPE (I found this out the hard way).  So these diffs also
move GLOB_PERIOD to 0x2000 - and comment the issue somewhat.

These diffs are relative to
.\"     $NetBSD: glob.3,v 1.31 2007/12/05 20:25:55 christos Exp $
/*      $NetBSD: glob.c,v 1.21 2008/02/01 23:29:54 christos Exp $       */
/*      $NetBSD: glob.h,v 1.22 2007/12/05 20:25:54 christos Exp $       */

--- STOCK/lib/libc/gen/glob.3   2008-02-22 09:54:54.000000000 -0500
+++ NEW/lib/libc/gen/glob.3     2008-02-22 01:05:22.000000000 -0500
@@ -261,6 +261,14 @@
 .Li */../*/..
 .It Dv GLOB_PERIOD
 Allow metacharacters to match a leading period in a filename.
+.It Dv GLOB_NO_DOTDIRS
+Hide
+.Sq Li \&.
+and
+.Sq Li \&..
+from metacharacter matches, regardless of whether
+.Dv GLOB_PERIOD
+is set and whether the pattern component begins with a literal period.
 .El
 .Pp
 If, during the search, a directory is encountered that cannot be opened
--- STOCK/lib/libc/gen/glob.c   2008-02-22 09:55:01.000000000 -0500
+++ NEW/lib/libc/gen/glob.c     2008-02-22 00:58:23.000000000 -0500
@@ -61,6 +61,8 @@
  *     expand {1,2}{a,b} to 1a 1b 2a 2b 
  * GLOB_PERIOD:
  *     allow metacharacters to match leading dots in filenames.
+ * GLOB_NO_DOTDIRS:
+ *     . and .. are hidden from wildcards, even if GLOB_PERIOD is set.
  * gl_matchc:
  *     Number of matches in the current invocation of glob.
  */
@@ -727,6 +729,14 @@
                        if (dp->d_name[0] == DOT && *pattern != DOT)
                                continue;
                /*
+                * If GLOB_NO_DOTDIRS is set, . and .. vanish.
+                */
+               if ((pglob->gl_flags & GLOB_NO_DOTDIRS) &&
+                   (dp->d_name[0] == DOT) &&
+                   ((dp->d_name[1] == EOS) ||
+                    ((dp->d_name[1] == DOT) && (dp->d_name[2] == EOS))))
+                       continue;
+               /*
                 * The resulting string contains EOS, so we can
                 * use the pathlim character, if it is the nul
                 */
--- STOCK/include/glob.h        2008-02-22 09:54:09.000000000 -0500
+++ NEW/include/glob.h  2008-02-22 00:54:05.000000000 -0500
@@ -84,13 +84,16 @@
 #define        GLOB_NOSYS      (-4)    /* Implementation does not support 
function. */
 
 #if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
+/* 0x0001-0x0020 defined above */
 #define        GLOB_ALTDIRFUNC 0x0040  /* Use alternately specified directory 
funcs. */
 #define        GLOB_BRACE      0x0080  /* Expand braces ala csh. */
 #define        GLOB_MAGCHAR    0x0100  /* Pattern had globbing characters. */
 #define        GLOB_NOMAGIC    0x0200  /* GLOB_NOCHECK without magic chars 
(csh). */
 #define GLOB_LIMIT     0x0400  /* Limit memory used by matches to ARG_MAX */
 #define        GLOB_TILDE      0x0800  /* Expand tilde names from the passwd 
file. */
-#define        GLOB_PERIOD     0x1000  /* Allow metachars to match leading 
periods. */
+/* 0x1000 defined above */
+#define        GLOB_PERIOD     0x2000  /* Allow metachars to match leading 
periods. */
+#define GLOB_NO_DOTDIRS        0x4000  /* Make . and .. vanish from wildcards. 
*/
 #define        GLOB_QUOTE      0               /* source compatibility */
 
 #define        GLOB_ABEND      GLOB_ABORTED    /* source compatibility */

/~\ The ASCII                           der Mouse
\ / Ribbon Campaign
 X  Against HTML               mouse%rodents.montreal.qc.ca@localhost
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index