mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] glob: implement GLOB_TILDE
@ 2019-07-23 18:33 Ismael Luceno
  2019-07-23 20:07 ` Rich Felker
  0 siblings, 1 reply; 5+ messages in thread
From: Ismael Luceno @ 2019-07-23 18:33 UTC (permalink / raw)
  To: musl; +Cc: Ismael Luceno

Signed-off-by: Ismael Luceno <ismael@iodev.co.uk>
---
 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 <stdlib.h>
 #include <errno.h>
 #include <stddef.h>
+#include <unistd.h>
+#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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-07-24 17:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-23 18:33 [PATCH] glob: implement GLOB_TILDE Ismael Luceno
2019-07-23 20:07 ` Rich Felker
2019-07-24  7:15   ` Ismael Luceno
2019-07-24 13:00     ` Rich Felker
2019-07-24 17:25       ` Ismael Luceno

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).