-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.c
66 lines (61 loc) · 1.61 KB
/
options.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
#include <errno.h>
#include <error.h>
#include <getopt.h>
#include <stdio.h>
#include <string.h>
#include "options.h"
FILE *input;
int handleOptions(int argc, char *argv[]) {
int c;
struct option long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "version", no_argument, NULL, 'v' }
};
while (1) {
c = getopt_long(argc, argv, "hv", long_options, NULL);
if (c == -1) {
break;
}
switch(c) {
case 'h':
printf("Usage: %s [FILE]\n", argv[0]);
return 0;
case 'v':
printf(
"ja2l (JSON array to lines) 0.1\n"
"Copyright © 2017 Lucas Werkmeister\n"
"License AGPLv3+: GNU AGPL version 3 or later <https://gnu.org/licenses/agpl.html>\n"
"\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"This program includes code from the systemd project,\n"
"published under the LGPLv2.1+:\n"
"GNU LGPL version 2.1 or later <https://gnu.org/licenses/lgpl.html>\n"
);
return 0;
case '?':
return 1;
}
}
if (optind < argc) {
if (argc == optind + 1) {
if (strcmp(argv[optind], "-") == 0) {
input = stdin;
} else {
input = fopen(argv[optind], "r");
if (input == NULL) {
error(0, errno, "fopen(%s)", argv[optind]);
return 1;
}
}
} else {
error(0, 0, "too many arguments");
fprintf(stderr, "Try ‘%s --help’ for more information.\n", argv[0]);
return 1;
}
} else {
input = stdin;
}
return -1;
}