From 323fd19544bed16ca2c0acf9c465f7311bf3965d Mon Sep 17 00:00:00 2001 From: Jacob Date: Thu, 12 Dec 2019 16:08:27 -0500 Subject: [PATCH] Working on host replacement method --- CMakeLists.txt | 2 ++ main.c | 68 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..5f9a23e --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,2 @@ +project(HostBlocker) +add_executable(hb main.c) \ No newline at end of file diff --git a/main.c b/main.c index 948d4d0..c954630 100644 --- a/main.c +++ b/main.c @@ -11,15 +11,27 @@ * block sites within a certain time limit. */ const char *blockString = "0.0.0.0 "; +static const char *HOSTFILE = "/etc/hosts"; +void replacehost(char *oldhost, char *newhost, FILE *hostsFile); + +/** + * Return an open handle to the hosts file. + * + * @param mode whichever mode host file you want to open + * @return FILE * + */ FILE *fopenHostsFile(int mode) { switch(mode) { case 0: - return fopen("/etc/hosts", "r"); + return fopen(HOSTFILE, "r"); case 1: - return fopen("/etc/hosts", "a"); + return fopen(HOSTFILE, "a"); + case 2: + return fopen(HOSTFILE, "r+"); + } } @@ -69,18 +81,23 @@ void readToHost(char *host, FILE *hostsFile) char buf[256]; char *ptr, *f; - while ((ptr = fgets(buf, 256, hostsFile)) != NULL) + printf("Starting to read!\n"); + printf("Looking for host:%s\n", host); + fseek(hostsFile, 0, SEEK_SET); + while (fgets(buf, 256, hostsFile) != NULL) { /** printf("%s\n", ptr); printf("%s\n", host); **/ - f = strcasestr(host, ptr); + printf("Reading!\n"); + f = strcasestr(buf, host); if (f != NULL) { printf("FOUND IT\n"); break; } + memset(buf, 0, sizeof(buf)); } } @@ -100,16 +117,17 @@ int main(int argc, char **argv) { if (strcmp(argv[i], "add") == 0) { - if (argc < 3) { + if (argc < 3) { printf("Please provide a host!\n"); - exit(1); - } + exit(1); + } hostsFile = fopenHostsFile(1); blockHost(hostsFile, argv[i+1]); } else if (strcmp(argv[i], "edit") == 0) { - readToHost(argv[i+1], hostsFile); + hostsFile = fopenHostsFile(2); + replacehost(argv[i+1], argv[i+2], hostsFile); } else if (strcmp(argv[i], "delete") == 0) { @@ -129,3 +147,37 @@ int main(int argc, char **argv) if (hostsFile != NULL) fclose(hostsFile); } + +void replacehost(char *oldhost, char *newhost, FILE *hostsFile) { + char buf[256]; + char *ptr, *f; + + char newHostsFile[4096]; + + printf("Starting to read!\n"); + printf("Looking for host:%s\n", oldhost); + fseek(hostsFile, 0, SEEK_SET); + while (fgets(buf, 256, hostsFile) != NULL) + { + /** + printf("%s\n", ptr); + printf("%s\n", host); + **/ + + printf("Reading!\n"); + f = strcasestr(buf, oldhost); + if (f != NULL) { + printf("FOUND IT\n"); + memset(buf, 0, sizeof(buf)); + sprintf(buf, "%s %s\n", blockString, newhost); + printf("New buf: %s", buf); + } + + strcat(newHostsFile, buf); + memset(buf, 0, sizeof(buf)); + } + + if(0 >= fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile)) { + fprintf(stderr, "Did not write anything!\n"); + } +}