Cleanup, renaming other fn to be general.

This commit is contained in:
Jacob Windle 2020-01-17 08:12:05 -05:00
parent f5036783f9
commit fac37b8b40

27
main.c
View File

@ -50,7 +50,7 @@ FILE *fopenHostsFile(char *mode)
* @param newhost
* @param hostsFile
*/
void replacehost(char *oldhost, char *newhost, int deleteHost) {
void modifyHostsFile(char *oldhost, char *newhost, int deleteHost) {
FILE *hostsFile = fopenHostsFile("r");
char buf[256];
char *ptr, *f;
@ -109,9 +109,12 @@ void blockHost(char *host)
void deleteHost(char *host)
{
replacehost(host, NULL, 1);
modifyHostsFile(host, NULL, 1);
}
void replaceHost(char *oldhost, char *newhost) {
modifyHostsFile(oldhost, newhost, 0);
}
void showHosts()
{
@ -244,11 +247,9 @@ int main(int argc, char **argv)
}
// Process our command line arguments.
for (int i = 0; i < argc; i++)
{
for (int i = 0; i < argc; i++) {
// Opens a hosts file in append mode, adding a host.
if (ARG_IS("add"))
{
if (ARG_IS("add")) {
if (argc < 3) {
printf("Please provide a host!\n");
exit(1);
@ -256,25 +257,21 @@ int main(int argc, char **argv)
blockHost(argv[++i]);
}
// Replaces a host
else if (ARG_IS("edit"))
{
replacehost(argv[i + 1], argv[i + 2]);
else if (ARG_IS("edit")) {
replacehost(argv[i + 1], argv[i + 2], 0);
i += 2;
}
// Deletes a host.
else if (ARG_IS("delete"))
{
else if (ARG_IS("delete")) {
fprintf(stdout, "Soon to be implemented!\n");
}
// Shows usage.
else if (ARG_IS("-h"))
{
else if (ARG_IS("-h")) {
usage();
exit(0);
}
// Show the entire hosts file.
else if (ARG_IS("show"))
{
else if (ARG_IS("show")) {
showHosts();
}
else if (ARG_IS("-config")) {