X-Git-Url: https://git.danieliu.xyz/?a=blobdiff_plain;f=surf.c;h=105b28e69b61305e384bcf9bd673e922fbcb336e;hb=cee8f0186ce3277add9b6a8ac07588be40f0f080;hp=938785991f1688f5f3f1244f8d8ec962dd32696a;hpb=7ca9421a69956c27b010cc9fc6162df7fa9699cf;p=surf.git diff --git a/surf.c b/surf.c index 9387859..105b28e 100644 --- a/surf.c +++ b/surf.c @@ -350,29 +350,58 @@ buildfile(const char *path) return fpath; } +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; +} + char * buildpath(const char *path) { - struct passwd *pw; char *apath, *name, *p, *fpath; if (path[0] == '~') { + const char *homedir; if (path[1] == '/' || path[1] == '\0') { p = (char *)&path[1]; - pw = getpwuid(getuid()); + homedir = get_current_user_homedir(); } 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 = get_user_homedir(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); } @@ -415,7 +444,10 @@ loaduri(Client *c, const Arg *a) if (g_strcmp0(uri, "") == 0) return; - if (g_strrstr(uri, "://") || g_str_has_prefix(uri, "about:")) { + if (g_str_has_prefix(uri, "http://") || + g_str_has_prefix(uri, "https://") || + g_str_has_prefix(uri, "file://") || + g_str_has_prefix(uri, "about:")) { url = g_strdup(uri); } else if (!stat(uri, &st) && (path = realpath(uri, NULL))) { url = g_strdup_printf("file://%s", path);