commit 8e1c912636a89f60749f511d525f7d7891f31862
parent d21cddd34b838976ff3583f3c85d34f34346b8d6
Author: William Casarin <jb55@jb55.com>
Date: Fri, 9 Oct 2020 11:42:29 -0700
usage
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
3 files changed, 71 insertions(+), 7 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,9 +1,14 @@
-CFLAGS = -Wall -Werror -std=c99
+CFLAGS = -g -Wall -Werror -std=c99
+PREFIX ?= /usr
datefmt: datefmt.c
$(CC) $(CFLAGS) $< -o $@
+install: datefmt
+ mkdir -p $(PREFIX)/bin
+ cp datefmt $(PREFIX)/bin
+
clean: fake
rm -f datefmt
diff --git a/datefmt.c b/datefmt.c
@@ -3,6 +3,9 @@
#include <ctype.h>
#include <inttypes.h>
#include <time.h>
+#include <string.h>
+
+#define VERSION "0.1"
enum state {
BEGIN,
@@ -145,14 +148,70 @@ static void parser_init(struct parser *parser)
parser->format = "%F %R";
}
-int main(int argc, const char *argv[])
+static void shift_arg(int *argc, char **argv)
{
- struct parser parser;
- parser_init(&parser);
+ if (*argc < 1)
+ return;
+ memmove(argv, &argv[1], ((*argc)-1) * sizeof(char*));
+ *argc = *argc - 1;
+}
+
+static void usage() {
+ printf("usage: datefmt [OPTION...] [FORMAT]\n\n");
+ printf("format unix timestamps from stdin\n\n");
+ printf(" -a, --after <timestamp> only format timestamps after this date \n");
+ printf(" --version display version information and exit \n");
+
+ printf("\n FORMAT\n a strftime format string, defaults to '%%F %%R'\n");
- if (argc == 2) {
- parser.format = argv[1];
+ printf("\n EXAMPLE\n datefmt --after $(date -d yesterday +%%s) < spreadsheet.csv\n\n");
+ printf(" Created By: William Casarin <https://jb55.com>\n");
+ exit(0);
+}
+
+static void parse_arg(int *argc, char **argv, struct parser *parser)
+{
+ char *endptr;
+
+ if (!strcmp(argv[0], "--after") || !strcmp(argv[0], "-a")) {
+ shift_arg(argc, argv);
+ if (*argc > 0) {
+ parser->after = strtoll(argv[0], &endptr, 10);
+ if (endptr == argv[0]) {
+ printf("error: invalid after value '%s'\n", argv[0]);
+ exit(1);
+ }
+ shift_arg(argc, argv);
+ } else {
+ printf("error: expected argument to --after\n");
+ exit(1);
+ }
+ } else if (!strcmp("--help", argv[0])) {
+ usage();
+ } else if (!strcmp("--version", argv[0])) {
+ printf("datefmt " VERSION "\n");
+ exit(0);
+ } else {
+ parser->format = (const char*)argv[0];
+ shift_arg(argc, argv);
}
+}
+
+static void parse_args(int argc, char **argv, struct parser *parser)
+{
+ shift_arg(&argc, argv);
+
+ while (argc > 0) {
+ parse_arg(&argc, argv, parser);
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ struct parser parser;
+
+ parser_init(&parser);
+ parse_args(argc, argv, &parser);
do {
parser.len = fread(parser.buf, 1, READSIZE, stdin);
diff --git a/default.nix b/default.nix
@@ -2,5 +2,5 @@
with pkgs;
stdenv.mkDerivation {
name = "datefmt";
- nativeBuildInputs = [ ];
+ nativeBuildInputs = [ gdb ];
}