about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--misc/subr.c19
-rw-r--r--misc/subr.h2
2 files changed, 21 insertions, 0 deletions
diff --git a/misc/subr.c b/misc/subr.c
index 30b4f3e..ffa5d46 100644
--- a/misc/subr.c
+++ b/misc/subr.c
@@ -6,6 +6,7 @@
 #include <inttypes.h>
 #include <limits.h>
 #include <math.h>
+#include <pwd.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -180,3 +181,21 @@ read_fully(int fd, void *buf, size_t len)
     }
     return 0;
 }
+
+char *
+find_btpd_dir(void)
+{
+    char *res = getenv("BTPD_HOME");
+    if (res != NULL)
+        return strdup(res);
+    char *home = getenv("HOME");
+    if (home == NULL) {
+        struct passwd *pwent = getpwuid(getuid());
+        endpwent();
+        if (pwent != NULL)
+            home = pwent->pw_dir;
+    }
+    if (home != NULL)
+        asprintf(&res, "%s/.btpd", home);
+    return res;
+}
diff --git a/misc/subr.h b/misc/subr.h
index 1b9c90d..9260ed9 100644
--- a/misc/subr.h
+++ b/misc/subr.h
@@ -25,4 +25,6 @@ long rand_between(long min, long max);
 int read_fully(int fd, void *buf, size_t len);
 int write_fully(int fd, const void *buf, size_t len);
 
+char *find_btpd_dir(void);
+
 #endif