From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14457 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Ismael Luceno Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH v3] glob: implement GLOB_TILDE and GLOB_TILDE_CHECK Date: Thu, 25 Jul 2019 23:50:48 +0200 Message-ID: <20190725215048.3554-1-ismael@iodev.co.uk> Reply-To: musl@lists.openwall.com Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="159653"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Ismael Luceno To: musl@lists.openwall.com Original-X-From: musl-return-14473-gllmg-musl=m.gmane.org@lists.openwall.com Thu Jul 25 23:51:06 2019 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.89) (envelope-from ) id 1hqldh-000fNz-4M for gllmg-musl@m.gmane.org; Thu, 25 Jul 2019 23:51:05 +0200 Original-Received: (qmail 26162 invoked by uid 550); 25 Jul 2019 21:51:02 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 26129 invoked from network); 25 Jul 2019 21:51:01 -0000 X-Mailer: git-send-email 2.22.0 Xref: news.gmane.org gmane.linux.lib.musl.general:14457 Archived-At: Signed-off-by: Ismael Luceno --- Notes: Changes since v2: - Fixed GLOB_TILDE value and added GLOB_TILDE_CHECK. - Removed use of issetugid. - Switched to __strchrnul. - Mimick error handling in other implementations. - Fixed expansion of the path delimiter. include/glob.h | 3 +++ src/regex/glob.c | 42 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/include/glob.h b/include/glob.h index 76f6c1c68a23..4a562a206d52 100644 --- a/include/glob.h +++ b/include/glob.h @@ -31,6 +31,9 @@ void globfree(glob_t *); #define GLOB_NOESCAPE 0x40 #define GLOB_PERIOD 0x80 +#define GLOB_TILDE 0x1000 +#define GLOB_TILDE_CHECK 0x4000 + #define GLOB_NOSPACE 1 #define GLOB_ABORTED 2 #define GLOB_NOMATCH 3 diff --git a/src/regex/glob.c b/src/regex/glob.c index aa1c6a4482ee..58248675c203 100644 --- a/src/regex/glob.c +++ b/src/regex/glob.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include struct match { @@ -182,6 +184,39 @@ static int sort(const void *a, const void *b) return strcmp(*(const char **)a, *(const char **)b); } +static int expand_tilde(char **pat, char *buf, size_t *pos) +{ + char *p = *pat + 1; + size_t i = 0; + + char delim, *name_end = __strchrnul(p, '/'); + if ((delim = *name_end)) *name_end++ = 0; + *pat = name_end; + + char *home = *p ? NULL : getenv("HOME"); + if (!home) { + struct passwd pw, *res; + switch (*p ? getpwnam_r(p, &pw, buf, PATH_MAX, &res) + : getpwuid_r(getuid(), &pw, buf, PATH_MAX, &res)) { + case ENOMEM: + return GLOB_NOSPACE; + case 0: + if (!res) + default: + return GLOB_NOMATCH; + } + home = pw.pw_dir; + } + while (i < PATH_MAX - 2 && *home) + buf[i++] = *home++; + if (*home) + return GLOB_NOMATCH; + if ((buf[i] = delim)) + buf[++i] = 0; + *pos = i; + return 0; +} + int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g) { struct match head = { .next = NULL }, *tail = &head; @@ -202,7 +237,12 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i char *p = strdup(pat); if (!p) return GLOB_NOSPACE; buf[0] = 0; - error = do_glob(buf, 0, 0, p, flags, errfunc, &tail); + size_t pos = 0; + char *s = p; + if ((flags & (GLOB_TILDE | GLOB_TILDE_CHECK)) && *p == '~') + error = expand_tilde(&s, buf, &pos); + if (!error) + error = do_glob(buf, pos, 0, s, flags, errfunc, &tail); free(p); } -- 2.22.0