sfeed

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

commit 0c509625066b59346b6a0be6bfaf96bfeb0fa0db
parent 0761823f244682e43f3058cede70f4cc239cb452
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Wed,  2 Apr 2014 16:00:52 +0200

sfeed: improve parsetime(), cleanup

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>

Diffstat:
MTODO | 2++
Msfeed.c | 7++++---
Mutil.c | 49+++----------------------------------------------
3 files changed, 9 insertions(+), 49 deletions(-)

diff --git a/TODO b/TODO @@ -9,3 +9,5 @@ bugs?: [ ] flabber link is broken (flabberc2 feedname). [ ] feeds file: empty tab (encoding cant be detected?) + +[ ] optimize afgets() diff --git a/sfeed.c b/sfeed.c @@ -402,7 +402,7 @@ tmtotime(struct tm *tm) { } static time_t -parsetime(const char *s, char *buf) { +parsetime(const char *s, char *buf, size_t bufsiz) { struct tm tm; char tz[64]; const char *end; @@ -414,7 +414,7 @@ parsetime(const char *s, char *buf) { offset = gettimetz(end, tz, sizeof(tz) - 1); /* TODO: use snprintf(): make sure *printf can't overflow */ if(buf) - sprintf(buf, "%04d-%02d-%02d %02d:%02d:%02d %-.16s", + snprintf(buf, bufsiz, "%04d-%02d-%02d %02d:%02d:%02d %-.16s", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tz); /* return UNIX time, reverse offset to GMT+0 */ @@ -683,7 +683,8 @@ xml_handler_end_element(XMLParser *p, const char *name, size_t namelen, int issh (feeditem.feedtype == FeedTypeRSS && istag(name, namelen, "item", strlen("item")))) /* RSS */ { - printf("%ld", (long)parsetime((&feeditem.timestamp)->data, timebuf)); + printf("%ld", (long)parsetime((&feeditem.timestamp)->data, + timebuf, sizeof(timebuf))); putchar(FieldSeparator); fputs(timebuf, stdout); putchar(FieldSeparator); diff --git a/util.c b/util.c @@ -7,47 +7,6 @@ #include "util.h" -#if 0 -/* TODO: optimize */ -char * -afgets(char **p, size_t *size, FILE *fp) { - char buf[BUFSIZ], *alloc = NULL; - size_t n, len = 0, allocsiz; - int end = 0; - - while(fgets(buf, sizeof(buf), fp)) { - n = strlen(buf); - if(buf[n - 1] == '\n') { /* dont store newlines. */ - buf[n - 1] = '\0'; - n--; - end = 1; /* newline found, end */ - } - len += n; - allocsiz = len + 1; - if(allocsiz > *size) { - if((alloc = realloc(*p, allocsiz))) { - *p = alloc; - *size = allocsiz; - } else { - free(*p); - *p = NULL; - fputs("error: could not realloc\n", stderr); - exit(EXIT_FAILURE); - return NULL; - } - } - strncpy((*p + (len - n)), buf, n); - if(end || feof(fp)) - break; - } - if(*p && len > 0) { - (*p)[len] = '\0'; - return *p; - } - return NULL; -} -#endif - /* * Taken from OpenBSD. * Copy src to string dst of size siz. At most siz-1 characters @@ -77,7 +36,6 @@ strlcpy(char *dst, const char *src, size_t siz) { return(s - src - 1); /* count does not include NUL */ } -/* TODO: optimize */ char * afgets(char **p, size_t *size, FILE *fp) { char buf[BUFSIZ], *alloc = NULL; @@ -105,8 +63,7 @@ afgets(char **p, size_t *size, FILE *fp) { return NULL; } } - strlcpy((*p + (len - n)), buf, n + 1); /* TODO: dont depend on strlcpy */ -/* strncpy((*p + (len - n)), buf, n);*/ + strlcpy((*p + (len - n)), buf, n + 1); } if(*p && len > 0) { (*p)[len] = '\0'; @@ -194,9 +151,9 @@ feedsfree(struct feed *f) { for(; f; f = next) { next = f->next; - /*f->next = NULL;*/ + f->next = NULL; free(f->name); - /*f->name = NULL;*/ + f->name = NULL; free(f); } }