Cleaning up C code now that I'm more skilled

pull/4/head
Jacob Windle 2019-12-13 16:28:11 -05:00
parent dbc49e15a6
commit ee9bc42f8c
1 changed files with 27 additions and 67 deletions

94
main.c
View File

@ -31,7 +31,7 @@ void int_handler(int signal) {
fprintf(stderr, "SIGINT received, cleaning up...\n"); fprintf(stderr, "SIGINT received, cleaning up...\n");
} }
void replacehost(char *oldhost, char *newhost, FILE *hostsFile); void replacehost(char *oldhost, char *newhost);
/** /**
* Return an open handle to the hosts file. * Return an open handle to the hosts file.
@ -39,42 +39,35 @@ void replacehost(char *oldhost, char *newhost, FILE *hostsFile);
* @param mode whichever mode host file you want to open * @param mode whichever mode host file you want to open
* @return FILE * * @return FILE *
*/ */
FILE *fopenHostsFile(int mode) FILE *fopenHostsFile(char *mode)
{ {
switch(mode) return fopen(HOSTFILE, mode);
{
case 0:
return fopen(HOSTFILE, "r");
case 1:
return fopen(HOSTFILE, "a");
case 2:
return fopen(HOSTFILE, "r+");
}
} }
/** /**
* Block a hosts using the hostsFile in FILE * Block a hosts using the hostsFile in FILE
*/ */
void blockHost(FILE *hostsFile, char *host) void blockHost(char *host)
{ {
// Buffer to hold our block rule. FILE *hostsFile = fopenHostsFile("a");
char blockRule[256]; char blockRule[256];
sprintf(blockRule, "%s %s\n", blockString, host);
// Copy the blockstring and host to the blockrule.
strcpy(blockRule, blockString);
strcat(blockRule, host);
strcat(blockRule, "\n");
// Add a null terminator..
blockRule[strlen(blockRule)] = 0; blockRule[strlen(blockRule)] = 0;
fputs(blockRule, hostsFile);
ONFAILED(EOF, (fputs(blockRule, hostsFile))) {
fprintf(stderr, "Failed to write block rule to file\n");
}
fclose(hostsFile);
} }
void usage() void usage()
{ {
fprintf(stdout, "Usage: "); fprintf(stdout, "Usage: ");
fprintf(stdout, "hb [add] <sitename>\n"); fprintf(stdout, "hb [add] <sitename>\n");
fprintf(stdout, "hb [edit] <oldsite> <newsite>\n");
fprintf(stdout, "hb [show]\n");
fprintf(stdout, "hb [delete] <sitename>\n");
} }
void showHosts() void showHosts()
@ -82,7 +75,7 @@ void showHosts()
pid_t child = fork(); pid_t child = fork();
int rc = 0; int rc = 0;
char *const parmList[] = {"/bin/cat", "/etc/hosts", NULL}; char *const parmList[] = {"/bin/cat", HOSTFILE, NULL};
if (child == 0) if (child == 0)
{ {
@ -94,31 +87,6 @@ void showHosts()
} }
} }
void readToHost(char *host, FILE *hostsFile)
{
char buf[256];
char *ptr, *f;
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);
**/
printf("Reading!\n");
f = strcasestr(buf, host);
if (f != NULL) {
printf("FOUND IT\n");
break;
}
memset(buf, 0, sizeof(buf));
}
}
/** /**
* Daemonize the process, to be run like this ONLY if there were no arguments * Daemonize the process, to be run like this ONLY if there were no arguments
* provided. * provided.
@ -147,7 +115,6 @@ int main(int argc, char **argv)
// Install a sigint handler to help us clean up. // Install a sigint handler to help us clean up.
signal(SIGINT, int_handler); signal(SIGINT, int_handler);
FILE *hostsFile;
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");
@ -163,16 +130,14 @@ int main(int argc, char **argv)
printf("Please provide a host!\n"); printf("Please provide a host!\n");
exit(1); exit(1);
} }
hostsFile = fopenHostsFile(1); blockHost(argv[i+1]);
blockHost(hostsFile, argv[i+1]);
} }
// Replaces a host // Replaces a host
// TODO: this currently duplicates the whole file and appends it to the end. // TODO: this currently duplicates the whole file and appends it to the end.
else if (strcmp(argv[i], "edit") == 0) else if (strcmp(argv[i], "edit") == 0)
{ {
hostsFile = fopenHostsFile(2); replacehost(argv[i+1], argv[i+2]);
replacehost(argv[i+1], argv[i+2], hostsFile); }
}
// Deletes a host. // Deletes a host.
else if (strcmp(argv[i], "delete") == 0) else if (strcmp(argv[i], "delete") == 0)
{ {
@ -194,9 +159,6 @@ int main(int argc, char **argv)
// daemonize(); // daemonize();
} }
} }
if (hostsFile != NULL)
fclose(hostsFile);
} }
/** /**
@ -206,7 +168,8 @@ int main(int argc, char **argv)
* @param newhost * @param newhost
* @param hostsFile * @param hostsFile
*/ */
void replacehost(char *oldhost, char *newhost, FILE *hostsFile) { void replacehost(char *oldhost, char *newhost) {
FILE *hostsFile = fopenHostsFile("w+");
char buf[256]; char buf[256];
char *ptr, *f; char *ptr, *f;
@ -216,20 +179,13 @@ void replacehost(char *oldhost, char *newhost, FILE *hostsFile) {
printf("Starting to read!\n"); printf("Starting to read!\n");
printf("Looking for host:%s\n", oldhost); printf("Looking for host:%s\n", oldhost);
fseek(hostsFile, 0, SEEK_SET); fseek(hostsFile, 0, SEEK_SET);
while (fgets(buf, 256, hostsFile) != NULL) while (fgets(buf, 256, hostsFile) != NULL)
{ {
/**
printf("%s\n", ptr);
printf("%s\n", host);
**/
printf("Reading!\n");
f = strcasestr(buf, oldhost); f = strcasestr(buf, oldhost);
if (f != NULL) { if (f != NULL) {
printf("FOUND IT\n");
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
sprintf(buf, "%s %s\n", blockString, newhost); sprintf(buf, "%s %s\n", blockString, newhost);
printf("New buf: %s", buf);
} }
strcat(newHostsFile, buf); strcat(newHostsFile, buf);
@ -239,8 +195,12 @@ void replacehost(char *oldhost, char *newhost, FILE *hostsFile) {
// Ensure we have an EOF at the end of the file. // Ensure we have an EOF at the end of the file.
newHostsFile[strlen(newHostsFile)] = EOF; newHostsFile[strlen(newHostsFile)] = EOF;
// Seek to 0
fseek(hostsFile, 0, SEEK_SET);
// If this failed, write an error message to stderr // If this failed, write an error message to stderr
ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) {
fprintf(stderr, "Did not write anything!\n"); fprintf(stderr, "Did not write anything!\n");
} }
fclose(hostsFile);
} }