feature/delete-host: Delete a hosts from the hosts file. #4
94
main.c
94
main.c
@ -32,9 +32,6 @@ static char *HOSTFILE = "/etc/hosts";
|
||||
static char *CONFIG;
|
||||
// Our configuration
|
||||
|
||||
|
||||
void replacehost(char *oldhost, char *newhost);
|
||||
|
||||
/**
|
||||
* Return an open handle to the hosts file.
|
||||
*
|
||||
@ -46,6 +43,53 @@ FILE *fopenHostsFile(char *mode)
|
||||
return fopen(HOSTFILE, mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a host in the hosts file with the NEW host.
|
||||
*
|
||||
* @param oldhost
|
||||
* @param newhost
|
||||
* @param hostsFile
|
||||
*/
|
||||
void replacehost(char *oldhost, char *newhost, int deleteHost) {
|
||||
FILE *hostsFile = fopenHostsFile("r");
|
||||
char buf[256];
|
||||
char *ptr, *f;
|
||||
|
||||
char newHostsFile[4096];
|
||||
memset(newHostsFile, 0, 4096);
|
||||
|
||||
// fseek(hostsFile, 0, SEEK_SET);
|
||||
|
||||
while (fgets(buf, 256, hostsFile) != NULL)
|
||||
{
|
||||
f = strcasestr(buf, oldhost);
|
||||
if (f != NULL) {
|
||||
// Just skip writing this host to the hosts file if we are deleting it
|
||||
if (deleteHost)
|
||||
continue;
|
||||
memset(buf, 0, sizeof(buf));
|
||||
sprintf(buf, "%s %s\n", blockString, newhost);
|
||||
}
|
||||
strcat(newHostsFile, buf);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
// Ensure we have an EOF at the end of the file.
|
||||
newHostsFile[strlen(newHostsFile)] = EOF;
|
||||
|
||||
// // Seek to 0
|
||||
// fseek(hostsFile, 0, SEEK_SET);
|
||||
fclose(hostsFile);
|
||||
|
||||
// If this failed, write an error message to stderr
|
||||
// ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) {
|
||||
// fprintf(stderr, "Did not write anything!\n");
|
||||
// }
|
||||
fprintf(stderr, "New hosts file: \n%s\n", newHostsFile);
|
||||
// fclose(hostsFile);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Block a hosts using the hostsFile in FILE
|
||||
*/
|
||||
@ -65,9 +109,10 @@ void blockHost(char *host)
|
||||
|
||||
void deleteHost(char *host)
|
||||
{
|
||||
|
||||
replacehost(host, NULL, 1);
|
||||
}
|
||||
|
||||
|
||||
void showHosts()
|
||||
{
|
||||
pid_t child = fork();
|
||||
@ -251,45 +296,4 @@ int main(int argc, char **argv)
|
||||
free_list(&hosts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a host in the hosts file with the NEW host.
|
||||
*
|
||||
* @param oldhost
|
||||
* @param newhost
|
||||
* @param hostsFile
|
||||
*/
|
||||
void replacehost(char *oldhost, char *newhost) {
|
||||
FILE *hostsFile = fopenHostsFile("r");
|
||||
char buf[256];
|
||||
char *ptr, *f;
|
||||
|
||||
char newHostsFile[4096];
|
||||
memset(newHostsFile, 0, 4096);
|
||||
|
||||
// fseek(hostsFile, 0, SEEK_SET);
|
||||
|
||||
while (fgets(buf, 256, hostsFile) != NULL)
|
||||
{
|
||||
f = strcasestr(buf, oldhost);
|
||||
if (f != NULL) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
sprintf(buf, "%s %s\n", blockString, newhost);
|
||||
}
|
||||
strcat(newHostsFile, buf);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
}
|
||||
|
||||
// Ensure we have an EOF at the end of the file.
|
||||
newHostsFile[strlen(newHostsFile)] = EOF;
|
||||
|
||||
// // Seek to 0
|
||||
// fseek(hostsFile, 0, SEEK_SET);
|
||||
fclose(hostsFile);
|
||||
|
||||
// If this failed, write an error message to stderr
|
||||
// ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) {
|
||||
// fprintf(stderr, "Did not write anything!\n");
|
||||
// }
|
||||
fprintf(stderr, "New hosts file: \n%s\n", newHostsFile);
|
||||
// fclose(hostsFile);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user