sfeed

Simple RSS and Atom feed parser
git clone https://git.sinitax.com/codemadness/sfeed
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

sfeed_twtxt.c (1569B)


      1#include <stdio.h>
      2#include <string.h>
      3#include <time.h>
      4
      5#include "util.h"
      6
      7static char *line;
      8static size_t linesize;
      9
     10static void
     11printfeed(FILE *fp, const char *feedname)
     12{
     13	char *fields[FieldLast];
     14	struct tm parsedtm, *tm;
     15	time_t parsedtime;
     16	ssize_t linelen;
     17
     18	while ((linelen = getline(&line, &linesize, fp)) > 0 &&
     19	       !ferror(stdout)) {
     20		if (line[linelen - 1] == '\n')
     21			line[--linelen] = '\0';
     22		parseline(line, fields);
     23
     24		parsedtime = 0;
     25		if (!strtotime(fields[FieldUnixTimestamp], &parsedtime) &&
     26		    (tm = gmtime_r(&parsedtime, &parsedtm))) {
     27			fprintf(stdout, "%04d-%02d-%02dT%02d:%02d:%02dZ\t",
     28			        tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
     29			        tm->tm_hour, tm->tm_min, tm->tm_sec);
     30		} else {
     31			fputs("\t", stdout);
     32		}
     33		if (feedname[0])
     34			printf("[%s] ", feedname);
     35		fputs(fields[FieldTitle], stdout);
     36		if (fields[FieldLink][0]) {
     37			fputs(": ", stdout);
     38			fputs(fields[FieldLink], stdout);
     39		}
     40		putchar('\n');
     41	}
     42}
     43
     44int
     45main(int argc, char *argv[])
     46{
     47	FILE *fp;
     48	char *name;
     49	int i;
     50
     51	if (pledge(argc == 1 ? "stdio" : "stdio rpath", NULL) == -1)
     52		err(1, "pledge");
     53
     54	if (argc == 1) {
     55		printfeed(stdin, "");
     56		checkfileerror(stdin, "<stdin>", 'r');
     57	} else {
     58		for (i = 1; i < argc; i++) {
     59			if (!(fp = fopen(argv[i], "r")))
     60				err(1, "fopen: %s", argv[i]);
     61			name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i];
     62			printfeed(fp, name);
     63			checkfileerror(fp, argv[i], 'r');
     64			checkfileerror(stdout, "<stdout>", 'w');
     65			fclose(fp);
     66		}
     67	}
     68
     69	checkfileerror(stdout, "<stdout>", 'w');
     70
     71	return 0;
     72}