From 39d91820f4043b8130f0ba62ecaeeb85cf5e00b2 Mon Sep 17 00:00:00 2001 From: w00fpack Date: Fri, 19 Nov 2021 17:20:38 -0500 Subject: [PATCH] If not exist, automatically copy default /usr/etc/dillo config files to ~/.dillo/ --- src/paths.cc | 36 +++++++++++++++++++++++++++--------- src/paths.hh | 3 +++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/paths.cc b/src/paths.cc index bb233de8..10b488fe 100644 --- a/src/paths.cc +++ b/src/paths.cc @@ -17,6 +17,8 @@ #include "../dlib/dlib.h" #include "paths.hh" +#include + /* * Local data */ @@ -81,21 +83,37 @@ FILE *Paths::getPrefsFP(const char *rcFile) { FILE *fp; char *path = dStrconcat(dGethomedir(), "/.dillo/", rcFile, NULL); + char *path2 = dStrconcat(DILLO_SYSCONF, rcFile, NULL); if (!(fp = fopen(path, "r"))) { - MSG("paths: Cannot open file '%s': %s\n", path, dStrerror(errno)); - - char *path2 = dStrconcat(DILLO_SYSCONF, rcFile, NULL); - if (!(fp = fopen(path2, "r"))) { - MSG("paths: Cannot open file '%s': %s\n", path2, dStrerror(errno)); - MSG("paths: Using internal defaults...\n"); - } else { - MSG("paths: Using %s\n", path2); + copy_file(path2, path); + if (!(fp = fopen(path, "r"))) { + MSG("paths: Using internal defaults because could not make '%s': %s\n", path, dStrerror(errno)); } - dFree(path2); } dFree(path); + dFree(path2); + return fp; } +void Paths::copy_file( char *filename, char *outfile){ + int in_fd, out_fd, n_chars; + char buf[BUFFERSIZE]; + + if ( (in_fd=open(filename, O_RDONLY)) == -1 ) + MSG("Cannot open %s: %s", filename,dStrerror(errno)); + + if ( (out_fd=creat( outfile, COPYMODE)) == -1 ) + MSG( "Cannot create %s: %s", outfile, dStrerror(errno)); + + while ( (n_chars = read(in_fd , buf, BUFFERSIZE)) > 0 ) + + if ( write( out_fd, buf, n_chars ) != n_chars ) + MSG("Write error to %s: %s", outfile, dStrerror(errno)); + if ( n_chars == -1 ) + MSG("Read error from %s: %s", outfile, dStrerror(errno)); + if ( close(in_fd) == -1 || close(out_fd) == -1 ) + MSG("Error closing files %s", dStrerror(errno)); +} \ No newline at end of file diff --git a/src/paths.hh b/src/paths.hh index ecc02f8b..f4e99e1d 100644 --- a/src/paths.hh +++ b/src/paths.hh @@ -16,6 +16,8 @@ #define PATHS_RC_KEYS "keysrc" #define PATHS_RC_DOMAIN "domainrc" #define PATHS_HSTS_PRELOAD "hsts_preload" +#define BUFFERSIZE 4096 +#define COPYMODE 0644 class Paths { public: @@ -23,6 +25,7 @@ public: static void free(void); static char *getOldWorkingDir(void); static FILE *getPrefsFP(const char *rcFile); + static void copy_file(char *, char*); }; #endif /* __PATHS_HH__ */