From 10b4195af24561c8d4a39b27c31cde21eb1cf1b9 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 3 Apr 2019 23:41:09 +0200 Subject: Replace rdline with libbsd fparseln Signed-off-by: David Oberhollenzer --- rdcron.c | 53 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 11 deletions(-) (limited to 'rdcron.c') diff --git a/rdcron.c b/rdcron.c index 9a9171e..91be4d0 100644 --- a/rdcron.c +++ b/rdcron.c @@ -96,6 +96,10 @@ static const struct { /*****************************************************************************/ +#define complainf(rd, msg, ...) \ + fprintf(stderr, "%s: %zu: " msg "\n", \ + rd->filename, rd->lineno, __VA_ARGS__) + static char *readnum(char *line, int *out, int minval, int maxval, const enum_map_t *mnemonic, rdline_t *rd) { @@ -113,7 +117,7 @@ static char *readnum(char *line, int *out, int minval, int maxval, } if (ev->name == NULL) { - rdline_complain(rd, "unexpected '%.*s'", i, line); + complainf(rd, "unexpected '%.*s'", i, line); return NULL; } @@ -138,13 +142,14 @@ static char *readnum(char *line, int *out, int minval, int maxval, *out = value; return line; fail_of: - rdline_complain(rd, "value exceeds maximum (%d > %d)", value, maxval); + complainf(rd, "value exceeds maximum (%d > %d)", value, maxval); return NULL; fail_uf: - rdline_complain(rd, "value too small (%d < %d)", value, minval); + complainf(rd, "value too small (%d < %d)", value, minval); return NULL; fail_mn: - rdline_complain(rd, "expected numeric value"); + fprintf(stderr, "%s: %zu: expected numeric value\n", + rd->filename, rd->lineno); return NULL; } @@ -201,7 +206,8 @@ next: return line; fail: - rdline_complain(rd, "invalid time range expression"); + fprintf(stderr, "%s: %zu: invalid time range expression\n", + rd->filename, rd->lineno); return NULL; } @@ -229,7 +235,7 @@ static char *cron_interval(crontab_t *cron, rdline_t *rd) *cron = intervals[i].tab; return rd->line + j; fail: - rdline_complain(rd, "unknown interval '%.*s'", (int)j, rd->line); + complainf(rd, "unknown interval '%.*s'", (int)j, rd->line); return NULL; } @@ -266,14 +272,39 @@ crontab_t *rdcron(int dirfd, const char *filename) crontab_t *cron, *list = NULL; rdline_t rd; char *ptr; + int fd; + + memset(&rd, 0, sizeof(rd)); + rd.filename = filename; + + fd = openat(dirfd, filename, O_RDONLY); + if (fd == -1) { + perror(filename); + return NULL; + } - if (rdline_init(&rd, dirfd, filename)) + rd.fp = fdopen(fd, "r"); + if (rd.fp == NULL) { + perror("fdopen"); + close(fd); return NULL; + } + + for (;;) { + free(rd.line); + errno = 0; + + rd.line = fparseln(rd.fp, NULL, &rd.lineno, NULL, 0); + + if (rd.line == NULL) { + if (errno) + perror(filename); + break; + } - while (rdline(&rd) == 0) { cron = calloc(1, sizeof(*cron)); if (cron == NULL) { - rdline_complain(&rd, strerror(errno)); + perror(filename); break; } @@ -293,7 +324,7 @@ crontab_t *rdcron(int dirfd, const char *filename) cron->exec = strdup(ptr); if (cron->exec == NULL) { - rdline_complain(&rd, strerror(errno)); + perror(filename); free(cron); continue; } @@ -302,6 +333,6 @@ crontab_t *rdcron(int dirfd, const char *filename) list = cron; } - rdline_cleanup(&rd); + fclose(rd.fp); return list; } -- cgit v1.2.3