summaryrefslogtreecommitdiff
path: root/gtk
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2014-05-07 16:14:18 +0100
committerVincent Sanders <vince@kyllikki.org>2014-05-07 16:24:51 +0100
commitc56642819eed87431dff3446fe111f7f3eefaa7d (patch)
tree397126ca4baac425f9c1c44f3d5c56c681e2c4fe /gtk
parentc1e2da80dfa62793ea107cf12408c814e268a54b (diff)
downloadnetsurf-c56642819eed87431dff3446fe111f7f3eefaa7d.tar.gz
netsurf-c56642819eed87431dff3446fe111f7f3eefaa7d.tar.bz2
add file operations table and make all frontends use it.
This rationalises the path construction and basename file operations. The default implementation is POSIX which works for all frontends except windows, riscos and amiga which have differeing path separators and rules. These implementations are significantly more robust than the previous nine implementations and also do not use unsafe strncpy or buffers with arbitrary length limits. These implementations also carry full documentation comments.
Diffstat (limited to 'gtk')
-rw-r--r--gtk/dialogs/preferences.c18
-rw-r--r--gtk/fetch.c42
-rw-r--r--gtk/gui.c55
-rw-r--r--gtk/scaffolding.c10
4 files changed, 44 insertions, 81 deletions
diff --git a/gtk/dialogs/preferences.c b/gtk/dialogs/preferences.c
index fcf30c8fa..d36d0c68b 100644
--- a/gtk/dialogs/preferences.c
+++ b/gtk/dialogs/preferences.c
@@ -19,12 +19,12 @@
#include <stdint.h>
#include <math.h>
-#include "utils/filepath.h"
-#include "utils/log.h"
#include "utils/utils.h"
#include "utils/messages.h"
-#include "desktop/browser.h"
#include "utils/nsoption.h"
+#include "utils/file.h"
+#include "utils/log.h"
+#include "desktop/browser.h"
#include "desktop/searchweb.h"
#include "gtk/compat.h"
@@ -1002,10 +1002,10 @@ nsgtk_preferences_fileChooserDownloads_realize(GtkWidget *widget,
G_MODULE_EXPORT void
nsgtk_preferences_dialogPreferences_response(GtkDialog *dlg, gint resid)
{
- char *choices;
+ char *choices = NULL;
if (resid == GTK_RESPONSE_CLOSE) {
- choices = filepath_append(nsgtk_config_home, "Choices");
+ netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
if (choices != NULL) {
nsoption_write(choices, NULL, NULL);
free(choices);
@@ -1018,9 +1018,9 @@ G_MODULE_EXPORT gboolean
nsgtk_preferences_dialogPreferences_deleteevent(GtkDialog *dlg,
struct ppref *priv)
{
- char *choices;
+ char *choices = NULL;
- choices = filepath_append(nsgtk_config_home, "Choices");
+ netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
if (choices != NULL) {
nsoption_write(choices, NULL, NULL);
free(choices);
@@ -1037,9 +1037,9 @@ nsgtk_preferences_dialogPreferences_deleteevent(GtkDialog *dlg,
G_MODULE_EXPORT void
nsgtk_preferences_dialogPreferences_destroy(GtkDialog *dlg, struct ppref *priv)
{
- char *choices;
+ char *choices = NULL;
- choices = filepath_append(nsgtk_config_home, "Choices");
+ netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
if (choices != NULL) {
nsoption_write(choices, NULL, NULL);
free(choices);
diff --git a/gtk/fetch.c b/gtk/fetch.c
index 8db102ce8..a76ad9f4d 100644
--- a/gtk/fetch.c
+++ b/gtk/fetch.c
@@ -226,45 +226,6 @@ const char *fetch_filetype(const char *unix_path)
return type;
}
-/**
- * Return the filename part of a full path
- *
- * \param path full path and filename
- * \return filename (will be freed with free())
- */
-static char *filename_from_path(char *path)
-{
- char *leafname;
-
- leafname = strrchr(path, '/');
- if (!leafname) {
- leafname = path;
- } else {
- leafname += 1;
- }
-
- return strdup(leafname);
-}
-
-/**
- * Add a path component/filename to an existing path
- *
- * \param path buffer containing path + free space
- * \param length length of buffer "path"
- * \param newpart string containing path component to add to path
- * \return true on success
- */
-static bool path_add_part(char *path, int length, const char *newpart)
-{
- if (path[strlen(path) - 1] != '/') {
- strncat(path, "/", length);
- }
-
- strncat(path, newpart, length);
-
- return true;
-}
-
char *path_to_url(const char *path)
{
int urllen;
@@ -337,14 +298,11 @@ static nsurl *gui_get_resource_url(const char *path)
}
static struct gui_fetch_table fetch_table = {
- .filename_from_path = filename_from_path,
- .path_add_part = path_add_part,
.filetype = fetch_filetype,
.path_to_url = path_to_url,
.url_to_path = url_to_path,
.get_resource_url = gui_get_resource_url,
-
};
struct gui_fetch_table *nsgtk_fetch_table = &fetch_table;
diff --git a/gtk/gui.c b/gtk/gui.c
index 3147bf1b8..bae3568c6 100644
--- a/gtk/gui.c
+++ b/gtk/gui.c
@@ -55,6 +55,14 @@
#include "desktop/textinput.h"
#include "desktop/tree.h"
#include "css/utils.h"
+#include "render/form.h"
+#include "utils/filepath.h"
+#include "utils/log.h"
+#include "utils/messages.h"
+#include "utils/url.h"
+#include "utils/utf8.h"
+#include "utils/utils.h"
+#include "utils/file.h"
#include "gtk/compat.h"
#include "gtk/completion.h"
@@ -71,13 +79,6 @@
#include "gtk/selection.h"
#include "gtk/search.h"
-#include "render/form.h"
-#include "utils/filepath.h"
-#include "utils/log.h"
-#include "utils/messages.h"
-#include "utils/url.h"
-#include "utils/utf8.h"
-#include "utils/utils.h"
char *toolbar_indices_file_location;
char *res_dir_location;
@@ -253,33 +254,37 @@ static nserror set_defaults(struct nsoption_s *defaults)
char *fname;
/* cookie file default */
- fname = filepath_append(nsgtk_config_home, "Cookies");
+ fname = NULL;
+ netsurf_mkpath(&fname, NULL, 2, nsgtk_config_home, "Cookies");
if (fname != NULL) {
nsoption_setnull_charp(cookie_file, fname);
}
/* cookie jar default */
- fname = filepath_append(nsgtk_config_home, "Cookies");
+ fname = NULL;
+ netsurf_mkpath(&fname, NULL, 2, nsgtk_config_home, "Cookies");
if (fname != NULL) {
nsoption_setnull_charp(cookie_jar, fname);
}
/* url database default */
- fname = filepath_append(nsgtk_config_home, "URLs");
+ fname = NULL;
+ netsurf_mkpath(&fname, NULL, 2, nsgtk_config_home, "URLs");
if (fname != NULL) {
nsoption_setnull_charp(url_file, fname);
}
/* bookmark database default */
- fname = filepath_append(nsgtk_config_home, "Hotlist");
+ fname = NULL;
+ netsurf_mkpath(&fname, NULL, 2, nsgtk_config_home, "Hotlist");
if (fname != NULL) {
nsoption_setnull_charp(hotlist_path, fname);
}
/* download directory default */
- fname = filepath_append(getenv("HOME"), "");
+ fname = getenv("HOME");
if (fname != NULL) {
- nsoption_setnull_charp(downloads_directory, fname);
+ nsoption_setnull_charp(downloads_directory, strdup(fname));
}
/* default path to certificates */
@@ -955,12 +960,12 @@ static nserror
check_dirname(const char *path, const char *leaf, char **dirname_out)
{
nserror ret;
- char *dirname;
+ char *dirname = NULL;
struct stat dirname_stat;
- dirname = filepath_append(path, leaf);
- if (dirname == NULL) {
- return NSERROR_NOMEM;
+ ret = netsurf_mkpath(&dirname, NULL, 2, path, leaf);
+ if (ret != NSERROR_OK) {
+ return ret;
}
/* ensure access is possible and the entry is actualy
@@ -1075,14 +1080,14 @@ static nserror create_config_home(char **config_home_out)
return NSERROR_NOT_DIRECTORY;
}
- config_home = filepath_append(home_dir, ".config/netsurf/");
- if (config_home == NULL) {
- return NSERROR_NOMEM;
+ ret = netsurf_mkpath(&config_home, NULL, 2, home_dir, ".config/netsurf/");
+ if (ret != NSERROR_OK) {
+ return ret;
}
} else {
- config_home = filepath_append(xdg_config_dir, "netsurf/");
- if (config_home == NULL) {
- return NSERROR_NOMEM;
+ ret = netsurf_mkpath(&config_home, NULL, 2, xdg_config_dir, "netsurf/");
+ if (ret != NSERROR_OK) {
+ return ret;
}
}
@@ -1113,8 +1118,8 @@ static nserror nsgtk_option_init(int *pargc, char** argv)
}
/* Attempt to load the user choices */
- choices = filepath_append(nsgtk_config_home, "Choices");
- if (choices != NULL) {
+ ret = netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
+ if (ret == NSERROR_OK) {
nsoption_read(choices, nsoptions);
free(choices);
}
diff --git a/gtk/scaffolding.c b/gtk/scaffolding.c
index 436830bd3..fde195657 100644
--- a/gtk/scaffolding.c
+++ b/gtk/scaffolding.c
@@ -30,6 +30,7 @@
#include "utils/url.h"
#include "utils/log.h"
#include "utils/nsoption.h"
+#include "utils/file.h"
#include "desktop/browser_history.h"
#include "desktop/browser_private.h"
#include "desktop/hotlist.h"
@@ -72,7 +73,6 @@
#include "gtk/tabs.h"
#include "gtk/schedule.h"
-
/** Macro to define a handler for menu, button and activate events. */
#define MULTIHANDLER(q)\
static gboolean nsgtk_on_##q##_activate(struct gtk_scaffolding *g);\
@@ -867,7 +867,7 @@ MULTIHANDLER(print)
GtkPrintSettings *print_settings;
GtkPrintOperationResult res = GTK_PRINT_OPERATION_RESULT_ERROR;
struct print_settings *nssettings;
- char *settings_fname;
+ char *settings_fname = NULL;
print_op = gtk_print_operation_new();
if (print_op == NULL) {
@@ -876,7 +876,7 @@ MULTIHANDLER(print)
}
/* use previously saved settings if any */
- settings_fname = filepath_append(nsgtk_config_home, "Print");
+ netsurf_mkpath(&settings_fname, NULL, 2, nsgtk_config_home, "Print");
if (settings_fname != NULL) {
print_settings = gtk_print_settings_new_from_file(settings_fname, NULL);
if (print_settings != NULL) {
@@ -1260,7 +1260,7 @@ MULTIHANDLER(downloads)
MULTIHANDLER(savewindowsize)
{
int x,y,w,h;
- char *choices;
+ char *choices = NULL;
gtk_window_get_position(g->window, &x, &y);
gtk_window_get_size(g->window, &w, &h);
@@ -1270,7 +1270,7 @@ MULTIHANDLER(savewindowsize)
nsoption_set_int(window_x, x);
nsoption_set_int(window_y, y);
- choices = filepath_append(nsgtk_config_home, "Choices");
+ netsurf_mkpath(&choices, NULL, 2, nsgtk_config_home, "Choices");
if (choices != NULL) {
nsoption_write(choices, NULL, NULL);
free(choices);