#include <libgen.h>
#include <stdarg.h>
#include <regex.h>
+#include <pwd.h>
+#include <string.h>
#include "arg.h"
static void beforerequest(WebKitWebView *w, WebKitWebFrame *f,
WebKitWebResource *r, WebKitNetworkRequest *req,
WebKitNetworkResponse *resp, Client *c);
+static char *buildfile(const char *path);
static char *buildpath(const char *path);
static gboolean buttonrelease(WebKitWebView *web, GdkEventButton *e, Client *c);
static void cleanup(void);
}
static char *
-buildpath(const char *path) {
- char *apath, *p;
+buildfile(const char *path) {
+ char *dname, *bname, *bpath, *fpath;
FILE *f;
- /* creating directory */
- if(path[0] == '/') {
- apath = g_strdup(path);
- } else if(path[0] == '~') {
- if(path[1] == '/') {
- apath = g_strconcat(g_get_home_dir(), &path[1], NULL);
+ dname = g_path_get_dirname(path);
+ bname = g_path_get_basename(path);
+
+ bpath = buildpath(dname);
+ g_free(dname);
+
+ fpath = g_build_filename(bpath, bname, NULL);
+ g_free(bname);
+
+ if(!(f = fopen(fpath, "a")))
+ die("Could not open file: %s\n", fpath);
+
+ g_chmod(fpath, 0600); /* always */
+ fclose(f);
+
+ return fpath;
+}
+
+static char *
+buildpath(const char *path) {
+ struct passwd *pw;
+ char *apath, *name, *p, *fpath;
+
+ if(path[0] == '~') {
+ if(path[1] == '/' || path[1] == '\0') {
+ p = (char *)&path[1];
+ pw = getpwuid(getuid());
} else {
- apath = g_strconcat(g_get_home_dir(), "/",
- &path[1], NULL);
+ 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);
+ }
+ g_free(name);
}
+ apath = g_build_filename(pw->pw_dir, p, NULL);
} else {
- apath = g_strconcat(g_get_current_dir(), "/", path, NULL);
+ apath = g_strdup(path);
}
- if((p = strrchr(apath, '/'))) {
- *p = '\0';
- g_mkdir_with_parents(apath, 0700);
- g_chmod(apath, 0700); /* in case it existed */
- *p = '/';
- }
- /* creating file (gives error when apath ends with "/") */
- if((f = fopen(apath, "a"))) {
- g_chmod(apath, 0600); /* always */
- fclose(f);
- }
+ /* creating directory */
+ if(g_mkdir_with_parents(apath, 0700) < 0)
+ die("Could not access directory: %s\n", apath);
+
+ fpath = realpath(apath, NULL);
+ g_free(apath);
- return apath;
+ return fpath;
}
static gboolean
return g_object_new(COOKIEJAR_TYPE,
SOUP_COOKIE_JAR_TEXT_FILENAME, filename,
SOUP_COOKIE_JAR_READ_ONLY, read_only,
- SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
+ SOUP_COOKIE_JAR_ACCEPT_POLICY, policy, NULL);
}
static void
g_object_set(G_OBJECT(settings), "user-agent", ua, NULL);
if (enablestyles) {
g_object_set(G_OBJECT(settings), "user-stylesheet-uri",
- getstyle("about:blank"), NULL);
+ getstyle("about:blank"), NULL);
}
g_object_set(G_OBJECT(settings), "auto-load-images", loadimages,
NULL);
static void
menuactivate(GtkMenuItem *item, Client *c) {
/*
- * context-menu-action-2000 open link
- * context-menu-action-1 open link in window
- * context-menu-action-2 download linked file
- * context-menu-action-3 copy link location
- * context-menu-action-7 copy image address
- * context-menu-action-13 reload
- * context-menu-action-10 back
- * context-menu-action-11 forward
- * context-menu-action-12 stop
+ * context-menu-action-2000 open link
+ * context-menu-action-1 open link in window
+ * context-menu-action-2 download linked file
+ * context-menu-action-3 copy link location
+ * context-menu-action-7 copy image address
+ * context-menu-action-13 reload
+ * context-menu-action-10 back
+ * context-menu-action-11 forward
+ * context-menu-action-12 stop
*/
GtkAction *a = NULL;
reload(Client *c, const Arg *arg) {
gboolean nocache = *(gboolean *)arg;
if(nocache) {
- webkit_web_view_reload_bypass_cache(c->view);
+ webkit_web_view_reload_bypass_cache(c->view);
} else {
- webkit_web_view_reload(c->view);
+ webkit_web_view_reload(c->view);
}
}
atoms[AtomUri] = XInternAtom(dpy, "_SURF_URI", False);
/* dirs and files */
- cookiefile = buildpath(cookiefile);
- scriptfile = buildpath(scriptfile);
+ cookiefile = buildfile(cookiefile);
+ scriptfile = buildfile(scriptfile);
cachefolder = buildpath(cachefolder);
styledir = buildpath(styledir);
if(stylefile == NULL) {
styles[i].regex);
styles[i].regex = NULL;
}
- styles[i].style = buildpath(
- g_strconcat(styledir,
+ styles[i].style = buildfile(
+ g_strconcat(styledir, "/",
styles[i].style, NULL));
}
} else {
- stylefile = buildpath(stylefile);
+ stylefile = buildfile(stylefile);
}
/* request handler */
/* proxy */
if((proxy = getenv("http_proxy")) && strcmp(proxy, "")) {
- new_proxy = g_strrstr(proxy, "http://") ? g_strdup(proxy) :
- g_strdup_printf("http://%s", proxy);
+ new_proxy = g_strrstr(proxy, "http://")
+ || g_strrstr(proxy, "socks://")
+ || g_strrstr(proxy, "socks4://")
+ || g_strrstr(proxy, "socks5://")
+ ? g_strdup(proxy)
+ : g_strdup_printf("http://%s", proxy);
puri = soup_uri_new(new_proxy);
g_object_set(G_OBJECT(s), "proxy-uri", puri, NULL);
soup_uri_free(puri);