aboutsummaryrefslogtreecommitdiff
path: root/src/paths.cc
diff options
context:
space:
mode:
authorTim Nieradzik, Jorge Arellano Cid <devnull@localhost>2009-04-28 08:32:47 -0400
committerTim Nieradzik, Jorge Arellano Cid <devnull@localhost>2009-04-28 08:32:47 -0400
commit25308249920a3f0bf194569193ddf369ff19e480 (patch)
treee250e306df1031bf532cd154e68ea415431e16c2 /src/paths.cc
parent3b8078f51c75b6d9160688c544db60729df83e90 (diff)
Refactor the preferences parser
Diffstat (limited to 'src/paths.cc')
-rw-r--r--src/paths.cc96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/paths.cc b/src/paths.cc
new file mode 100644
index 00000000..febe7741
--- /dev/null
+++ b/src/paths.cc
@@ -0,0 +1,96 @@
+/*
+ * File: paths.cc
+ *
+ * Copyright 2006-2009 Jorge Arellano Cid <jcid@dillo.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include "msg.h"
+#include "../dlib/dlib.h"
+#include "paths.hh"
+
+/*
+ * Local data
+ */
+
+// Dillo works from an unmounted directory (/tmp)
+static char* oldWorkingDir = NULL;
+
+/*
+ * Changes current working directory to /tmp and creates ~/.dillo
+ * if not exists.
+ */
+void Paths::init(void)
+{
+ char *path;
+ struct stat st;
+
+ dFree(oldWorkingDir);
+ oldWorkingDir = dGetcwd();
+ chdir("/tmp");
+
+ path = dStrconcat(dGethomedir(), "/.dillo", NULL);
+ if (stat(path, &st) == -1) {
+ if (errno == ENOENT) {
+ MSG("paths: creating directory %s.\n", path);
+ if (mkdir(path, 0700) < 0) {
+ MSG("paths: error creating directory %s: %s\n",
+ path, dStrerror(errno));
+ }
+ } else {
+ MSG("Dillo: error reading %s: %s\n", path, dStrerror(errno));
+ }
+ }
+
+ dFree(path);
+}
+
+/*
+ * Return the initial current working directory in a string.
+ */
+char *Paths::getOldWorkingDir(void)
+{
+ return oldWorkingDir;
+}
+
+/*
+ * Free memory
+ */
+void Paths::free(void)
+{
+ dFree(oldWorkingDir);
+}
+
+/*
+ * Examines the path for "rcFile" and assign its file pointer to "fp".
+ */
+FILE *Paths::getPrefsFP(const char *rcFile)
+{
+ FILE *fp;
+ char *path = dStrconcat(dGethomedir(), "/.dillo/", rcFile, NULL);
+
+ if (!(fp = fopen(path, "r"))) {
+ MSG("paths: Cannot open file '%s'\n", path);
+
+ char *path2 = dStrconcat(PATHS_RC_SYS, rcFile, NULL);
+ if (!(fp = fopen(path2, "r"))) {
+ MSG("paths: Cannot open file '%s'\n",path2);
+ MSG("paths: Using internal defaults...\n");
+ } else {
+ MSG("paths: Using %s\n", path);
+ }
+ dFree(path2);
+ }
+
+ dFree(path);
+ return fp;
+}
+