From be0686f35d2dd2e8e22453c2450aa67ae24ef1a4 Mon Sep 17 00:00:00 2001 From: Morgan Howe Date: Thu, 21 Feb 2013 16:24:00 +0800 Subject: [PATCH] Use stat() and realpath() to improve handling of file parameters. --- main.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index a7f325e..40c6e8d 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ */ #include +#include #include #include #include @@ -1209,6 +1210,7 @@ open_arg(const Arg *arg) { Arg a = { .i = NavigationReload }; int len; char *search_uri, *search_term; + struct stat statbuf; if (client.state.embed) { gchar winid[64]; @@ -1259,10 +1261,17 @@ open_arg(const Arg *arg) { ++s; } *p = '\0'; - } else if (strcspn(s, "/") == 0 || strcspn(s, "./") == 0) { /* prepend "file://" */ - new = g_malloc(sizeof("file://") + len); - strcpy(new, "file://"); - memcpy(&new[sizeof("file://") - 1], s, len + 1); + } else if (!stat(s, &statbuf)) { /* prepend "file://" */ + char *rpath = realpath(s, NULL); + if (rpath != NULL) { + len = strlen(rpath); + new = g_malloc(sizeof("file://") + len); + sprintf(new, "file://%s", rpath); + free(rpath); + } else { + new = g_malloc(sizeof("file://") + len); + sprintf(new, "file://%s", s); + } } else if (p || !strchr(s, '.')) { /* whitespaces or no dot? */ search_uri = find_uri_for_searchengine(defaultsearch); if (search_uri != NULL) { -- 2.11.4.GIT