about summary refs log tree commit diff
diff options
context:
space:
mode:
authornzl <uruabi@gmail.com>2018-03-15 03:46:52 +0800
committerQuentin Rameau <quinq@fifth.space>2018-10-08 11:38:03 +0200
commitbefe481a9b970cf2bc90ca671e1df1d1082ac41e (patch)
tree56a0edee2eaa1e1d5b675aa0b39cba367ae3e31b
parentFixed wording in FAQ.md (diff)
downloadsurf-befe481a9b970cf2bc90ca671e1df1d1082ac41e.tar.gz
surf-befe481a9b970cf2bc90ca671e1df1d1082ac41e.tar.bz2
surf-befe481a9b970cf2bc90ca671e1df1d1082ac41e.zip
Allow tilde expansion in loaduri
also fixed a bug that ~foo/ was expanded to /home/fo/o/
-rw-r--r--surf.c63
1 files changed, 40 insertions, 23 deletions
diff --git a/surf.c b/surf.c
index a5cc5a7..ae2bc81 100644
--- a/surf.c
+++ b/surf.c
@@ -146,6 +146,7 @@ static void sigchld(int unused);
 static void sighup(int unused);
 static char *buildfile(const char *path);
 static char *buildpath(const char *path);
+static char *untildepath(const char *path);
 static const char *getuserhomedir(const char *user);
 static const char *getcurrentuserhomedir(void);
 static Client *newclient(Client *c);
@@ -470,26 +471,12 @@ getcurrentuserhomedir(void)
 char *
 buildpath(const char *path)
 {
-	char *apath, *name, *p, *fpath;
-	const char *homedir;
-
-	if (path[0] == '~') {
-		if (path[1] == '/' || path[1] == '\0') {
-			p = (char *)&path[1];
-			homedir = getcurrentuserhomedir();
-		} else {
-			if ((p = strchr(path, '/')))
-				name = g_strndup(&path[1], --p - path);
-			else
-				name = g_strdup(&path[1]);
+	char *apath, *fpath;
 
-			homedir = getuserhomedir(name);
-			g_free(name);
-		}
-		apath = g_build_filename(homedir, p, NULL);
-	} else {
+	if (path[0] == '~')
+		apath = untildepath(path);
+	else
 		apath = g_strdup(path);
-	}
 
 	/* creating directory */
 	if (g_mkdir_with_parents(apath, 0700) < 0)
@@ -501,6 +488,28 @@ buildpath(const char *path)
 	return fpath;
 }
 
+char *
+untildepath(const char *path)
+{
+       char *apath, *name, *p;
+       const char *homedir;
+
+       if (path[1] == '/' || path[1] == '\0') {
+               p = (char *)&path[1];
+               homedir = getcurrentuserhomedir();
+       } else {
+               if ((p = strchr(path, '/')))
+                       name = g_strndup(&path[1], p - (path + 1));
+               else
+                       name = g_strdup(&path[1]);
+
+               homedir = getuserhomedir(name);
+               g_free(name);
+       }
+       apath = g_build_filename(homedir, p, NULL);
+       return apath;
+}
+
 Client *
 newclient(Client *rc)
 {
@@ -522,7 +531,7 @@ void
 loaduri(Client *c, const Arg *a)
 {
 	struct stat st;
-	char *url, *path;
+	char *url, *path, *apath;
 	const char *uri = a->v;
 
 	if (g_strcmp0(uri, "") == 0)
@@ -533,11 +542,19 @@ loaduri(Client *c, const Arg *a)
 	    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);
-		free(path);
 	} else {
-		url = g_strdup_printf("http://%s", uri);
+		if (uri[0] == '~')
+			apath = untildepath(uri);
+		else
+			apath = (char *)uri;
+		if (!stat(apath, &st) && (path = realpath(apath, NULL))) {
+			url = g_strdup_printf("file://%s", path);
+			free(path);
+		} else {
+			url = g_strdup_printf("http://%s", uri);
+		}
+		if (apath != uri)
+			free(apath);
 	}
 
 	setatom(c, AtomUri, url);