diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/arg.c | 64 | ||||
-rw-r--r-- | src/lib/util.c | 2 |
2 files changed, 65 insertions, 1 deletions
diff --git a/src/lib/arg.c b/src/lib/arg.c new file mode 100644 index 0000000..6178156 --- /dev/null +++ b/src/lib/arg.c @@ -0,0 +1,64 @@ +#include "arg.h" +#include "util.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +struct config conf; +void argerr(char *r, char *arg) { + fprintf(stderr, "lat: %s '%s'\n", r, arg); + exit(EXIT_FAILURE); +} + +void parselongarg(char *arg) { + if (strcmp(arg, "--color") == 0) { + conf.color = !conf.color; + return; + } + + if (strcmp(arg, "--lines") == 0) { + conf.lines = !conf.lines; + return; + } + + argerr("unrecognized arg", arg); +} + +void parseshortarg(char *arg) { + size_t i = 1; + while (arg[i] != '\0') { + char c = arg[i]; + switch (c) { + case 'c': + conf.color = !conf.color; + break; + case 'n': + conf.lines = !conf.lines; + break; + default: + argerr("unrecognized flag", &c); + break; + } + i++; + } +} + +int parseargs(int argc, char *argv[]) { + int i; + for (i = 1; i < argc; i++) { // offset for argv[0] + char *arg = argv[i]; + + if (arg[0] == '-') { + if (arg[1] == '-') { + parselongarg(arg); + continue; + } + + parseshortarg(arg); + } else { + return i; + } + } + + return i; +} diff --git a/src/lib/util.c b/src/lib/util.c index d2d4f6a..d66579d 100644 --- a/src/lib/util.c +++ b/src/lib/util.c @@ -4,7 +4,7 @@ void die(const char *message) { perror(message); - exit(1); + exit(EXIT_FAILURE); } char *formatbytes(size_t bytes, float *rounded) { |