sfeed

simple feed reader - forked from git.codemadness.org/sfeed
git clone git://src.gearsix.net/sfeed
Log | Files | Refs | Atom | README | LICENSE

sfeed_twtxt.c (1593B)


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