From 395de250d9d9762b8ac1ce98b297d60d0b5bd643 Mon Sep 17 00:00:00 2001 From: Matthieu Moy Date: Tue, 17 Nov 2009 18:24:25 +0100 Subject: Expand ~ and ~user in core.excludesfile, commit.template These config variables are parsed to substitute ~ and ~user with getpw entries. user_path() refactored into new function expand_user_path(), to allow dynamically allocating the return buffer. Original patch by Karl Chen, modified by Matthieu Moy, and further amended by Junio C Hamano. Signed-off-by: Karl Chen Signed-off-by: Matthieu Moy Signed-off-by: Junio C Hamano --- path.c | 80 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 46 insertions(+), 34 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index 047fdb0a1f..2470f78d39 100644 --- a/path.c +++ b/path.c @@ -11,6 +11,7 @@ * which is what it's designed for. */ #include "cache.h" +#include "strbuf.h" static char bad_path[] = "/bad-path/"; @@ -207,43 +208,44 @@ int validate_headref(const char *path) return -1; } -static char *user_path(char *buf, char *path, int sz) +static struct passwd *getpw_str(const char *username, size_t len) { struct passwd *pw; - char *slash; - int len, baselen; + char *username_z = xmalloc(len + 1); + memcpy(username_z, username, len); + username_z[len] = '\0'; + pw = getpwnam(username_z); + free(username_z); + return pw; +} - if (!path || path[0] != '~') - return NULL; - path++; - slash = strchr(path, '/'); - if (path[0] == '/' || !path[0]) { - pw = getpwuid(getuid()); - } - else { - if (slash) { - *slash = 0; - pw = getpwnam(path); - *slash = '/'; - } - else - pw = getpwnam(path); - } - if (!pw || !pw->pw_dir || sz <= strlen(pw->pw_dir)) - return NULL; - baselen = strlen(pw->pw_dir); - memcpy(buf, pw->pw_dir, baselen); - while ((1 < baselen) && (buf[baselen-1] == '/')) { - buf[baselen-1] = 0; - baselen--; - } - if (slash && slash[1]) { - len = strlen(slash); - if (sz <= baselen + len) - return NULL; - memcpy(buf + baselen, slash, len + 1); +/* + * Return a string with ~ and ~user expanded via getpw*. If buf != NULL, + * then it is a newly allocated string. Returns NULL on getpw failure or + * if path is NULL. + */ +char *expand_user_path(const char *path) +{ + struct strbuf user_path = STRBUF_INIT; + const char *first_slash = strchrnul(path, '/'); + const char *to_copy = path; + + if (path == NULL) + goto return_null; + if (path[0] == '~') { + const char *username = path + 1; + size_t username_len = first_slash - username; + struct passwd *pw = getpw_str(username, username_len); + if (!pw) + goto return_null; + strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir)); + to_copy = first_slash; } - return buf; + strbuf_add(&user_path, to_copy, strlen(to_copy)); + return strbuf_detach(&user_path, NULL); +return_null: + strbuf_release(&user_path); + return NULL; } /* @@ -291,8 +293,18 @@ char *enter_repo(char *path, int strict) if (PATH_MAX <= len) return NULL; if (path[0] == '~') { - if (!user_path(used_path, path, PATH_MAX)) + char *newpath = expand_user_path(path); + if (!newpath || (PATH_MAX - 10 < strlen(newpath))) { + free(newpath); return NULL; + } + /* + * Copy back into the static buffer. A pity + * since newpath was not bounded, but other + * branches of the if are limited by PATH_MAX + * anyway. + */ + strcpy(used_path, newpath); free(newpath); strcpy(validated_path, path); path = used_path; } -- cgit v1.2.1 From df2a79f4225b7216dce010ebfe41f8ad0de34b24 Mon Sep 17 00:00:00 2001 From: Matthieu Moy Date: Thu, 19 Nov 2009 16:21:15 +0100 Subject: expand_user_path: expand ~ to $HOME, not to the actual homedir. In 395de250d (Expand ~ and ~user in core.excludesfile, commit.template), we introduced the mechanism. But expanding ~ using getpw is not what people overriding $HOME would usually expect. In particular, git looks for the user's .gitconfig using $HOME, so it's better to be consistent. Signed-off-by: Matthieu Moy Signed-off-by: Junio C Hamano --- path.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index 2470f78d39..00d0633295 100644 --- a/path.c +++ b/path.c @@ -235,10 +235,15 @@ char *expand_user_path(const char *path) if (path[0] == '~') { const char *username = path + 1; size_t username_len = first_slash - username; - struct passwd *pw = getpw_str(username, username_len); - if (!pw) - goto return_null; - strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir)); + if (username_len == 0) { + const char *home = getenv("HOME"); + strbuf_add(&user_path, home, strlen(home)); + } else { + struct passwd *pw = getpw_str(username, username_len); + if (!pw) + goto return_null; + strbuf_add(&user_path, pw->pw_dir, strlen(pw->pw_dir)); + } to_copy = first_slash; } strbuf_add(&user_path, to_copy, strlen(to_copy)); -- cgit v1.2.1