aboutsummaryrefslogtreecommitdiff
path: root/src/helper
diff options
context:
space:
mode:
Diffstat (limited to 'src/helper')
-rw-r--r--src/helper/configuration.c44
-rw-r--r--src/helper/configuration.h1
2 files changed, 45 insertions, 0 deletions
diff --git a/src/helper/configuration.c b/src/helper/configuration.c
index 49c1c08f..bfb73fd7 100644
--- a/src/helper/configuration.c
+++ b/src/helper/configuration.c
@@ -128,3 +128,47 @@ int parse_config_file(struct command_context *cmd_ctx)
return ERROR_OK;
}
+
+#ifndef _WIN32
+#include <pwd.h>
+#endif
+
+char *get_home_dir(const char *append_path)
+{
+ char *home = getenv("HOME");
+
+ if (home == NULL) {
+
+#ifdef _WIN32
+ home = getenv("USERPROFILE");
+
+ if (home == NULL) {
+
+ char homepath[MAX_PATH];
+ char *drive = getenv("HOMEDRIVE");
+ char *path = getenv("HOMEPATH");
+ if (drive && path) {
+ snprintf(homepath, MAX_PATH, "%s/%s", drive, path);
+ home = homepath;
+ }
+ }
+#else
+ struct passwd *pwd = getpwuid(getuid());
+ if (pwd)
+ home = pwd->pw_dir;
+
+#endif
+ }
+
+ if (home == NULL)
+ return home;
+
+ char *home_path;
+
+ if (append_path)
+ home_path = alloc_printf("%s/%s", home, append_path);
+ else
+ home_path = alloc_printf("%s", home);
+
+ return home_path;
+}
diff --git a/src/helper/configuration.h b/src/helper/configuration.h
index b329da76..749f007d 100644
--- a/src/helper/configuration.h
+++ b/src/helper/configuration.h
@@ -40,5 +40,6 @@ int configuration_output_handler(struct command_context *cmd_ctx,
FILE *open_file_from_path(const char *file, const char *mode);
char *find_file(const char *name);
+char *get_home_dir(const char *append_path);
#endif /* CONFIGURATION_H */