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
|
#include <sys/types.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "util.h"
static time_t comparetime;
static char *line;
static size_t linesize;
static void
printfeed(FILE *fp, const char *feedname)
{
char *fields[FieldLast];
struct tm *tm;
time_t parsedtime;
ssize_t linelen;
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");
fputs("<entry>\n\t<title>", stdout);
if (feedname[0]) {
fputs("[", stdout);
xmlencode(feedname, stdout);
fputs("] ", stdout);
}
xmlencode(fields[FieldTitle], stdout);
fputs("</title>\n\t<link rel=\"alternate\" href=\"", stdout);
xmlencode(fields[FieldLink], stdout);
fputs("\" />\n", stdout);
if (fields[FieldEnclosure][0]) {
fputs("\t<link rel=\"enclosure\" href=\"", stdout);
xmlencode(fields[FieldEnclosure], stdout);
fputs("\" />\n", stdout);
}
fprintf(stdout, "\t<published>%04d-%02d-%02dT%02d:%02d:%02dZ</published>\n",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
if (fields[FieldAuthor][0]) {
fputs("\t<author><name>", stdout);
xmlencode(fields[FieldAuthor], stdout);
fputs("</name></author>\n", stdout);
}
fputs("</entry>\n", stdout);
}
}
int
main(int argc, char *argv[])
{
FILE *fp;
char *name;
int i;
if (argc == 1) {
if (pledge("stdio", NULL) == -1)
err(1, "pledge");
} else {
if (pledge("stdio rpath", NULL) == -1)
err(1, "pledge");
}
fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<feed xmlns=\"http://www.w3.org/2005/Atom\" xml:lang=\"en\">\n",
stdout);
if (argc == 1) {
printfeed(stdin, "");
} else {
for (i = 1; i < argc; i++) {
if (!(fp = fopen(argv[i], "r")))
err(1, "fopen: %s", argv[i]);
name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
printfeed(fp, name);
if (ferror(fp))
err(1, "ferror: %s", argv[i]);
fclose(fp);
}
}
fputs("</feed>\n", stdout);
return 0;
}
|