scron

simple cron daemon
git clone git://git.2f30.org/scron
Log | Files | Refs | README | LICENSE

commit f5e54befa9ebbb40b40dc184542ea945db4a07e2
parent f86d29fb74dd17c6195035360ffefb5093d26d06
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Fri,  6 Feb 2015 21:11:37 +0200

Remove getfieldtype()

Diffstat:
Mcrond.c | 80++++++++++++++++++++++++++++---------------------------------------------------
1 file changed, 28 insertions(+), 52 deletions(-)

diff --git a/crond.c b/crond.c @@ -21,16 +21,14 @@ #define LEN(x) (sizeof (x) / sizeof *(x)) -enum fieldtype { - ERROR, - WILDCARD, - NUMBER, - RANGE, - STEP -}; - struct field { - enum fieldtype type; + enum { + ERROR, + WILDCARD, + NUMBER, + RANGE, + STEP + } type; long *val; }; @@ -269,46 +267,34 @@ matchentry(struct ctabentry *cte, struct tm *tm) return 1; } -static enum fieldtype -getfieldtype(const char *field) +static int +parsefield(const char *field, long low, long high, struct field *f) { + char *e1, *e2; const char *p; - if (strcmp(field, "*") == 0) - return WILDCARD; - p = field; while (isdigit(*p)) p++; - if (*p == '\0') - return NUMBER; - - p = field; - while (isdigit(*p) || *p == '-') - p++; - if (*p == '\0') - return RANGE; - - p = field; - while (isdigit(*p) || *p == '*' || *p == '/') - p++; - if (*p == '\0') - return STEP; - - return ERROR; -} - -static int -parsefield(const char *field, long low, long high, struct field *f) -{ - char *e1, *e2; f->type = ERROR; - switch (getfieldtype(field)) { - case WILDCARD: - f->type = WILDCARD; + + switch (*p) { + case '*': + if (p[1] == '\0') { + f->type = WILDCARD; + } else if (p[1] == '/') { + f->val = emalloc(sizeof(*f->val)); + errno = 0; + f->val[0] = strtol(field + 2, &e1, 10); + if (e1[0] != '\0' || errno != 0) + break; + if (f->val[0] == 0 || f->val[0] < low || f->val[0] > high) + break; + f->type = STEP; + } break; - case NUMBER: + case '\0': f->val = emalloc(sizeof(*f->val)); errno = 0; f->val[0] = strtol(field, &e1, 10); @@ -318,7 +304,7 @@ parsefield(const char *field, long low, long high, struct field *f) break; f->type = NUMBER; break; - case RANGE: + case '-': f->val = emalloc(2 * sizeof(*f->val)); errno = 0; f->val[0] = strtol(field, &e1, 10); @@ -334,20 +320,10 @@ parsefield(const char *field, long low, long high, struct field *f) break; f->type = RANGE; break; - case STEP: - if (strncmp(field, "*/", 2) != 0) - return -1; - f->val = emalloc(sizeof(*f->val)); - f->val[0] = strtol(field + 2, &e1, 10); - if (e1[0] != '\0' || errno != 0) - break; - if (f->val[0] == 0 || f->val[0] < low || f->val[0] > high) - break; - f->type = STEP; - break; default: return -1; } + if (f->type == ERROR) { free(f->val); return -1;