Macro for command line argument handling
This commit is contained in:
parent
e5f4dd5637
commit
bdca3cf572
48
main.c
48
main.c
@ -12,6 +12,8 @@
|
|||||||
// Macro for status checks.
|
// Macro for status checks.
|
||||||
#define ONFAILED(status, fn) if(status > fn)
|
#define ONFAILED(status, fn) if(status > fn)
|
||||||
|
|
||||||
|
#define ARG_IS(argname) (strcmp(argv[i], argname) == 0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: Add config file reading for daemonization.
|
* TODO: Add config file reading for daemonization.
|
||||||
* TODO: Allow daemonized process to call file reading/editing routines at will.
|
* TODO: Allow daemonized process to call file reading/editing routines at will.
|
||||||
@ -23,6 +25,7 @@
|
|||||||
// Our rule we use to blackhole domains
|
// Our rule we use to blackhole domains
|
||||||
const char *blockString = "0.0.0.0 ";
|
const char *blockString = "0.0.0.0 ";
|
||||||
LinkedList *hosts = NULL;
|
LinkedList *hosts = NULL;
|
||||||
|
int HB_PERIOD = 60;
|
||||||
|
|
||||||
// The current hardcoded location of the hosts file.
|
// The current hardcoded location of the hosts file.
|
||||||
static char *HOSTFILE = "/etc/hosts";
|
static char *HOSTFILE = "/etc/hosts";
|
||||||
@ -131,7 +134,7 @@ int read_config_file() {
|
|||||||
// overwrite data in these nodes.
|
// overwrite data in these nodes.
|
||||||
if (ptr != NULL && strcmp(buf, ptr->data) != 0) {
|
if (ptr != NULL && strcmp(buf, ptr->data) != 0) {
|
||||||
if (strlen(ptr->data) < strlen(buf)) {
|
if (strlen(ptr->data) < strlen(buf)) {
|
||||||
if ((ptr->data = realloc(ptr->data, strlen(buf))) < 0) {
|
if ((ptr->data = realloc(ptr->data, strlen(buf))) == NULL) {
|
||||||
fprintf(stderr, "Failed to realloc pointer\n");
|
fprintf(stderr, "Failed to realloc pointer\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
@ -146,32 +149,12 @@ int read_config_file() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Daemonize the process, to be run like this ONLY if there were no arguments
|
* Wake up on SIGALRM to do our thing.
|
||||||
* provided.
|
|
||||||
*
|
|
||||||
* TODO:
|
|
||||||
* read the config file.
|
|
||||||
*/
|
*/
|
||||||
void daemonize() {
|
void run_loop(int signal) {
|
||||||
pid_t mypid = fork();
|
read_config_file();
|
||||||
if (mypid != 0) {
|
|
||||||
// I am the parent, kill myself
|
|
||||||
fprintf(stderr, "Parent exiting");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
alarm(HB_PERIOD);
|
||||||
// DO THE THING.
|
|
||||||
while(1) {
|
|
||||||
// read config
|
|
||||||
read_config_file();
|
|
||||||
break;
|
|
||||||
// make adjustments
|
|
||||||
|
|
||||||
// profit??
|
|
||||||
// JK, sleep.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -189,7 +172,7 @@ int main(int argc, char **argv)
|
|||||||
for (int i = 0; i < argc; i++)
|
for (int i = 0; i < argc; i++)
|
||||||
{
|
{
|
||||||
// Opens a hosts file in append mode, adding a host.
|
// Opens a hosts file in append mode, adding a host.
|
||||||
if (strcmp(argv[i], "add") == 0)
|
if (ARG_IS("add"))
|
||||||
{
|
{
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
printf("Please provide a host!\n");
|
printf("Please provide a host!\n");
|
||||||
@ -199,30 +182,33 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
// 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 (ARG_IS("edit"))
|
||||||
{
|
{
|
||||||
replacehost(argv[++i], argv[++i]);
|
replacehost(argv[++i], argv[++i]);
|
||||||
}
|
}
|
||||||
// Deletes a host.
|
// Deletes a host.
|
||||||
else if (strcmp(argv[i], "delete") == 0)
|
else if (ARG_IS("delete"))
|
||||||
{
|
{
|
||||||
fprintf(stdout, "Soon to be implemented!\n");
|
fprintf(stdout, "Soon to be implemented!\n");
|
||||||
}
|
}
|
||||||
// Shows usage.
|
// Shows usage.
|
||||||
else if (strcmp(argv[i], "-h") == 0)
|
else if (ARG_IS("-h"))
|
||||||
{
|
{
|
||||||
usage();
|
usage();
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
// Show the entire hosts file.
|
// Show the entire hosts file.
|
||||||
else if (strcmp(argv[i], "show") == 0)
|
else if (ARG_IS("show"))
|
||||||
{
|
{
|
||||||
showHosts();
|
showHosts();
|
||||||
}
|
}
|
||||||
else if (strcmp(argv[i], "-config") == 0) {
|
else if (ARG_IS("-config")) {
|
||||||
CONFIG = argv[++i];
|
CONFIG = argv[++i];
|
||||||
read_config_file();
|
read_config_file();
|
||||||
}
|
}
|
||||||
|
else if (ARG_IS("-period")) {
|
||||||
|
HB_PERIOD = atoi(argv[++i]);
|
||||||
|
}
|
||||||
|
|
||||||
// Daemonize this process, allowing for hosts file to be automagically managed.
|
// Daemonize this process, allowing for hosts file to be automagically managed.
|
||||||
else {
|
else {
|
||||||
|
Loading…
Reference in New Issue
Block a user