]> git.scottworley.com Git - uniqt/blob - uniqt.c
2b7d229ac94e152d0644877404ec8426c3b5390c
[uniqt] / uniqt.c
1 #define _POSIX_C_SOURCE 199309L
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 return line;
48 }
49
50 static void write_line(time_range_t *range, char *line) {
51 if (line == NULL)
52 return;
53 if (printf("%ld %ld %s\n", range->start, range->end, line) < 0)
54 die("Couldn't write");
55 }
56
57 static int same(char *a, char *b) { return a && b && strcmp(a, b) == 0; }
58
59 static void uniqt() {
60 char *current_line = NULL;
61 time_range_t current_time_range = make_time_range(NULL_TIME);
62 for (;;) {
63 char *new_line = read_line();
64 if (new_line == NULL)
65 break;
66 time_t now = time(NULL);
67 if (same(current_line, new_line)) {
68 free(new_line);
69 extend_time_range(&current_time_range, now);
70 } else {
71 write_line(&current_time_range, current_line);
72 free(current_line);
73 current_line = new_line;
74 current_time_range = make_time_range(now);
75 }
76 }
77 write_line(&current_time_range, current_line);
78 free(current_line);
79 }
80
81 int main() {
82 uniqt();
83 return 0;
84 }