diff options
author | Max Kellermann <max@duempel.org> | 2008-12-29 17:42:46 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-12-29 17:42:46 +0100 |
commit | db849d1eede5a19a8c47deb888772ae10cf830f6 (patch) | |
tree | 54b71ffa45a8e59a3f005d6612cb5f4d9d8ee5e6 /src/utils.c | |
parent | 3c9992aead84237af76e1514a826a56daa8465bc (diff) | |
download | mpd-db849d1eede5a19a8c47deb888772ae10cf830f6.tar.gz mpd-db849d1eede5a19a8c47deb888772ae10cf830f6.tar.xz mpd-db849d1eede5a19a8c47deb888772ae10cf830f6.zip |
utils: make variables more local in parsePath()
Declare variables where they are really used.
Diffstat (limited to 'src/utils.c')
-rw-r--r-- | src/utils.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/utils.c b/src/utils.c index a3dca2f08..530de5a78 100644 --- a/src/utils.c +++ b/src/utils.c @@ -114,38 +114,43 @@ G_GNUC_MALLOC void *xcalloc(size_t nmemb, size_t size) char *parsePath(char *path) { - ConfigParam *param; - struct passwd *passwd; - char *newPath; - char *c; - int foundSlash = 0; - int pos = 1; - if (path[0] != '/' && path[0] != '~') { g_warning("\"%s\" is not an absolute path", path); return NULL; } else if (path[0] == '~') { + size_t pos = 1; + const char *home; + char *newPath; + if (path[1] == '/' || path[1] == '\0') { - param = getConfigParam(CONF_USER); + ConfigParam *param = getConfigParam(CONF_USER); if (param && param->value) { - passwd = getpwnam(param->value); + struct passwd *passwd = getpwnam(param->value); if (!passwd) { g_warning("no such user %s", param->value); return NULL; } + + home = passwd->pw_dir; } else { - passwd = getpwuid(geteuid()); + struct passwd *passwd = getpwuid(geteuid()); if (!passwd) { g_warning("problems getting passwd " "entry for current user"); return NULL; } + + home = passwd->pw_dir; } } else { + bool foundSlash = false; + struct passwd *passwd; + char *c; + for (c = path + 1; *c != '\0' && *c != '/'; c++); if (*c == '/') { - foundSlash = 1; + foundSlash = true; *c = '\0'; } pos = c - path; @@ -158,16 +163,17 @@ char *parsePath(char *path) if (foundSlash) *c = '/'; + + home = passwd->pw_dir; } - newPath = xmalloc(strlen(passwd->pw_dir) + strlen(path + pos) + 1); - strcpy(newPath, passwd->pw_dir); + newPath = xmalloc(strlen(home) + strlen(path + pos) + 1); + strcpy(newPath, home); strcat(newPath, path + pos); + return newPath; } else { - newPath = xstrdup(path); + return xstrdup(path); } - - return newPath; } int set_nonblocking(int fd) |