From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/14436 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Ismael Luceno Newsgroups: gmane.linux.lib.musl.general Subject: [PATCH] glob: implement GLOB_TILDE Date: Tue, 23 Jul 2019 20:33:54 +0200 Message-ID: <20190723183354.31633-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="39365"; mail-complaints-to="usenet@blaine.gmane.org" Cc: Ismael Luceno To: musl@lists.openwall.com Original-X-From: musl-return-14452-gllmg-musl=m.gmane.org@lists.openwall.com Tue Jul 23 20:34:15 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 1hpzc6-000A8F-Mu for gllmg-musl@m.gmane.org; Tue, 23 Jul 2019 20:34:14 +0200 Original-Received: (qmail 30489 invoked by uid 550); 23 Jul 2019 18:34:11 -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 30457 invoked from network); 23 Jul 2019 18:34:11 -0000 X-Mailer: git-send-email 2.22.0 Xref: news.gmane.org gmane.linux.lib.musl.general:14436 Archived-At: Signed-off-by: Ismael Luceno --- include/glob.h | 1 + src/regex/glob.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/glob.h b/include/glob.h index 76f6c1c68a23..fc8106b20c5b 100644 --- a/include/glob.h +++ b/include/glob.h @@ -30,6 +30,7 @@ void globfree(glob_t *); #define GLOB_APPEND 0x20 #define GLOB_NOESCAPE 0x40 #define GLOB_PERIOD 0x80 +#define GLOB_TILDE 0x100 #define GLOB_NOSPACE 1 #define GLOB_ABORTED 2 diff --git a/src/regex/glob.c b/src/regex/glob.c index aa1c6a4482ee..77b81f707420 100644 --- a/src/regex/glob.c +++ b/src/regex/glob.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include "../passwd/pwf.h" struct match { @@ -35,6 +37,30 @@ static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (* /* If GLOB_MARK is unused, we don't care about type. */ if (!type && !(flags & GLOB_MARK)) type = DT_REG; + if ((flags & GLOB_TILDE) && *pat == '~') { + struct passwd pw, *res; + char *buf = NULL; + size_t len = 0; + char *name_end = strchr(++pat, '/'); + if (name_end) + *name_end = 0; + uid_t uid = *pat ? 0 : getuid(); + int err = __getpw_a(pat, uid, &pw, &buf, &len, &res); + if (!err && res) { + while (pos < PATH_MAX - 1 && *pw.pw_dir) + buf[pos++] = *pw.pw_dir++; + } + free(buf); + if (err) + return GLOB_NOSPACE; + if (!res) + return 0; + if (name_end) { + pat = name_end; + *pat = '/'; + } + } + /* Special-case the remaining pattern being all slashes, in * which case we can use caller-passed type if it's a dir. */ if (*pat && type!=DT_DIR) type = 0; -- 2.22.0