aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2011-09-09 22:17:35 +0200
committerMax Kellermann <max@duempel.org>2011-09-09 22:55:57 +0200
commit61fc01e79e385bc903edf1fd0cac0e5843911d58 (patch)
tree3f51d966baa85fcf11b67dfb3378abfc388a51a6 /src/utils.c
parentcceaec1d744b2059cc4ca25324da3c9a44d72191 (diff)
downloadmpd-61fc01e79e385bc903edf1fd0cac0e5843911d58.tar.gz
mpd-61fc01e79e385bc903edf1fd0cac0e5843911d58.tar.xz
mpd-61fc01e79e385bc903edf1fd0cac0e5843911d58.zip
utils: pass a const string to parsePath()
Remove the slash hack, allocate memory for the user name.
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c26
1 files changed, 12 insertions, 14 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 <windows.h>
#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);