+static const char*
+get_user_homedir(const char *user) {
+ struct passwd *pw = getpwnam(user);
+ if (!pw) {
+ die("Can't get user `%s' home directory.\n", user);
+ }
+ return pw->pw_dir;
+}
+
+static const char*
+get_current_user_homedir() {
+ const char *homedir;
+ const char *user;
+ struct passwd *pw;
+
+ homedir = getenv("HOME");
+ if (homedir) {
+ return homedir;
+ }
+
+ user = getenv("USER");
+ if (user) {
+ return get_user_homedir(user);
+ }
+
+ pw = getpwuid(getuid());
+ if (!pw) {
+ die("Can't get current user home directory\n");
+ }
+ return pw->pw_dir;
+}
+