sfeed

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

sfeed_plain.c (1858B)


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