diff options
author | Lubomir I. Ivanov <neolit123@gmail.com> | 2015-10-06 13:10:18 +0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-10-06 16:04:24 +0100 |
commit | e8a868aae2583fc3065b3ff150edb005cc32682b (patch) | |
tree | ca0112c54c88503f342987265a1338e20df3a3db /linux.c | |
parent | ed98d3bc603f8b503de76e4e70e304518f83499c (diff) | |
download | subsurface-e8a868aae2583fc3065b3ff150edb005cc32682b.tar.gz |
linux.c: update the default path retrieval
- added system_default_directory()
- both system_default_directory() and system_default_filename()
now use the helper system_default_path_append()
- less safer compared to windows.c, assuming the OS is stricter on which
environment variables exist!
[Dirk Hohndel: switched filename to be dynamically allocated to avoid
possible buffer overflow]
Signed-off-by: Lubomir I. Ivanov <neolit123@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'linux.c')
-rw-r--r-- | linux.c | 54 |
1 files changed, 42 insertions, 12 deletions
@@ -48,24 +48,54 @@ void subsurface_user_info(struct user_info *user) } } -const char *system_default_filename(void) +static const char *system_default_path_append(const char *append) { - const char *home, *user; - char *buffer; - int len; - - home = getenv("HOME"); + const char *home = getenv("HOME"); if (!home) home = "~"; - user = getenv("LOGNAME"); - if (!user) - user = "default"; - len = strlen(home) + strlen(user) + 17; - buffer = malloc(len); - snprintf(buffer, len, "%s/subsurface/%s.xml", home, user); + const char *path = "/subsurface"; + + int len = strlen(home) + strlen(path) + 1; + if (append) + len += strlen(append) + 1; + + char *buffer = (char *)malloc(len); + memset(buffer, 0, len); + strcat(buffer, home); + strcat(buffer, path); + if (append) { + strcat(buffer, "/"); + strcat(buffer, append); + } + return buffer; } +const char *system_default_directory(void) +{ + static const char *path = NULL; + if (!path) + path = system_default_path_append(NULL); + return path; +} + +const char *system_default_filename(void) +{ + static char *filename = NULL; + if (!filename) { + const char *user = getenv("LOGNAME"); + if (same_string(user, "")) + user = "username"; + filename = malloc(strlen(user) + 5); + strcat(filename, user); + strcat(filename, ".xml"); + } + static const char *path = NULL; + if (!path) + path = system_default_path_append(filename); + return path; +} + int enumerate_devices(device_callback_t callback, void *userdata, int dc_type) { int index = -1, entries = 0; |