diff --git a/src/conf.c b/src/conf.c index 1addd7e82..d76d4ad9b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -397,55 +397,12 @@ ConfigParam *parseConfigFilePath(char *name, int force) if (!param) return NULL; - path = param->value; + path = parsePath(param->value); + if (!path) + FATAL("error parsing \"%s\" at line %i\n", name, param->line); - if (path[0] != '/' && path[0] != '~') { - FATAL("\"%s\" is not an absolute path at line %i\n", - param->value, param->line); - } - /* Parse ~ in path */ - else if (path[0] == '~') { - struct passwd *pwd = NULL; - char *newPath; - int pos = 1; - if (path[1] == '/' || path[1] == '\0') { - ConfigParam *userParam = getConfigParam(CONF_USER); - - if (userParam) { - pwd = getpwnam(userParam->value); - if (!pwd) { - FATAL("no such user %s at line %i\n", - userParam->value, - userParam->line); - } - } else { - uid_t uid = geteuid(); - if ((pwd = getpwuid(uid)) == NULL) { - FATAL("problems getting passwd entry " - "for current user\n"); - } - } - } else { - int foundSlash = 0; - char *ch = path + 1; - for (; *ch != '\0' && *ch != '/'; ch++) ; - if (*ch == '/') - foundSlash = 1; - *ch = '\0'; - pos += ch - path - 1; - if ((pwd = getpwnam(path + 1)) == NULL) { - FATAL("user \"%s\" not found at line %i\n", - path + 1, param->line); - } - if (foundSlash) - *ch = '/'; - } - newPath = xmalloc(strlen(pwd->pw_dir) + strlen(path + pos) + 1); - strcpy(newPath, pwd->pw_dir); - strcat(newPath, path + pos); - free(param->value); - param->value = newPath; - } + free(param->value); + param->value = path; return param; } diff --git a/src/utils.c b/src/utils.c index a6dd9d8ae..ea7edb4c5 100644 --- a/src/utils.c +++ b/src/utils.c @@ -18,6 +18,7 @@ #include "utils.h" #include "log.h" +#include "conf.h" #include #include @@ -28,6 +29,7 @@ #include #include #include +#include char *myFgets(char *buffer, int bufferSize, FILE * fp) { @@ -158,4 +160,60 @@ mpd_malloc void *xcalloc(size_t nmemb, size_t size) return ret; } +char *parsePath(char *path) +{ + ConfigParam *param; + struct passwd *passwd; + char *newPath; + char *c; + int foundSlash = 0; + int pos = 1; + if (path[0] != '/' && path[0] != '~') { + ERROR("\"%s\" is not an absolute path\n", path); + return NULL; + } else if (path[0] == '~') { + if (path[1] == '/' || path[1] == '\0') { + param = getConfigParam(CONF_USER); + if (param && param->value) { + passwd = getpwnam(param->value); + if (!passwd) { + ERROR("no such user %s\n", + param->value); + return NULL; + } + } else { + passwd = getpwuid(geteuid()); + if (!passwd) { + ERROR("problems getting passwd entry " + "for current user\n"); + return NULL; + } + } + } else { + for (c = path + 1; *c != '\0' && *c != '/'; c++); + if (*c == '/') { + foundSlash = 1; + *c = '\0'; + } + pos = c - path; + + passwd = getpwnam(path + 1); + if (!passwd) { + ERROR("user \"%s\" not found\n", path + 1); + return NULL; + } + + if (foundSlash) + *c = '/'; + } + + newPath = xmalloc(strlen(passwd->pw_dir) + strlen(path + pos) + 1); + strcpy(newPath, passwd->pw_dir); + strcat(newPath, path + pos); + } else { + newPath = xstrdup(path); + } + + return newPath; +} diff --git a/src/utils.h b/src/utils.h index 2f911499b..09a9bc4e8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -82,4 +82,6 @@ mpd_malloc void *xrealloc(void *ptr, size_t size); mpd_malloc void *xcalloc(size_t nmemb, size_t size); +char *parsePath(char *path); + #endif