Module Name: src
Committed By: christos
Date: Wed May 29 01:21:33 UTC 2019
Modified Files:
src/include: glob.h
src/lib/libc/gen: glob.3 glob.c
Log Message:
Add GLOB_TILDE_CHECK (from GNU)
To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/include/glob.h
cvs rdiff -u -r1.43 -r1.44 src/lib/libc/gen/glob.3
cvs rdiff -u -r1.38 -r1.39 src/lib/libc/gen/glob.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/include/glob.h
diff -u src/include/glob.h:1.26 src/include/glob.h:1.27
--- src/include/glob.h:1.26 Mon Sep 6 10:38:56 2010
+++ src/include/glob.h Tue May 28 21:21:33 2019
@@ -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 @@ typedef struct {
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_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_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. */
#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
Index: src/lib/libc/gen/glob.3
diff -u src/lib/libc/gen/glob.3:1.43 src/lib/libc/gen/glob.3:1.44
--- src/lib/libc/gen/glob.3:1.43 Mon Jul 3 17:32:49 2017
+++ src/lib/libc/gen/glob.3 Tue May 28 21:21:33 2019
@@ -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 @@ The following values may also be include
.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 @@ characters will also follow symbolic lin
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
Index: src/lib/libc/gen/glob.c
diff -u src/lib/libc/gen/glob.c:1.38 src/lib/libc/gen/glob.c:1.39
--- src/lib/libc/gen/glob.c:1.38 Mon May 8 10:42:16 2017
+++ src/lib/libc/gen/glob.c Tue May 28 21:21:33 2019
@@ -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 glob2(Char *, Char *, Char *
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 @@ globexp2(const Char *ptr, const Char *pa
/*
* 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 @@ globtilde(const Char *pattern, Char *pat
_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 @@ globtilde(const Char *pattern, Char *pat
continue;
if (d == (char *)(void *)pend)
- return NULL;
+ return GLOB_ABEND;
*d = EOS;
d = (char *)(void *)patbuf;
@@ -425,9 +428,8 @@ globtilde(const Char *pattern, Char *pat
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 @@ globtilde(const Char *pattern, Char *pat
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 @@ globtilde(const Char *pattern, Char *pat
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 @@ glob0(const Char *pattern, glob_t *pglob
_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;