From 61fc01e79e385bc903edf1fd0cac0e5843911d58 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 9 Sep 2011 22:17:35 +0200 Subject: utils: pass a const string to parsePath() Remove the slash hack, allocate memory for the user name. --- src/utils.c | 26 ++++++++++++-------------- src/utils.h | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/utils.c b/src/utils.c index 5ed053246..f8c08a08e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -41,7 +41,8 @@ #include #endif -char *parsePath(char *path) +char * +parsePath(const char *path) { #ifndef WIN32 if (!g_path_is_absolute(path) && path[0] != '~') { @@ -71,27 +72,24 @@ char *parsePath(char *path) ++path; } else { - bool foundSlash = false; - struct passwd *passwd; - char *c; + ++path; - for (c = path + 1; *c != '\0' && *c != '/'; c++); - if (*c == '/') { - foundSlash = true; - *c = '\0'; - } + const char *slash = strchr(path, '/'); + char *user = slash != NULL + ? g_strndup(path, slash - path) + : g_strdup(path); - passwd = getpwnam(path + 1); + struct passwd *passwd = getpwnam(user); if (!passwd) { - g_warning("user \"%s\" not found", path + 1); + g_warning("user \"%s\" not found", user); + g_free(user); return NULL; } - if (foundSlash) - *c = '/'; + g_free(user); home = passwd->pw_dir; - path = c; + path = slash; } return g_strconcat(home, path, NULL); diff --git a/src/utils.h b/src/utils.h index 8200400af..df36d1da0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,6 +31,7 @@ } while (0) #endif /* !assert_static */ -char *parsePath(char *path); +char * +parsePath(const char *path); #endif -- cgit v1.2.3