]> git.scottworley.com Git - uniqt/blob - uniqt.c
b755f4eb9def577608ff1bd738a6489b12b38fb0
[uniqt] / uniqt.c
1 #define _POSIX_C_SOURCE 200809L
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <time.h>
7
8 void die(const char *message) {
9 fputs(message, stderr);
10 fputc('\n', stderr);
11 exit(1);
12 }
13
14 void die_err(const char *message) {
15 perror(message);
16 exit(1);
17 }
18
19 char *encode_time(time_t t) {
20 struct tm tm;
21 localtime_r(&t, &tm);
22 const size_t size = 20;
23 char *out = (char *)malloc(size);
24 if (strftime(out, size, "%Y %m %d %H %M %S", &tm) != size - 1)
25 die("Couldn't format time");
26 return out;
27 }
28
29 typedef struct {
30 time_t start, end;
31 } time_range_t;
32
33 const time_t NULL_TIME = (time_t)-1;
34
35 static time_range_t make_time_range(time_t t) { return (time_range_t){t, t}; }
36
37 static void extend_time_range(time_range_t *r, time_t t) { r->end = t; }
38
39 static char *read_line() {
40 char *line = NULL;
41 int scanf_ret = scanf("%m[^\n]", &line);
42 if (scanf_ret == EOF && ferror(stdin))
43 die_err("Error reading");
44 int newline = getchar();
45 if (newline != EOF && newline != (int)'\n')
46 die("Expected newline");
47 if (scanf_ret == 0 && newline == (int)'\n')
48 return strdup("\n");
49 return line;
50 }
51
52 static void write_line(time_range_t *range, char *line) {
53 if (line == NULL)
54 return;
55 if (printf("%ld %ld %s\n", range->start, range->end, line) < 0)
56 die("Couldn't write");
57 }
58
59 static int same(char *a, char *b) { return a && b && strcmp(a, b) == 0; }
60
61 static void uniqt() {
62 char *current_line = NULL;
63 time_range_t current_time_range = make_time_range(NULL_TIME);
64 for (;;) {
65 char *new_line = read_line();
66 if (new_line == NULL)
67 break;
68 time_t now = time(NULL);
69 if (same(current_line, new_line)) {
70 free(new_line);
71 extend_time_range(&current_time_range, now);
72 } else {
73 write_line(&current_time_range, current_line);
74 free(current_line);
75 current_line = new_line;
76 current_time_range = make_time_range(now);
77 }
78 }
79 write_line(&current_time_range, current_line);
80 free(current_line);
81 }
82
83 int main() {
84 uniqt();
85 return 0;
86 }