aboutgitcodelistschat:MatrixIRC
path: root/eater/eater.c
diff options
context:
space:
mode:
authorAlice Frosi <afrosi@redhat.com>2023-04-18 16:25:15 +0200
committerAlice Frosi <afrosi@redhat.com>2023-04-20 14:52:53 +0200
commit26e87bc6131edf317ad8f1248652233ebf7e6ded (patch)
tree5899e448c9d90c6b10a9375613fd6316b0344bba /eater/eater.c
parentf8f638bccc8b49917a1886f1961af7dc553166ef (diff)
downloadseitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar.gz
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar.bz2
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar.lz
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar.xz
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.tar.zst
seitan-26e87bc6131edf317ad8f1248652233ebf7e6ded.zip
Replace argp with getopt
Diffstat (limited to 'eater/eater.c')
-rw-r--r--eater/eater.c71
1 files changed, 37 insertions, 34 deletions
diff --git a/eater/eater.c b/eater/eater.c
index 3396c78..0236637 100644
--- a/eater/eater.c
+++ b/eater/eater.c
@@ -14,61 +14,64 @@
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
+#include <getopt.h>
#include <fcntl.h>
#include <unistd.h>
-#include <argp.h>
#include <signal.h>
#include <dirent.h>
#include <sys/stat.h>
#include "common.h"
+static struct option options[] = {
+ { "input", required_argument, NULL, 'i' },
+ { 0, 0, 0, 0 },
+};
extern char **environ;
-static char doc[] =
- "Usage: seitan-eater: setain-eater -i <input file> -- program args1 args2...";
-
/* Eater options */
-static struct argp_option options[] = { { "input", 'i', "FILE", 0,
- "BPF filter input file", 0 },
- { 0 } };
-
struct arguments {
char *input_file;
unsigned int program_index;
};
-static error_t parse_opt(int key, char *arg, struct argp_state *state)
+static void usage()
{
- struct arguments *arguments = state->input;
+ printf("seitan-eater: installer for the BPF filter before launching the process\n"
+ "Example: seitan-eater: setain-eater -i <input file> -- program args1 args2...;\n"
+ "Usage:\n"
+ "\t-i, --input:\tAction input file\n");
+ exit(EXIT_FAILURE);
+}
- if (state->quoted == 0)
- arguments->program_index = state->next + 1;
- switch (key) {
- case 'i':
- if (state->quoted == 0)
- arguments->input_file = arg;
- break;
- case ARGP_KEY_END:
- if (arguments->input_file == NULL)
- argp_error(state, "missing input file");
- if (state->argv[arguments->program_index] == NULL)
- argp_error(state, "missing program");
- break;
+static void parse(int argc, char **argv, struct arguments *arguments)
+{
+ int index = 0;
+ int oc;
+
+ arguments->program_index = 0;
+ while ((oc = getopt_long(argc, argv, "i:", options, &index)) != -1) {
+ switch (oc) {
+ case 'i':
+ arguments->input_file = optarg;
+ break;
+ case '?':
+ fprintf(stderr, "unknow option %c\n", optopt);
+ usage();
+ }
+ }
+ arguments->program_index = optind;
+ if (arguments->input_file == NULL) {
+ fprintf(stderr, "missing input file\n");
+ usage();
+ }
+ if (strcmp(argv[optind - 1], "--") != 0) {
+ fprintf(stderr, "missing program\n");
+ usage();
}
-
- return 0;
}
-static struct argp argp = { .options = options,
- .parser = parse_opt,
- .args_doc = NULL,
- .doc = doc,
- .children = NULL,
- .help_filter = NULL,
- .argp_domain = NULL };
-
static void signal_handler(__attribute__((unused)) int s)
{
}
@@ -88,7 +91,7 @@ int main(int argc, char **argv)
int fd, flags;
size_t n;
- argp_parse(&argp, argc, argv, 0, 0, &arguments);
+ parse(argc, argv, &arguments);
fd = open(arguments.input_file, O_CLOEXEC | O_RDONLY);
n = read(fd, filter, sizeof(filter));
close(fd);