-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
102 lines (89 loc) · 2.53 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arguments.h"
#include "hosts_file.h"
#include "commands.h"
static int MAX_ARGS = 10;
static const char* DEFAULT_FILENAME = "/etc/hosts";
typedef struct command_struct {
const char *name;
int (*function)(HostsFile*, Arguments);
int numArgs;
} Command;
static void printVersion() {
printf("hosts v1.0.0 (c) 2013 Ulrich Schmidt-Goertz\n");
}
static void printUsage() {
printVersion();
printf("\nUsage: hosts [command] [-f <filename> | -h | -v] [arguments]\n\n");
printf("Available commands:\n");
printf(" list Print the list of entries in the hosts file\n");
printf(" add Adds an entry to the hosts file. Arguments: [hostname] [ip-address]\n");
printf(" rm Removes one or more entries from the hosts file. Arguments: [hostname]...\n\n");
printf("Available options:\n");
printf(" -f <filename> Chooses the file to operate on (default: /etc/hosts)\n");
printf(" -h Prints this help text\n");
printf(" -v Prints version information\n");
}
static int handleCommand(const char *command, Arguments arguments) {
static Command commands[] = {
{ "list", cmd_list, 0 },
{ "add", cmd_add, 2 },
{ "rm", cmd_rm, 1 }
};
HostsFile *hostsFile = readHostsFile(arguments.filename);
for (int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
if (strcmp(command, commands[i].name) == 0) {
if (arguments.size < commands[i].numArgs) {
fprintf(stderr, "Error: command %s needs %d argument(s)\n", commands[i].name, commands[i].numArgs);
exit(EXIT_FAILURE);
}
return commands[i].function(hostsFile, arguments);
}
}
printUsage();
return 1;
}
int main(int argc, const char **argv) {
if (argc < 2) {
printUsage();
return 1;
}
Arguments arguments;
arguments.filename = DEFAULT_FILENAME;
arguments.values = (const char**)malloc(MAX_ARGS*sizeof(char*));
int nonopt = 0;
const char *command = "";
for (int i = 1; i < argc; ++i) {
const char *argument = argv[i];
if (argument[0] == '-') {
switch (argument[1]) {
case 'h':
printUsage();
exit(EXIT_SUCCESS);
case 'v':
printVersion();
exit(EXIT_SUCCESS);
case 'f':
arguments.filename = argv[++i];
break;
default:
printUsage();
exit(EXIT_FAILURE);
}
} else {
if (i == 1) {
command = argument;
} else {
if (nonopt >= MAX_ARGS-1) {
fputs("Error: Too many arguments", stderr);
exit(EXIT_FAILURE);
}
arguments.values[nonopt++] = argument;
}
}
}
arguments.size = nonopt;
return handleCommand(command, arguments);
}