Source-Changes-HG archive

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

[src/trunk]: src Add GLOB_TILDE_CHECK (from GNU)



details:   https://anonhg.NetBSD.org/src/rev/c0d77e92c65f
branches:  trunk
changeset: 451605:c0d77e92c65f
user:      christos <christos%NetBSD.org@localhost>
date:      Wed May 29 01:21:33 2019 +0000

description:
Add GLOB_TILDE_CHECK (from GNU)

diffstat:

 include/glob.h      |  47 ++++++++++++++++++++++++-----------------------
 lib/libc/gen/glob.3 |  16 +++++++++++++---
 lib/libc/gen/glob.c |  42 +++++++++++++++++++++++-------------------
 3 files changed, 60 insertions(+), 45 deletions(-)

diffs (229 lines):

diff -r 71e04f5ede5e -r c0d77e92c65f include/glob.h
--- a/include/glob.h    Tue May 28 22:11:32 2019 +0000
+++ b/include/glob.h    Wed May 29 01:21:33 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: glob.h,v 1.26 2010/09/06 14:38:56 christos Exp $       */
+/*     $NetBSD: glob.h,v 1.27 2019/05/29 01:21:33 christos Exp $       */
 
 /*
  * Copyright (c) 1989, 1993
@@ -70,31 +70,32 @@
        int (*gl_stat)(const char *, __gl_stat_t *);
 } glob_t;
 
-#define        GLOB_APPEND     0x0001  /* Append to output from previous call. */
-#define        GLOB_DOOFFS     0x0002  /* Use gl_offs. */
-#define        GLOB_ERR        0x0004  /* Return on error. */
-#define        GLOB_MARK       0x0008  /* Append / to matching directories. */
-#define        GLOB_NOCHECK    0x0010  /* Return pattern itself if nothing matches. */
-#define        GLOB_NOSORT     0x0020  /* Don't sort. */
-#define        GLOB_NOESCAPE   0x1000  /* Disable backslash escaping. */
+#define        GLOB_APPEND      0x00001 /* Append to output from previous call. */
+#define        GLOB_DOOFFS      0x00002 /* Use gl_offs. */
+#define        GLOB_ERR         0x00004 /* Return on error. */
+#define        GLOB_MARK        0x00008 /* Append / to matching directories. */
+#define        GLOB_NOCHECK     0x00010 /* Return pattern itself if nothing matches. */
+#define        GLOB_NOSORT      0x00020 /* Don't sort. */
+#define        GLOB_NOESCAPE    0x01000 /* Disable backslash escaping. */
 
-#define        GLOB_NOSPACE    (-1)    /* Malloc call failed. */
-#define        GLOB_ABORTED    (-2)    /* Unignored error. */
-#define        GLOB_NOMATCH    (-3)    /* No match, and GLOB_NOCHECK was not set. */
-#define        GLOB_NOSYS      (-4)    /* Implementation does not support function. */
+#define        GLOB_NOSPACE     (-1)    /* Malloc call failed. */
+#define        GLOB_ABORTED     (-2)    /* Unignored error. */
+#define        GLOB_NOMATCH     (-3)    /* No match, and GLOB_NOCHECK was not set. */
+#define        GLOB_NOSYS       (-4)    /* Implementation does not support function. */
 
 #if defined(_NETBSD_SOURCE) || defined(HAVE_NBTOOL_CONFIG_H)
-#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. */
-/*     GLOB_NOESCAPE   0x1000  above */
-#define        GLOB_PERIOD     0x2000  /* Allow metachars to match leading periods. */
-#define        GLOB_NO_DOTDIRS 0x4000  /* Make . and .. vanish from wildcards. */
-#define        GLOB_STAR       0x8000  /* Use glob ** to recurse directories */
-#define        GLOB_QUOTE      0       /* source compatibility */
+#define        GLOB_ALTDIRFUNC  0x00040 /* Use alternately specified directory funcs. */
+#define        GLOB_BRACE       0x00080 /* Expand braces ala csh. */
+#define        GLOB_MAGCHAR     0x00100 /* Pattern had globbing characters. */
+#define        GLOB_NOMAGIC     0x00200 /* GLOB_NOCHECK without magic chars (csh). */
+#define        GLOB_LIMIT       0x00400 /* Limit memory used by matches to ARG_MAX */
+#define        GLOB_TILDE       0x00800 /* Expand tilde names from the passwd file. */
+/*     GLOB_NOESCAPE    0x01000 above */
+#define        GLOB_PERIOD      0x02000 /* Allow metachars to match leading periods. */
+#define        GLOB_NO_DOTDIRS  0x04000 /* Make . and .. vanish from wildcards. */
+#define        GLOB_STAR        0x08000 /* Use glob ** to recurse directories */
+#define        GLOB_TILDE_CHECK 0x10000 /* Expand tilde names from the passwd file. */
+#define        GLOB_QUOTE       0       /* source compatibility */
 
 #define        GLOB_ABEND      GLOB_ABORTED    /* source compatibility */
 #endif
diff -r 71e04f5ede5e -r c0d77e92c65f lib/libc/gen/glob.3
--- a/lib/libc/gen/glob.3       Tue May 28 22:11:32 2019 +0000
+++ b/lib/libc/gen/glob.3       Wed May 29 01:21:33 2019 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: glob.3,v 1.43 2017/07/03 21:32:49 wiz Exp $
+.\"    $NetBSD: glob.3,v 1.44 2019/05/29 01:21:33 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1991, 1993, 1994
 .\"    The Regents of the University of California.  All rights reserved.
@@ -31,7 +31,7 @@
 .\"
 .\"     @(#)glob.3     8.3 (Berkeley) 4/16/94
 .\"
-.Dd December 27, 2012
+.Dd May 28, 2019
 .Dt GLOB 3
 .Os
 .Sh NAME
@@ -200,7 +200,7 @@
 .Fa flags ,
 however, they are non-standard extensions to
 .St -p1003.2 .
-.Bl -tag -width GLOB_ALTDIRFUNC
+.Bl -tag -width GLOB_TILDE_CHECK
 .It Dv GLOB_ALTDIRFUNC
 The following additional fields in the pglob structure have been
 initialized with alternate functions for glob to use to open, read,
@@ -285,6 +285,16 @@
 Expand patterns that start with
 .Ql ~
 to user name home directories.
+If the user with the given user name (or the user id of the current user
+in the case of
+.Dq ~/ )
+is not found, the original pattern is returned.
+.It Dv GLOB_TILDE_CHECK
+When used with
+.Dv GLOB_TILDE
+and the user name or the user id is not found, then
+.Dv GLOB_NOMATCH
+is returned instead of the original pattern.
 .El
 .Pp
 If, during the search, a directory is encountered that cannot be opened
diff -r 71e04f5ede5e -r c0d77e92c65f lib/libc/gen/glob.c
--- a/lib/libc/gen/glob.c       Tue May 28 22:11:32 2019 +0000
+++ b/lib/libc/gen/glob.c       Wed May 29 01:21:33 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: glob.c,v 1.38 2017/05/08 14:42:16 christos Exp $       */
+/*     $NetBSD: glob.c,v 1.39 2019/05/29 01:21:33 christos Exp $       */
 
 /*
  * Copyright (c) 1989, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)glob.c     8.3 (Berkeley) 10/13/93";
 #else
-__RCSID("$NetBSD: glob.c,v 1.38 2017/05/08 14:42:16 christos Exp $");
+__RCSID("$NetBSD: glob.c,v 1.39 2019/05/29 01:21:33 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -170,7 +170,8 @@
 static int      glob3(Char *, Char *, Char *, const Char *, const Char *,
     const Char *, glob_t *, struct glob_limit *);
 static int      globextend(const Char *, glob_t *, struct glob_limit *);
-static const Char *globtilde(const Char *, Char *, size_t, glob_t *);
+static int       globtilde(const Char **, const Char *, Char *, size_t,
+    glob_t *);
 static int      globexp1(const Char *, glob_t *, struct glob_limit *);
 static int      globexp2(const Char *, const Char *, glob_t *, int *,
     struct glob_limit *);
@@ -378,8 +379,9 @@
 /*
  * expand tilde from the passwd file.
  */
-static const Char *
-globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
+static int
+globtilde(const Char **qpatnext, const Char *pattern, Char *patbuf,
+    size_t patsize, glob_t *pglob)
 {
        struct passwd *pwd;
        const char *h;
@@ -397,9 +399,10 @@
        _DIAGASSERT(pattern != NULL);
        _DIAGASSERT(patbuf != NULL);
        _DIAGASSERT(pglob != NULL);
+       *qpatnext = pattern;
 
        if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE))
-               return pattern;
+               return 0;
 
        /* Copy up to the end of the string or / */
        for (p = pattern + 1, d = (char *)(void *)patbuf; 
@@ -408,7 +411,7 @@
                continue;
 
        if (d == (char *)(void *)pend)
-               return NULL;
+               return GLOB_ABEND;
 
        *d = EOS;
        d = (char *)(void *)patbuf;
@@ -425,9 +428,8 @@
                        if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
                            &pwd) != 0 || pwd == NULL)
 #endif
-                               return pattern;
-                       else
-                               h = pwd->pw_dir;
+                               goto nouser;
+                       h = pwd->pw_dir;
                }
        }
        else {
@@ -440,9 +442,8 @@
                if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
                    pwd == NULL)
 #endif
-                       return pattern;
-               else
-                       h = pwd->pw_dir;
+                       goto nouser;
+               h = pwd->pw_dir;
        }
 
        /* Copy the home directory */
@@ -450,16 +451,19 @@
                continue;
 
        if (b == pend)
-               return NULL;
+               return GLOB_ABEND;
        
        /* Append the rest of the pattern */
        while (b < pend && (*b++ = *p++) != EOS)
                continue;
 
        if (b == pend)
-               return NULL;
+               return GLOB_ABEND;
 
-       return patbuf;
+       *qpatnext = patbuf;
+       return 0;
+nouser:
+       return (pglob->gl_flags & GLOB_TILDE_CHECK) ?  GLOB_NOMATCH : 0;
 }
        
 
@@ -481,9 +485,9 @@
        _DIAGASSERT(pattern != NULL);
        _DIAGASSERT(pglob != NULL);
 
-       if ((qpatnext = globtilde(pattern, patbuf, sizeof(patbuf),
-           pglob)) == NULL)
-               return GLOB_ABEND;
+       if ((error = globtilde(&qpatnext, pattern, patbuf, sizeof(patbuf),
+           pglob)) != 0)
+               return error;
        oldpathc = pglob->gl_pathc;
        bufnext = patbuf;
 



Home | Main Index | Thread Index | Old Index