Fix segfault when missing host on add subcommand
This commit is contained in:
parent
ed0ba08d66
commit
7989bf9278
15
main.c
15
main.c
@ -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]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user