diff options
author | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2010-08-20 23:24:19 +0200 |
---|---|---|
committer | Johannes Hofmann <Johannes.Hofmann@gmx.de> | 2010-08-20 23:24:19 +0200 |
commit | f5c598b518d1f906148534d015f50075d3e8242d (patch) | |
tree | 21dd70add5b366c3dd80641b77f6b18e0baa009e /src/paths.cc | |
parent | e98d02a01ffeb18ede86af025e51ae1ec011c75a (diff) | |
parent | 5f0fc0e48b8cbee7e1795935da0abff6627fd498 (diff) |
merge
Diffstat (limited to 'src/paths.cc')
-rw-r--r-- | src/paths.cc | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/src/paths.cc b/src/paths.cc new file mode 100644 index 00000000..6fccb89a --- /dev/null +++ b/src/paths.cc @@ -0,0 +1,101 @@ +/* + * 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; + int rc = 0; + + dFree(oldWorkingDir); + oldWorkingDir = dGetcwd(); + rc = chdir("/tmp"); + if (rc == -1) { + MSG("paths: error changing directory to /tmp: %s\n", + dStrerror(errno)); + } + + 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(DILLO_SYSCONF, 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", path2); + } + dFree(path2); + } + + dFree(path); + return fp; +} + |