summaryrefslogtreecommitdiff
path: root/sfeed_twtxt.c
diff options
context:
space:
mode:
authorHiltjo Posthuma <hiltjo@codemadness.org>2018-08-22 16:23:52 +0200
committerHiltjo Posthuma <hiltjo@codemadness.org>2018-08-22 16:24:42 +0200
commit6446070da557bf8b56fa44b2bbdc4690edf490a9 (patch)
tree5432ec8fe24b30e461e9e2f07c2cbec842e6ce67 /sfeed_twtxt.c
parent68a48bea8231afdf3f5e533a07bffeb1dc39f875 (diff)
add sfeed_twtxt format program: format to a twtxt feed
Diffstat (limited to 'sfeed_twtxt.c')
-rw-r--r--sfeed_twtxt.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/sfeed_twtxt.c b/sfeed_twtxt.c
new file mode 100644
index 0000000..5df0fa1
--- /dev/null
+++ b/sfeed_twtxt.c
@@ -0,0 +1,76 @@
+#include <ctype.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+#include "util.h"
+
+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 = gmtime(&parsedtime)))
+ err(1, "localtime");
+
+ fprintf(stdout, "%04d-%02d-%02dT%02d:%02d:%02dZ\t",
+ tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+ tm->tm_hour, tm->tm_min, tm->tm_sec);
+ if (feedname[0])
+ printf("[%s] ", feedname);
+ fputs(fields[FieldTitle], stdout);
+ if (fields[FieldLink][0]) {
+ fputs(" @<", stdout);
+ if (fields[FieldAuthor][0]) {
+ fputs(fields[FieldAuthor], stdout);
+ fputs(" ", stdout);
+ }
+ fputs(fields[FieldLink], stdout);
+ fputs(">", stdout);
+ }
+ putchar('\n');
+ }
+}
+
+int
+main(int argc, char *argv[])
+{
+ FILE *fp;
+ char *name;
+ int i;
+
+ if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
+ err(1, "pledge");
+
+ 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);
+ }
+ }
+ return 0;
+}