Fixing the last patch to the surf style.
[surf.git] / surf.c
diff --git a/surf.c b/surf.c
index 3d6cc11..9851bba 100644 (file)
--- a/surf.c
+++ b/surf.c
@@ -144,6 +144,8 @@ static void setup(void);
 static void sigchld(int unused);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
+static const char *getuserhomedir(const char *user);
+static const char *getcurrentuserhomedir(void);
 static Client *newclient(Client *c);
 static void loaduri(Client *c, const Arg *a);
 static const char *geturi(Client *c);
@@ -350,29 +352,59 @@ buildfile(const char *path)
        return fpath;
 }
 
+static const char*
+getuserhomedir(const char *user)
+{
+       struct passwd *pw = getpwnam(user);
+
+       if (!pw)
+               die("Can't get user %s login information.\n", user);
+
+       return pw->pw_dir;
+}
+
+static const char*
+getcurrentuserhomedir(void)
+{
+       const char *homedir;
+       const char *user;
+       struct passwd *pw;
+
+       homedir = getenv("HOME");
+       if (homedir)
+               return homedir;
+
+       user = getenv("USER");
+       if (user)
+               return getuserhomedir(user);
+
+       pw = getpwuid(getuid());
+       if (!pw)
+               die("Can't get current user home directory\n");
+
+       return pw->pw_dir;
+}
+
 char *
 buildpath(const char *path)
 {
-       struct passwd *pw;
        char *apath, *name, *p, *fpath;
+       const char *homedir;
 
        if (path[0] == '~') {
                if (path[1] == '/' || path[1] == '\0') {
                        p = (char *)&path[1];
-                       pw = getpwuid(getuid());
+                       homedir = getcurrentuserhomedir();
                } else {
                        if ((p = strchr(path, '/')))
                                name = g_strndup(&path[1], --p - path);
                        else
                                name = g_strdup(&path[1]);
 
-                       if (!(pw = getpwnam(name))) {
-                               die("Can't get user %s home directory: %s.\n",
-                                   name, path);
-                       }
+                       homedir = getuserhomedir(name);
                        g_free(name);
                }
-               apath = g_build_filename(pw->pw_dir, p, NULL);
+               apath = g_build_filename(homedir, p, NULL);
        } else {
                apath = g_strdup(path);
        }