From ad183ca49b89ea7eef84872b30bebdcaf003f535 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 29 Aug 2021 10:12:21 -0400 Subject: Move concatenation function --- (limited to 'src/datetime.c') diff --git a/src/datetime.c b/src/datetime.c index 8597ea2..d01f5e1 100644 --- a/src/datetime.c +++ b/src/datetime.c @@ -19,12 +19,9 @@ #define _XOPEN_SOURCE -#include #include -#include #include #include -#include #include #include "datetime.h" @@ -33,73 +30,31 @@ static const char *DATETIME_FMTS_[] = { NULL }; -static char * -datetime_concat_args(int argc, const char *argv[]) -{ - int buf_l; - int i; - char *buf; - int buf_i; - int j; - - buf_l = 0; - for (i = 0; i < argc; ++i) { - buf_l += strlen(argv[i]) + 1; - } - - buf = calloc(buf_l, sizeof(*buf)); - if (buf == NULL) { - fprintf(stderr, "Failed to allocate buffer: %s\n", - strerror(errno)); - return NULL; - } - - buf_i = 0; - for (i = 0; i < argc; ++i) { - for (j = 0; argv[i][j] != '\0'; ++j) { - buf[buf_i++] = argv[i][j]; - } - buf[buf_i++] = ' '; - } - buf[--buf_i] = '\0'; - - return buf; -} - int -datetime_parse(int argc, const char *argv[], time_t *arg_sec) +datetime_parse(const char *input, time_t *arg_sec) { - char *buf; int i; char *end; struct tm arg_tm; time_t now_sec; struct tm *now_tm; - buf = datetime_concat_args(argc, argv); - if (buf == NULL) { - return -1; - } - arg_tm.tm_mday = INT_MIN; /* Sentinel */ arg_tm.tm_sec = 0; /* Default */ arg_tm.tm_isdst = -1; for (i = 0; DATETIME_FMTS_[i] != NULL; ++i) { - /* printf("%s =~ %s\n", buf, DATETIME_FMTS_[i]); */ - end = strptime(buf, DATETIME_FMTS_[i], &arg_tm); + /* printf("%s =~ %s\n", input, DATETIME_FMTS_[i]); */ + end = strptime(input, DATETIME_FMTS_[i], &arg_tm); if (end != NULL && *end == '\0') { goto found; } } - free(buf); fprintf(stderr, "Unknown date format\n"); return -1; found: - free(buf); - /* TODO: Support %a-only dates and optional years */ if (arg_tm.tm_mday == INT_MIN) { /* No date specified; try today. */ -- cgit v0.9.1