Trying to fix the reading routine

This commit is contained in:
Jacob Windle 2019-04-17 21:06:55 -04:00
parent 51b2a4e7d0
commit ed0ba08d66
3 changed files with 23 additions and 18 deletions

View File

@ -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

View File

@ -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.

32
main.c
View File

@ -1,6 +1,7 @@
#define _GNU_SOURCE
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
@ -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);
**/
}