sfeed

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

commit f5be0d36c5d45c10e055c13dd8060152effa3aed
parent bcd1e1972d63cdd2ca8b42572a129be521558aba
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri,  2 Jan 2015 15:32:45 +0100

cleanup

- dont free at end (not needed in our case).
- use 0 and 1 instead of EXIT_SUCCESS, EXIT_FAILURE.
- use err (from err.h) instead of custom die().

Diffstat:
Msfeed_frames.c | 65++++++++++++++++++++++++-----------------------------------------
Msfeed_html.c | 26+++++---------------------
Msfeed_opml_import.c | 4++--
Msfeed_plain.c | 4+---
Msfeed_stats.c | 26+++++---------------------
Msfeed_web.c | 2+-
Msfeed_xmlenc.c | 6+++---
Mutil.c | 14--------------
8 files changed, 41 insertions(+), 106 deletions(-)

diff --git a/sfeed_frames.c b/sfeed_frames.c @@ -1,4 +1,5 @@ #include <ctype.h> +#include <err.h> #include <errno.h> #include <limits.h> #include <stdio.h> @@ -18,36 +19,21 @@ static FILE *fpcontent = NULL; static char *line = NULL; static struct feed *feeds = NULL; -/* print error message to stderr */ -static void -die(const char *s) -{ - fprintf(stderr, "sfeed_frames: %s\n", s); - exit(EXIT_FAILURE); -} - static void cleanup(void) { - if(fpmenu) { + if(fpmenu) fclose(fpmenu); - fpmenu = NULL; - } - if(fpitems) { + if(fpitems) fclose(fpitems); - fpitems = NULL; - } - if(fpindex) { + if(fpindex) fclose(fpindex); - fpindex = NULL; - } - if(fpcontent) { + if(fpcontent) fclose(fpcontent); - fpcontent = NULL; - } - free(line); /* free line */ - line = NULL; - feedsfree(feeds); /* free feeds linked-list */ + fpmenu = NULL; + fpitems = NULL; + fpindex = NULL; + fpcontent = NULL; } /* print text, ignore tabs, newline and carriage return etc @@ -126,17 +112,17 @@ main(int argc, char *argv[]) /* write main index page */ if(snprintf(dirpath, sizeof(dirpath), "%s/index.html", basepath) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); if(!(fpindex = fopen(dirpath, "w+b"))) - die("can't write index.html"); + err(1, "fopen"); if(snprintf(dirpath, sizeof(dirpath), "%s/menu.html", basepath) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); if(!(fpmenu = fopen(dirpath, "w+b"))) - die("can't write menu.html"); + err(1, "fopen: can't write menu.html"); if(snprintf(dirpath, sizeof(dirpath), "%s/items.html", basepath) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); if(!(fpitems = fopen(dirpath, "w+b"))) - die("can't write items.html"); + err(1, "fopen"); fputs("<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"../style.css\" />" "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head>" "<body class=\"frame\"><div id=\"items\">", fpitems); @@ -159,20 +145,17 @@ main(int argc, char *argv[]) continue; if(snprintf(dirpath, sizeof(dirpath), "%s/%s", basepath, name) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); /* directory doesn't exist: try to create it. */ if(stat(dirpath, &st) == -1) { - if(mkdir(dirpath, S_IRWXU) == -1) { - fprintf(stderr, "sfeed_frames: can't make directory '%s': %s\n", - dirpath, strerror(errno)); - exit(EXIT_FAILURE); - } + if(mkdir(dirpath, S_IRWXU) == -1) + err(1, "sfeed_frames: can't make directory '%s'", dirpath); } - strlcpy(reldirpath, name, sizeof(reldirpath)); + strlcpy(reldirpath, name, sizeof(reldirpath)); /* TODO: check truncation */ if(!(f = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); + err(1, "calloc"); if(totalfeeds) { /* end previous one. */ fputs("</table>\n", fpitems); @@ -185,7 +168,7 @@ main(int argc, char *argv[]) } /* write menu link if new. */ if(!(fcur->name = strdup(feedname))) - die("can't allocate enough memory"); + err(1, "strdup"); if(fields[FieldFeedName][0] != '\0') { fputs("<h2 id=\"", fpitems); printfeednameid(fcur->name, fpitems); @@ -202,9 +185,9 @@ main(int argc, char *argv[]) if(!(namelen = makepathname(fields[FieldTitle], name, sizeof(name)))) continue; if(snprintf(filepath, sizeof(filepath), "%s/%s.html", dirpath, name) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); if(snprintf(relfilepath, sizeof(relfilepath), "%s/%s.html", reldirpath, name) <= 0) - die("snprintf() format error"); + err(1, "snprintf"); /* file doesn't exist yet and has write access */ if(access(filepath, F_OK) != 0 && (fpcontent = fopen(filepath, "w+b"))) { @@ -303,5 +286,5 @@ main(int argc, char *argv[]) "</frameset>\n" "</html>", fpindex); - return EXIT_SUCCESS; + return 0; } diff --git a/sfeed_html.c b/sfeed_html.c @@ -1,4 +1,5 @@ #include <ctype.h> +#include <err.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -11,22 +12,6 @@ static int showsidebar = 1; /* show sidebar ? */ static struct feed *feeds = NULL; /* start of feeds linked-list. */ static char *line = NULL; -static void -cleanup(void) -{ - free(line); /* free line */ - line = NULL; - feedsfree(feeds); /* free feeds linked-list */ -} - -/* print error message to stderr */ -static void -die(const char *s) -{ - fprintf(stderr, "sfeed_html: %s\n", s); - exit(EXIT_FAILURE); -} - int main(void) { @@ -37,7 +22,6 @@ main(void) time_t parsedtime, comparetime; size_t size = 0; - atexit(cleanup); comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ fputs( "<!DOCTYPE HTML>\n" @@ -50,7 +34,7 @@ main(void) stdout); if(!(fcur = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); + err(1, "calloc"); feeds = fcur; while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) { @@ -63,7 +47,7 @@ main(void) /* first of feed section or new feed section. */ if(!totalfeeds || (fcur && strcmp(fcur->name, fields[FieldFeedName]))) { if(!(f = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); + err(1, "calloc"); /*f->next = NULL;*/ if(totalfeeds) { /* end previous one. */ fputs("</table>\n", stdout); @@ -82,7 +66,7 @@ main(void) /* TODO: memcpy and make fcur->name static? */ if(!(fcur->name = strdup(fields[FieldFeedName]))) - die("can't allocate enough memory"); + err(1, "strdup"); if(fields[FieldFeedName][0] != '\0') { fputs("<h2 id=\"", stdout); @@ -153,5 +137,5 @@ main(void) fprintf(stdout, "%lu", totalnew); fputs(")</title>\n</html>", stdout); - return EXIT_SUCCESS; + return 0; } diff --git a/sfeed_opml_import.c b/sfeed_opml_import.c @@ -74,7 +74,6 @@ int main(void) { xmlparser_init(&parser, stdin); - parser.xmltagstart = xml_handler_start_element; parser.xmltagend = xml_handler_end_element; parser.xmlattr = xml_handler_attr; @@ -91,5 +90,6 @@ main(void) " # feed <name> <feedurl> <basesiteurl> [encoding]\n", stdout); xmlparser_parse(&parser); fputs("}\n", stdout); - return EXIT_SUCCESS; + + return 0; } diff --git a/sfeed_plain.c b/sfeed_plain.c @@ -34,7 +34,5 @@ main(void) printlink(fields[FieldLink], fields[FieldFeedUrl], stdout); putchar('\n'); } - free(line); - line = NULL; - return EXIT_SUCCESS; + return 0; } diff --git a/sfeed_stats.c b/sfeed_stats.c @@ -1,4 +1,5 @@ #include <ctype.h> +#include <err.h> #include <errno.h> #include <stdio.h> #include <stdlib.h> @@ -10,22 +11,6 @@ static struct feed *feeds = NULL; /* start of feeds linked-list. */ static char *line = NULL; -static void -cleanup(void) -{ - free(line); /* free line */ - line = NULL; - feedsfree(feeds); /* free feeds linked-list */ -} - -/* print error message to stderr */ -static void -die(const char *s) -{ - fprintf(stderr, "sfeed_stats: %s\n", s); - exit(EXIT_FAILURE); -} - int main(void) { @@ -36,11 +21,10 @@ main(void) time_t parsedtime, comparetime, timenewest = 0; size_t size = 0; - atexit(cleanup); comparetime = time(NULL) - (3600 * 24); /* 1 day is old news */ if(!(fcur = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); + err(1, "calloc"); feeds = fcur; while(parseline(&line, &size, fields, FieldLast, '\t', stdin) > 0) { @@ -52,7 +36,7 @@ main(void) /* first of feed section or new feed section. */ if(!totalfeeds || (fcur && strcmp(fcur->name, fields[FieldFeedName]))) { if(!(f = calloc(1, sizeof(struct feed)))) - die("can't allocate enough memory"); + err(1, "calloc"); if(totalfeeds) { /* end previous one. */ fcur->next = f; fcur = f; @@ -73,7 +57,7 @@ main(void) /* TODO: memcpy and make fcur->name static? */ if(!(fcur->name = strdup(fields[FieldFeedName]))) - die("can't allocate enough memory"); + err(1, "strdup"); totalfeeds++; } @@ -95,5 +79,5 @@ main(void) printf(" ================================\n"); printf("%c %-20.20s [%4lu/%-4lu] %s\n", totalnew > 0 ? 'N' : ' ', "Total:", totalnew, totalitems, timenewestformat); - return EXIT_SUCCESS; + return 0; } diff --git a/sfeed_web.c b/sfeed_web.c @@ -86,5 +86,5 @@ main(int argc, char *argv[]) xmlparser_parse(&x); - return found > 0 ? EXIT_SUCCESS : EXIT_FAILURE; + return found > 0 ? 0: 1; } diff --git a/sfeed_xmlenc.c b/sfeed_xmlenc.c @@ -15,7 +15,7 @@ xmltagstart(XMLParser *p, const char *tag, size_t taglen) /* optimization: try to find processing instruction at start */ if(tags > 3) - exit(EXIT_FAILURE); + exit(1); isxmlpi = (!strncasecmp(tag, "?xml", taglen)) ? 1 : 0; tags++; } @@ -47,7 +47,7 @@ xmlattr(XMLParser *p, const char *tag, size_t taglen, const char *name, putc(tolower((int)*value), stdout); putchar('\n'); } - exit(EXIT_SUCCESS); + exit(0); } } @@ -63,5 +63,5 @@ main(void) xmlparser_parse(&x); - return EXIT_FAILURE; + return 1; } diff --git a/util.c b/util.c @@ -98,20 +98,6 @@ printhtmlencoded(const char *s, FILE *fp) { } void -feedsfree(struct feed *f) -{ - struct feed *next = NULL; - - for(; f; f = next) { - next = f->next; - f->next = NULL; - free(f->name); - f->name = NULL; - free(f); - } -} - -void printutf8pad(FILE *fp, const char *s, size_t len, int pad) { wchar_t w;