Fix segfault when missing host on add subcommand

This commit is contained in:
Jacob Windle 2019-11-27 13:26:07 -05:00
parent ed0ba08d66
commit 7989bf9278

15
main.c
View File

@ -14,7 +14,7 @@ const char *blockString = "0.0.0.0 ";
FILE *fopenHostsFile(int mode) FILE *fopenHostsFile(int mode)
{ {
switch(mode) switch(mode)
{ {
case 0: case 0:
return fopen("/etc/hosts", "r"); return fopen("/etc/hosts", "r");
@ -89,22 +89,21 @@ void readToHost(char *host, FILE *hostsFile)
*/ */
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
FILE *hostsFile = fopenHostsFile(0); FILE *hostsFile;
readToHost("reddit.com", hostsFile);
fclose(hostsFile);
hostsFile = NULL;
if (getuid() != 0) if (getuid() != 0)
{ {
fprintf(stderr, "hb: Must run as root using sudo!\n"); fprintf(stderr, "hb: Must run as root using sudo!\n");
} }
printf("Argc: %d\n", argc);
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
if (strcmp(argv[i], "add") == 0) if (strcmp(argv[i], "add") == 0)
{ {
if (argc < 3) {
printf("Please provide a host!\n");
exit(1);
}
hostsFile = fopenHostsFile(1); hostsFile = fopenHostsFile(1);
blockHost(hostsFile, argv[i+1]); blockHost(hostsFile, argv[i+1]);
} }