From ed0ba08d6664877eecbaa3cc5712e63444357cf0 Mon Sep 17 00:00:00 2001 From: jaketothepast Date: Wed, 17 Apr 2019 21:06:55 -0400 Subject: [PATCH] Trying to fix the reading routine --- Makefile | 4 ++-- README.md | 5 +++++ main.c | 32 ++++++++++++++++---------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 777849c..94504e8 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ CC=gcc CFLAGS=-I. hello: main.c - $(CC) -o hb main.c + $(CC) -g -o hb main.c install: hello - install -m 744 hb /usr/local/bin \ No newline at end of file + install -m 744 hb /usr/local/bin diff --git a/README.md b/README.md index 2b9e943..9c2840b 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,10 @@ I would like to get better with C, and will be using C much more in the near fut 1. Get delete working for my /etc/hosts editor. 2. Daemonize this program to be started up by systemd. - As part of daemonization, remember and remove added hosts on teardown. + - Can do this with an atexit handler, and a routine in main. 3. Log dns requests that look at the hosts file -- To see how many go where 4. As part of daemonization -- only block hosts within a time limit. + - Sleep, wakeup, check if time has passed or not, block/unblock host, + sleep again. +5. Read hosts to block from a config file (to not pollute the /etc/hosts file) +6. Design a unified interface for reading a hosts file. diff --git a/main.c b/main.c index 2df37e0..88e9abf 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ +#define _GNU_SOURCE +#include #include #include -#include #include #include #include @@ -66,14 +67,19 @@ void showHosts() void readToHost(char *host, FILE *hostsFile) { char buf[256]; - char *ptr; + char *ptr, *f; - while ((fgets(buf, 256, hostsFile)) != NULL) + while ((ptr = fgets(buf, 256, hostsFile)) != NULL) { - ptr = strcasestr(host, buf); - if (ptr != NULL) { - break; + /** + printf("%s\n", ptr); + printf("%s\n", host); + **/ + + f = strcasestr(host, ptr); + if (f != NULL) { printf("FOUND IT\n"); + break; } } } @@ -83,18 +89,13 @@ void readToHost(char *host, FILE *hostsFile) */ int main(int argc, char **argv) { - FILE *hostsFile = NULL; + FILE *hostsFile = fopenHostsFile(0); - pid_t pid; + readToHost("reddit.com", hostsFile); - pid = fork(); + fclose(hostsFile); + hostsFile = NULL; - if (pid > 0) { - return 0; - } - - - /** if (getuid() != 0) { fprintf(stderr, "hb: Must run as root using sudo!\n"); @@ -128,5 +129,4 @@ int main(int argc, char **argv) if (hostsFile != NULL) fclose(hostsFile); - **/ }