sfeed

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

commit e5b0d4889556257a15027e729233e0c5d7e25d30
parent f945d2efff418cf5fa108790ca3adef4796065ef
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Thu,  4 Jul 2019 21:56:31 +0200

sfeed_gph: improvements

- pledge(2) sooner.
- Don't use getenv when it has no arguments and reads only from stdin.
  prefixpath is unused in that case.
- Check for path truncation or an encoding error. No need to format the same
  path twice.
- Use a fixed feed structure, no need for memory allocation in this format
  program.
- Remove "totalnew" of all feeds. It was unused.

Diffstat:
Msfeed_gph.c | 43++++++++++++++++---------------------------
1 file changed, 16 insertions(+), 27 deletions(-)

diff --git a/sfeed_gph.c b/sfeed_gph.c @@ -15,12 +15,11 @@ #include "util.h" -static struct feed **feeds; +static struct feed f; static char *prefixpath; static char *line; static size_t linesize; static time_t comparetime; -static unsigned long totalnew; /* Escape characters in links in geomyidae .gph format */ void @@ -71,7 +70,6 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) err(1, "localtime"); isnew = (parsedtime >= comparetime) ? 1 : 0; - totalnew += isnew; f->totalnew += isnew; f->total++; @@ -97,49 +95,41 @@ main(int argc, char *argv[]) { FILE *fpitems, *fpindex, *fp; char *name, path[PATH_MAX + 1]; - int i; - struct feed *f; + int i, r; - if (pledge("stdio rpath wpath cpath", NULL) == -1) + if (pledge(argc == 1 ? "stdio" : "stdio rpath wpath cpath", NULL) == -1) err(1, "pledge"); - if (!(prefixpath = getenv("SFEED_GPH_PATH"))) - prefixpath = "/"; - - if (!(feeds = calloc(argc, sizeof(struct feed *)))) - err(1, "calloc"); - if ((comparetime = time(NULL)) == -1) err(1, "time"); /* 1 day is old news */ comparetime -= 86400; if (argc == 1) { - if (pledge("stdio", NULL) == -1) - err(1, "pledge"); - if (!(feeds[0] = calloc(1, sizeof(struct feed)))) - err(1, "calloc"); - feeds[0]->name = ""; - printfeed(stdout, stdin, feeds[0]); + f.name = ""; + printfeed(stdout, stdin, &f); } else { + if (!(prefixpath = getenv("SFEED_GPH_PATH"))) + prefixpath = "/"; + /* write main index page */ if (!(fpindex = fopen("index.gph", "wb"))) err(1, "fopen: index.gph"); for (i = 1; i < argc; i++) { - if (!(feeds[i - 1] = calloc(1, sizeof(struct feed)))) - err(1, "calloc"); - f = feeds[i - 1]; + memset(&f, 0, sizeof(f)); name = ((name = strrchr(argv[i], '/'))) ? name + 1 : argv[i]; - f->name = name; + f.name = name; if (!(fp = fopen(argv[i], "r"))) err(1, "fopen: %s", argv[i]); - snprintf(path, sizeof(path), "%s.gph", f->name); + r = snprintf(path, sizeof(path), "%s.gph", name); + if (r < 0 || (size_t)r >= sizeof(path)) + errx(1, "path truncation: %s", path); if (!(fpitems = fopen(path, "wb"))) err(1, "fopen"); - printfeed(fpitems, fp, f); + printfeed(fpitems, fp, &f); if (ferror(fp)) err(1, "ferror: %s", argv[i]); fclose(fp); @@ -147,10 +137,9 @@ main(int argc, char *argv[]) /* append directory item to index */ fprintf(fpindex, "[1|"); - gphlink(fpindex, f->name, strlen(f->name)); + gphlink(fpindex, name, strlen(name)); fprintf(fpindex, " (%lu/%lu)|%s", - f->totalnew, f->total, prefixpath); - snprintf(path, sizeof(path), "%s.gph", f->name); + f.totalnew, f.total, prefixpath); gphlink(fpindex, path, strlen(path)); fputs("|server|port]\n", fpindex); }