commit ce3a22c641a9a7f1ced983d407e49ba31c71bfd2
parent 3d972927d748267b0e999ef85bc56bab9e771b5d
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun,  2 Aug 2015 13:45:43 +0200

util: simplify parseline() and check t for strtotime().

Diffstat:
Msfeed_frames.c | 2+-
Msfeed_html.c | 2+-
Msfeed_mbox.c | 2+-
Msfeed_plain.c | 2+-
Mutil.c | 25++++++++++++-------------
Mutil.h | 14+++++++-------
6 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/sfeed_frames.c b/sfeed_frames.c @@ -112,7 +112,7 @@ printfeed(FILE *fpitems, FILE *fpin, struct feed *f) } fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", fpitems); - while (parseline(&line, &linesize, fields, FieldLast, '\t', fpin) > 0) { + while (parseline(&line, &linesize, fields, fpin) > 0) { /* write content */ if (!(namelen = normalizepath(fields[FieldTitle], name, sizeof(name)))) continue; diff --git a/sfeed_html.c b/sfeed_html.c @@ -33,7 +33,7 @@ printfeed(FILE *fp, struct feed *f) } fputs("<table cellpadding=\"0\" cellspacing=\"0\">\n", stdout); - while (parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) { + while (parseline(&line, &linesize, fields, fp) > 0) { r = strtotime(fields[FieldUnixTimestamp], &parsedtime); isnew = (r != -1 && parsedtime >= comparetime) ? 1 : 0; islink = (fields[FieldLink][0] != '\0') ? 1 : 0; diff --git a/sfeed_mbox.c b/sfeed_mbox.c @@ -76,7 +76,7 @@ printfeed(FILE *fp, const char *feedname) if (!strftime(mtimebuf, sizeof(mtimebuf), "%a %b %d %H:%M:%S %Y", &tm)) errx(1, "can't format current time"); - while (parseline(&line, &linesize, fields, FieldLast, '\t', fp) > 0) { + while (parseline(&line, &linesize, fields, fp) > 0) { if ((r = strtotime(fields[FieldUnixTimestamp], &parsedtime)) == -1) continue; /* invalid date */ if (!gmtime_r(&parsedtime, &tm)) diff --git a/sfeed_plain.c b/sfeed_plain.c @@ -44,7 +44,7 @@ printfeed(FILE *fp, const char *feedname) char *fields[FieldLast]; time_t parsedtime; - while (parseline(&line, &size, fields, FieldLast, '\t', fp) > 0) { + while (parseline(&line, &size, fields, fp) > 0) { if (strtotime(fields[FieldUnixTimestamp], &parsedtime) != -1 && parsedtime >= comparetime) fputs(" N ", stdout); diff --git a/util.c b/util.c @@ -155,23 +155,21 @@ encodeuri(const char *s, char *buf, size_t bufsiz) /* Read a field-separated line from 'fp', * separated by a character 'separator', - * 'fields' is a list of pointer with a maximum size of 'maxfields'. - * 'maxfields' must be > 0. - * 'line' buffer is allocated using malloc, 'size' will contain the - * allocated buffer size. - * returns: amount of fields read or -1 on error. */ -int -parseline(char **line, size_t *size, char **fields, - unsigned int maxfields, int separator, FILE *fp) + * 'fields' is a list of pointers with a size of FieldLast (must be >0). + * 'line' buffer is allocated using malloc, 'size' will contain the allocated + * buffer size. + * returns: amount of fields read (>0) or -1 on error. */ +ssize_t +parseline(char **line, size_t *size, char *fields[FieldLast], FILE *fp) { char *prev, *s; - unsigned int i; + size_t i; if (getline(line, size, fp) <= 0) return -1; for (prev = *line, i = 0; - (s = strchr(prev, separator)) && i < maxfields - 1; + (s = strchr(prev, '\t')) && i < FieldLast - 1; i++) { *s = '\0'; fields[i] = prev; @@ -179,10 +177,10 @@ parseline(char **line, size_t *size, char **fields, } fields[i++] = prev; /* make non-parsed fields empty. */ - for (; i < maxfields; i++) + for (; i < FieldLast; i++) fields[i] = ""; - return (int)i; + return (ssize_t)i; } /* Parse time to time_t, assumes time_t is signed. */ @@ -195,7 +193,8 @@ strtotime(const char *s, time_t *t) l = strtol(s, NULL, 10); if (errno != 0) return -1; - *t = (time_t)l; + if (t) + *t = (time_t)l; return 0; } diff --git a/util.h b/util.h @@ -25,12 +25,12 @@ enum { FieldUnixTimestamp = 0, FieldTimeFormatted, FieldTitle, FieldLink, FieldContent, FieldContentType, FieldId, FieldAuthor, FieldFeedType, FieldLast }; -int absuri(const char *, const char *, char *, size_t); -int encodeuri(const char *, char *, size_t); -int parseline(char **, size_t *, char **, unsigned int, int, FILE *); -int parseuri(const char *, struct uri *, int); -int strtotime(const char *, time_t *); -char * xbasename(const char *); -void xmlencode(const char *, FILE *); +int absuri(const char *, const char *, char *, size_t); +int encodeuri(const char *, char *, size_t); +ssize_t parseline(char **, size_t *, char *[FieldLast], FILE *); +int parseuri(const char *, struct uri *, int); +int strtotime(const char *, time_t *); +char * xbasename(const char *); +void xmlencode(const char *, FILE *);