From 068d307a711bbcefc6868ff97fdf931269d13b44 Mon Sep 17 00:00:00 2001 From: Warren Dukes <warren.dukes@gmail.com> Date: Tue, 24 Feb 2004 21:20:16 +0000 Subject: [PATCH] make ~ work for paths git-svn-id: https://svn.musicpd.org/mpd/trunk@37 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/conf.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/conf.c b/src/conf.c index 656771cb0..889a0086b 100644 --- a/src/conf.c +++ b/src/conf.c @@ -29,6 +29,9 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> +#include <sys/types.h> +#include <pwd.h> #define MAX_STRING_SIZE MAXPATHLEN+80 @@ -206,11 +209,54 @@ char ** readConf(char * file) { for(i=0;i<CONF_NUMBER_OF_PATHS;i++) { if(conf_params[conf_absolutePaths[i]] && - conf_params[conf_absolutePaths[i]][0]!='/') { + conf_params[conf_absolutePaths[i]][0]!='/' && + conf_params[conf_absolutePaths[i]][0]!='~') + { ERROR("\"%s\" is not an absolute path\n", conf_params[conf_absolutePaths[i]]); exit(-1); } + else if(conf_params[conf_absolutePaths[i]] && + conf_params[conf_absolutePaths[i]][0]=='~') + { + struct passwd * pwd = NULL; + char * path; + int pos = 1; + if(conf_params[conf_absolutePaths[i]][1]=='/') { + uid_t uid = geteuid(); + if((pwd = getpwuid(uid)) == NULL) { + ERROR("problems getting passwd entry " + "for current user\n"); + exit(-1); + } + } + else { + int foundSlash = 0; + char * ch = &( + conf_params[conf_absolutePaths[i]][1]); + for(;*ch!='\0' && *ch!='/';ch++); + if(*ch=='/') foundSlash = 1; + * ch = '\0'; + pos+= ch- + &(conf_params[ + conf_absolutePaths[i]][1]); + if((pwd = getpwnam(&(conf_params[ + conf_absolutePaths[i]][1]))) == NULL) + { + ERROR("user \"%s\" not found\n", + &(conf_params[ + conf_absolutePaths[i]][1])); + exit(-1); + } + if(foundSlash) *ch = '/'; + } + path = malloc(strlen(pwd->pw_dir)+strlen( + &(conf_params[conf_absolutePaths[i]][pos]))); + strcpy(path,pwd->pw_dir); + strcat(path,&(conf_params[conf_absolutePaths[i]][pos])); + free(conf_params[conf_absolutePaths[i]]); + conf_params[conf_absolutePaths[i]] = path; + } } return conf_params;