sfeed/sfeed_plain.c

89 lines
1.6 KiB
C
Raw Normal View History

#include <ctype.h>
2015-06-20 22:24:30 +00:00
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include "util.h"
2015-06-20 22:24:30 +00:00
static time_t comparetime;
static char *line = NULL;
static size_t size = 0;
/* print `len' columns of characters. If string is shorter pad the rest
* with characters `pad`. */
static void
printutf8pad(FILE *fp, const char *s, size_t len, int pad)
{
wchar_t w;
size_t n = 0, i;
int r;
for (i = 0; *s && n < len; i++, s++) {
if (ISUTF8(*s)) {
if ((r = mbtowc(&w, s, 4)) == -1)
break;
if ((r = wcwidth(w)) == -1)
r = 1;
n += (size_t)r;
}
putc(*s, fp);
}
for (; n < len; n++)
putc(pad, fp);
}
2015-06-20 22:24:30 +00:00
static void
printfeed(FILE *fp, const char *feedname)
{
2015-06-20 22:24:30 +00:00
char *fields[FieldLast];
time_t parsedtime;
while (parseline(&line, &size, fields, fp) > 0) {
parsedtime = 0;
strtotime(fields[FieldUnixTimestamp], &parsedtime);
if (parsedtime >= comparetime)
fputs("N ", stdout);
else
fputs(" ", stdout);
2015-06-20 22:24:30 +00:00
2015-07-28 19:56:46 +00:00
if (feedname[0])
2015-06-20 22:24:30 +00:00
printf("%-15.15s ", feedname);
printf(" %-30.30s ", fields[FieldTimeFormatted]);
2014-11-11 18:11:56 +00:00
printutf8pad(stdout, fields[FieldTitle], 70, ' ');
printf(" %s\n", fields[FieldLink]);
}
2015-06-20 22:24:30 +00:00
}
int
main(int argc, char *argv[])
{
FILE *fp;
char *name;
2015-06-20 22:24:30 +00:00
int i;
2015-07-30 14:09:54 +00:00
if ((comparetime = time(NULL)) == -1)
err(1, "time");
2015-06-20 22:24:30 +00:00
/* 1 day is old news */
2015-07-30 14:09:54 +00:00
comparetime -= 86400;
2015-06-20 22:24:30 +00:00
2015-07-28 19:56:46 +00:00
if (argc == 1) {
2015-06-20 22:24:30 +00:00
printfeed(stdin, "");
} else {
2015-07-28 19:56:46 +00:00
for (i = 1; i < argc; i++) {
if (!(fp = fopen(argv[i], "r")))
2015-06-20 22:24:30 +00:00
err(1, "fopen: %s", argv[i]);
name = xbasename(argv[i]);
printfeed(fp, name);
free(name);
2015-07-28 19:56:46 +00:00
if (ferror(fp))
2015-06-20 22:24:30 +00:00
err(1, "ferror: %s", argv[i]);
fclose(fp);
}
}
return 0;
}