From d2d092ed3176bd62d88e3994d7c97cd7ea816151 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Thu, 26 Aug 2021 19:17:57 -0500 Subject: [PATCH] Copy tmp file to hosts file when complete --- src/rhosts.c | 26 ++++++++++++++++++++++++++ src/rhosts.h | 1 + 2 files changed, 27 insertions(+) diff --git a/src/rhosts.c b/src/rhosts.c index 57d7369..7c7c874 100644 --- a/src/rhosts.c +++ b/src/rhosts.c @@ -27,6 +27,11 @@ int main(int argc, char *argv[]){ printf("%d - download_entries failed",rc); return rc; } + rc = copy_tmp_to_hosts(); + if (rc != 0){ + printf("%d - failed to copy to hosts file",rc); + return rc; + } return 0; } @@ -333,3 +338,24 @@ int copy_old_download(char *url){ fclose(tmpf); return 0; } +// Copies the contents of the temp file to the hosts file +int copy_tmp_to_hosts(){ + FILE *tmpf; + tmpf = fopen(TMPLOCATION,"r"); + if (tmpf == NULL) + return 1; + FILE *hostsf; + hostsf = fopen(HOSTSLOCATION, "w"); + if (hostsf == NULL){ + printf("Failed to open %s\n",HOSTSLOCATION); + fclose(tmpf); + return 1; + } + char c; + + for(c = fgetc(tmpf);c != EOF;c = fgetc(tmpf)){ + fputc(c,hostsf); + } + remove(TMPLOCATION); + return 0; +} diff --git a/src/rhosts.h b/src/rhosts.h index 8147033..0a7b3e0 100644 --- a/src/rhosts.h +++ b/src/rhosts.h @@ -46,4 +46,5 @@ int add_site_entries(struct entry **entries); int download_libcurl(char *e); int parse_download(char *buff, size_t size, size_t nmemb); int copy_old_download(char *url); +int copy_tmp_to_hosts(); #endif