From df3af7d4f121be6264c0ce307f4a75e844d7282c Mon Sep 17 00:00:00 2001 From: Warren Dukes Date: Thu, 15 Apr 2004 03:26:15 +0000 Subject: clean up a little bit main() code git-svn-id: https://svn.musicpd.org/mpd/trunk@771 09075e82-0dd4-0310-85a5-a0d7c8717e4f --- src/path.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 3 deletions(-) (limited to 'src/path.c') diff --git a/src/path.c b/src/path.c index 7226cbdec..6eecf084e 100644 --- a/src/path.c +++ b/src/path.c @@ -24,6 +24,9 @@ #include #include +#include +#include +#include #ifdef HAVE_LOCALE #ifdef HAVE_LANGINFO @@ -32,8 +35,8 @@ #endif #endif -char musicDir[MAXPATHLEN+1]; -char playlistDir[MAXPATHLEN+1]; +char * musicDir; +char * playlistDir; char * fsCharset = NULL; @@ -106,9 +109,30 @@ char * getFsCharset() { return fsCharset; } -void initPaths() { +void initPaths(char * playlistDirArg, char * musicDirArg) { char * charset = NULL; char * originalLocale; + struct stat st; + + playlistDir = prependCwdToPathDup(playlistDirArg); + if((stat(playlistDir,&st))<0) { + ERROR("problem stat'ing \"%s\"\n",playlistDirArg); + exit(EXIT_FAILURE); + } + if(!S_ISDIR(st.st_mode)) { + ERROR("\"%s\" is not a directory\n",playlistDirArg); + exit(EXIT_FAILURE); + } + + musicDir = prependCwdToPathDup(musicDirArg); + if((stat(musicDir,&st))<0) { + ERROR("problem stat'ing \"%s\"\n",musicDirArg); + exit(EXIT_FAILURE); + } + if(!S_ISDIR(st.st_mode)) { + ERROR("\"%s\" is not a directory\n",musicDirArg); + exit(EXIT_FAILURE); + } if(getConf()[CONF_FS_CHARSET]) { charset = strdup(getConf()[CONF_FS_CHARSET]); @@ -241,4 +265,35 @@ char * sanitizePathDup(char * path) { return realloc(ret,len+1); } + +char * prependCwdToPathDup(char * path) { + int len = MAXPATHLEN+1; + char * ret = malloc(len); + + memset(ret,0,len); + + len = 0; + + if(path[0]=='/') { + strncpy(ret,path,MAXPATHLEN); + len = strlen(ret); + } + else { + getcwd(ret,MAXPATHLEN); + len = strlen(ret); + if(ret[len-1]!='/') { + strncat(ret,"/",MAXPATHLEN-len); + len = strlen(path); + } + strncat(ret,path,MAXPATHLEN-len); + len = strlen(ret); + } + if(ret[len-1]!='/') { + strncat(ret,"/",MAXPATHLEN-len); + len = strlen(path); + } + + return realloc(ret,len+1); +} + /* vim:set shiftwidth=4 tabstop=8 expandtab: */ -- cgit v1.2.3