@@ -6,6 +6,7 @@ | |||||
#include <inttypes.h> | #include <inttypes.h> | ||||
#include <limits.h> | #include <limits.h> | ||||
#include <math.h> | #include <math.h> | ||||
#include <pwd.h> | |||||
#include <stdarg.h> | #include <stdarg.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
@@ -180,3 +181,21 @@ read_fully(int fd, void *buf, size_t len) | |||||
} | } | ||||
return 0; | 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; | |||||
} |
@@ -25,4 +25,6 @@ long rand_between(long min, long max); | |||||
int read_fully(int fd, void *buf, size_t len); | int read_fully(int fd, void *buf, size_t len); | ||||
int write_fully(int fd, const void *buf, size_t len); | int write_fully(int fd, const void *buf, size_t len); | ||||
char *find_btpd_dir(void); | |||||
#endif | #endif |