diff --git a/cwFileSys.cpp b/cwFileSys.cpp index 4fc4ea3..dffe1a1 100644 --- a/cwFileSys.cpp +++ b/cwFileSys.cpp @@ -9,6 +9,7 @@ #include // basename() dirname() #include #include // opendir()/readdir() +#include #endif namespace cw @@ -203,6 +204,66 @@ char* cw::filesys::makeFn( const char* dir, const char* fn, const char* ext, .. return fnOut; } +char* cw::filesys::expandPath( const char* dir ) +{ + rc_t rc = kOkRC; + int sysRC = 0; + int flags = WRDE_NOCMD; + char* newDir = nullptr; + wordexp_t res; + + memset(&res,0,sizeof(res)); + + if((sysRC = wordexp(dir,&res,flags)) != 0) + { + switch(sysRC) + { + case WRDE_BADCHAR: + rc = cwLogError(kOpFailRC,"Bad character."); + break; + + case WRDE_BADVAL: + rc = cwLogError(kOpFailRC,"Bad value."); + break; + + case WRDE_CMDSUB: + rc = cwLogError(kOpFailRC,"Command substitution forbidden."); + break; + + case WRDE_NOSPACE: + rc = cwLogError(kOpFailRC,"Mem. alloc failed."); + break; + + case WRDE_SYNTAX: + rc = cwLogError(kOpFailRC,"Syntax error.."); + break; + } + + goto errLabel; + } + + if( res.we_wordc > 1 ) + { + rc = cwLogError(kOpFailRC,"Unexpected word expansion count: %i.", res.we_wordc ); + goto errLabel; + } + + if( res.we_wordc == 1 ) + newDir = mem::duplStr(res.we_wordv[0]); + else + newDir = mem::duplStr(dir); + + errLabel: + if( rc != kOkRC ) + rc = cwLogError(rc,"Path expansion failed."); + + wordfree(&res); + + + return newDir; + +} + cw::filesys::pathPart_t* cw::filesys::pathParts( const char* pathStr ) diff --git a/cwFileSys.h b/cwFileSys.h index 3ce3548..3f57792 100644 --- a/cwFileSys.h +++ b/cwFileSys.h @@ -20,11 +20,14 @@ namespace cw // 'dirPrefixStr' and the file name. // Terminate var arg's directory list with a nullptr. // - // The returned string must be released by a call to memRelease() or memFree(). + // The returned string must be released by a call to mem::release() or mem::free(). char* vMakeFn( const char* dir, const char* fn, const char* ext, va_list vl ); char* makeFn( const char* dir, const char* fn, const char* ext, ... ); + char* expandPath( const char* dir ); + + // Parse a path into its parts: // // Return record used by pathParts()