Add GLOB_TILDE_CHECK (from GNU)

This commit is contained in:
christos 2019-05-29 01:21:33 +00:00
parent 0924c525b1
commit 2f3bda949c
3 changed files with 60 additions and 45 deletions

View File

@ -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,13 +70,13 @@ 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_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. */
@ -84,16 +84,17 @@ typedef struct {
#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_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 */

View File

@ -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 included in
.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 links.
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

View File

@ -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 *, const Char *, glob_t *,
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 *pattern, glob_t *pglob, int *rv,
/*
* 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 *patbuf, size_t patsize, glob_t *pglob)
_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 *patbuf, size_t patsize, glob_t *pglob)
continue;
if (d == (char *)(void *)pend)
return NULL;
return GLOB_ABEND;
*d = EOS;
d = (char *)(void *)patbuf;
@ -425,8 +428,7 @@ globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
&pwd) != 0 || pwd == NULL)
#endif
return pattern;
else
goto nouser;
h = pwd->pw_dir;
}
}
@ -440,8 +442,7 @@ globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
if (getpwnam_r(d, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 ||
pwd == NULL)
#endif
return pattern;
else
goto nouser;
h = pwd->pw_dir;
}
@ -450,16 +451,19 @@ globtilde(const Char *pattern, Char *patbuf, size_t patsize, glob_t *pglob)
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, struct glob_limit *limit)
_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;