sfeed

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

sfeed_plain.c (1914B)


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