diff options
author | Chris Robinson <[email protected]> | 2016-06-04 10:45:44 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2016-06-04 10:45:44 -0700 |
commit | 0477d6159974837ee01bbc7c74a65a3be5794cdd (patch) | |
tree | d69c5e5ad3133f05c5399b70c3cac87373cf59f5 /Alc/helpers.c | |
parent | b2041a5ddfec78bbde1c63c8674614455ee31f93 (diff) |
Look in the executable's dir for another config file
On Windows it'll look for alsoft.ini, and elsewhere is alsoft.conf. This
applies after the user-local settings and before ALSOFT_CONF.
Diffstat (limited to 'Alc/helpers.c')
-rw-r--r-- | Alc/helpers.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/Alc/helpers.c b/Alc/helpers.c index e1cd6be1..bd345b6a 100644 --- a/Alc/helpers.c +++ b/Alc/helpers.c @@ -321,6 +321,57 @@ static int StringSortCompare(const void *str1, const void *str2) #ifdef _WIN32 +static WCHAR *strrchrW(WCHAR *str, WCHAR ch) +{ + WCHAR *ret = NULL; + while(*str) + { + if(*str == ch) + ret = str; + ++str; + } + return ret; +} + +al_string GetProcPath(void) +{ + al_string ret = AL_STRING_INIT_STATIC(); + WCHAR *pathname, *sep; + DWORD pathlen; + DWORD len; + + pathlen = 256; + pathname = malloc(pathlen * sizeof(pathname[0])); + while(pathlen > 0 && (len=GetModuleFileNameW(NULL, pathname, pathlen)) == pathlen) + { + free(pathname); + pathlen <<= 1; + pathname = malloc(pathlen * sizeof(pathname[0])); + } + if(len == 0) + { + free(pathname); + ERR("Failed to get process name: error %lu\n", GetLastError()); + return ret; + } + + pathname[len] = 0; + if((sep = strrchrW(pathname, '\\'))) + { + WCHAR *sep2 = strrchrW(pathname, '/'); + if(sep2) *sep2 = 0; + else *sep = 0; + } + else if((sep = strrchrW(pathname, '/'))) + *sep = 0; + al_string_copy_wcstr(&ret, pathname); + free(pathname); + + TRACE("Got: %s\n", al_string_get_cstr(ret)); + return ret; +} + + static WCHAR *FromUTF8(const char *str) { WCHAR *out = NULL; @@ -549,6 +600,52 @@ vector_al_string SearchDataFiles(const char *ext, const char *subdir) #else +al_string GetProcPath(void) +{ + al_string ret = AL_STRING_INIT_STATIC(); + const char *fname; + char *pathname, *sep; + size_t pathlen; + ssize_t len; + + pathlen = 256; + pathname = malloc(pathlen); + + fname = "/proc/self/exe"; + len = readlink(fname, pathname, pathlen); + if(len == -1 && errno == ENOENT) + { + fname = "/proc/self/file"; + len = readlink(fname, pathname, pathlen); + } + + while(len > 0 && (size_t)len == pathlen) + { + free(pathname); + pathlen <<= 1; + pathname = malloc(pathlen); + len = readlink(fname, pathname, pathlen); + } + if(len <= 0) + { + free(pathname); + ERR("Failed to link %s: %s\n", fname, strerror(errno)); + return ret; + } + + pathname[len] = 0; + sep = strrchr(pathname, '/'); + if(sep) + al_string_copy_range(&ret, pathname, sep); + else + al_string_copy_cstr(&ret, pathname); + free(pathname); + + TRACE("Got: %s\n", al_string_get_cstr(ret)); + return ret; +} + + #ifdef HAVE_DLFCN_H void *LoadLib(const char *name) |