/* * uniqt: uniq -c, but for time * Copyright (C) 2023 Scott Worley * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #define _POSIX_C_SOURCE 200809L #include #include #include #include void die(const char *message) { fputs(message, stderr); fputc('\n', stderr); exit(1); } void die_err(const char *message) { perror(message); exit(1); } char *encode_time(time_t t) { struct tm tm; localtime_r(&t, &tm); const size_t size = 20; char *out = (char *)malloc(size); if (strftime(out, size, "%Y %m %d %H %M %S", &tm) != size - 1) die("Couldn't format time"); return out; } typedef struct { char *time_format; } conf_t; typedef struct { time_t start, end; } time_range_t; const time_t NULL_TIME = (time_t)-1; static time_range_t make_time_range(time_t t) { return (time_range_t){t, t}; } static void extend_time_range(time_range_t *r, time_t t) { r->end = t; } static char *read_line() { char *line = NULL; int scanf_ret = scanf("%m[^\n]", &line); if (scanf_ret == EOF && ferror(stdin)) die_err("Error reading"); int newline = getchar(); if (newline != EOF && newline != (int)'\n') die("Expected newline"); if (scanf_ret == 0 && newline == (int)'\n') return strdup("\n"); return line; } static void format_time(conf_t *conf, char *buf, size_t size, time_t *t) { struct tm *tm = localtime(t); if (tm == NULL) die_err("Couldn't unpack time"); if (strftime(buf, size, conf->time_format, tm) == 0) die_err("Couldn't format time"); } static void write_line(conf_t *conf, time_range_t *range, char *line) { if (line == NULL) return; if (conf->time_format) { const size_t MAX_TIMESTAMP_LENGTH = 1024; char a[MAX_TIMESTAMP_LENGTH]; char b[MAX_TIMESTAMP_LENGTH]; format_time(conf, a, MAX_TIMESTAMP_LENGTH, &range->start); format_time(conf, b, MAX_TIMESTAMP_LENGTH, &range->end); if (printf("%s %s %s\n", a, b, line) < 0) die("Couldn't write"); } else { if (printf("%ld %ld %s\n", range->start, range->end, line) < 0) die("Couldn't write"); } if (fflush(stdout) == EOF) die_err("Couldn't flush"); } static int same(char *a, char *b) { return a && b && strcmp(a, b) == 0; } static void uniqt(conf_t *conf) { char *current_line = NULL; time_range_t current_time_range = make_time_range(NULL_TIME); for (;;) { char *new_line = read_line(); if (new_line == NULL) break; time_t now = time(NULL); if (same(current_line, new_line)) { free(new_line); extend_time_range(¤t_time_range, now); } else { write_line(conf, ¤t_time_range, current_line); free(current_line); current_line = new_line; current_time_range = make_time_range(now); } } write_line(conf, ¤t_time_range, current_line); free(current_line); } void usage() { die("usage: uniqt [-f time_format]"); } conf_t parse_command_line(int argc, char *argv[]) { conf_t conf; conf.time_format = NULL; for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-f") == 0) { if (i + 1 >= argc) die("-f requires an argument"); i++; conf.time_format = argv[i]; } else usage(); } return conf; } int main(int argc, char *argv[]) { conf_t conf = parse_command_line(argc, argv); uniqt(&conf); return 0; }