1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#include <sys/stat.h>
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tree.h"
#include "util.h"
static char *line;
static size_t linesize;
static int changed, firsttime = 1;
static time_t comparetime;
struct line {
char *id;
char *link;
char *title;
time_t timestamp;
RB_ENTRY(line) entry;
};
int
linecmp(struct line *e1, struct line *e2)
{
int r;
if ((r = strcmp(e1->id, e2->id)))
return r;
else if ((r = strcmp(e1->title, e2->title)))
return r;
return strcmp(e1->link, e2->link);
}
RB_HEAD(linetree, line) head = RB_INITIALIZER(&head);
RB_GENERATE_STATIC(linetree, line, entry, linecmp)
/* remove old entries from the tree that won't be shown anyway. */
static void
gc(void)
{
struct line *line, *tmp;
RB_FOREACH_SAFE(line, linetree, &head, tmp) {
if (line->timestamp < comparetime) {
free(line->id);
free(line->link);
free(line->title);
RB_REMOVE(linetree, &head, line);
free(line);
}
}
}
static void
printfeed(FILE *fp, const char *feedname)
{
struct line *add, search;
char *fields[FieldLast];
ssize_t linelen;
time_t parsedtime;
struct tm *tm;
while ((linelen = getline(&line, &linesize, fp)) > 0) {
if (line[linelen - 1] == '\n')
line[--linelen] = '\0';
if (!parseline(line, fields))
break;
parsedtime = 0;
if (strtotime(fields[FieldUnixTimestamp], &parsedtime))
continue;
if (!(tm = localtime(&parsedtime)))
err(1, "localtime");
/* old news: skip */
if (parsedtime < comparetime)
continue;
search.id = fields[FieldId];
search.link = fields[FieldLink];
search.title = fields[FieldTitle];
search.timestamp = parsedtime;
if (RB_FIND(linetree, &head, &search))
continue;
changed = 1;
if (!(add = calloc(1, sizeof(*add))))
err(1, "calloc");
if (!(add->id = strdup(fields[FieldId])))
err(1, "strdup");
if (!(add->link = strdup(fields[FieldLink])))
err(1, "strdup");
if (!(add->title = strdup(fields[FieldTitle])))
err(1, "strdup");
add->timestamp = parsedtime;
RB_INSERT(linetree, &head, add);
if (feedname[0]) {
printutf8pad(stdout, feedname, 15, ' ');
fputs(" ", stdout);
}
fprintf(stdout, "%04d-%02d-%02d %02d:%02d ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min);
printutf8pad(stdout, fields[FieldTitle], 70, ' ');
printf(" %s\n", fields[FieldLink]);
}
}
int
main(int argc, char *argv[])
{
struct stat *stfiles, st;
char *name;
FILE *fp;
int i, slept = 0;
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
if (argc <= 1) {
fprintf(stderr, "usage: %s <file>...\n", argv[0]);
return 1;
}
setlocale(LC_CTYPE, "");
if (!(stfiles = calloc(argc - 1, sizeof(*stfiles))))
err(1, "calloc");
while (1) {
changed = 0;
if ((comparetime = time(NULL)) == -1)
err(1, "time");
/* 1 day is old news */
comparetime -= 86400;
for (i = 1; i < argc; i++) {
if (!(fp = fopen(argv[i], "r"))) {
if (firsttime)
err(1, "fopen: %s", argv[i]);
/* don't report when the file is missing after the first run */
continue;
}
if (fstat(fileno(fp), &st) == -1) {
if (firsttime)
err(1, "fstat: %s", argv[i]);
warn("fstat: %s", argv[i]);
fclose(fp);
continue;
}
/* did the file change? by size, modification */
if (stfiles[i - 1].st_size != st.st_size ||
stfiles[i - 1].st_mtime != st.st_mtime) {
name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
printfeed(fp, name);
if (ferror(fp))
warn("ferror: %s", argv[i]);
memcpy(&stfiles[i - 1], &st, sizeof(st));
}
fclose(fp);
}
/* "garbage collect" on a change or every 5 minutes */
if (changed || slept > 300) {
gc();
changed = 0;
slept = 0;
}
sleep(1);
slept++;
firsttime = 0;
}
return 0;
}
|