From b9a28c37a879ba9ef09201001cb4e0b0f4362a3e Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Sun, 4 Oct 2009 14:44:20 +0200 Subject: [PATCH] tail, head: use OBJ_ARG to simplify code --- src/core.c | 4 ++-- src/tailhead.c | 36 ++++++++++++++++-------------------- 2 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/core.c b/src/core.c index 2430cc56..bbda97e1 100644 --- a/src/core.c +++ b/src/core.c @@ -901,9 +901,9 @@ struct text_object *construct_text_object(const char *s, const char *arg, long free(buf); } #endif /* __linux__ */ - END OBJ(tail, 0) + END OBJ_ARG(tail, 0, "tail needs arguments") init_tailhead("tail", arg, obj, free_at_crash); - END OBJ(head, 0) + END OBJ_ARG(head, 0, "head needs arguments") init_tailhead("head", arg, obj, free_at_crash); END OBJ_ARG(lines, 0, "lines needs an argument") obj->data.s = strndup(arg, text_buffer_size); diff --git a/src/tailhead.c b/src/tailhead.c index 47bca78e..81ec8e47 100644 --- a/src/tailhead.c +++ b/src/tailhead.c @@ -59,29 +59,25 @@ void tailstring(char *string, int endofstring, int wantedlines) { void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) { unsigned int args; - if(arg) { - obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE); - obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES; - args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses); - if(args == 2 || args == 3) { - if(obj->data.headtail.max_uses < 1) { - free(obj->data.headtail.logfile); - CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type); - } - if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) { - to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile); - obj->data.headtail.buffer = NULL; - obj->data.headtail.current_use = 0; - }else{ - free(obj->data.headtail.logfile); - CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES); - } - } else { + obj->data.headtail.logfile=malloc(DEFAULT_TEXT_BUFFER_SIZE); + obj->data.headtail.max_uses = DEFAULT_MAX_HEADTAIL_USES; + args = sscanf(arg, "%s %d %d", obj->data.headtail.logfile, &obj->data.headtail.wantedlines, &obj->data.headtail.max_uses); + if(args == 2 || args == 3) { + if(obj->data.headtail.max_uses < 1) { free(obj->data.headtail.logfile); - CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type); + CRIT_ERR(obj, free_at_crash, "invalid arg for %s, next_check must be larger than 0", type); + } + if (obj->data.headtail.wantedlines > 0 && obj->data.headtail.wantedlines <= MAX_HEADTAIL_LINES) { + to_real_path(obj->data.headtail.logfile, obj->data.headtail.logfile); + obj->data.headtail.buffer = NULL; + obj->data.headtail.current_use = 0; + }else{ + free(obj->data.headtail.logfile); + CRIT_ERR(obj, free_at_crash, "invalid arg for %s, number of lines must be between 1 and %d", type, MAX_HEADTAIL_LINES); } } else { - CRIT_ERR(obj, free_at_crash, "%s needs arguments", type); + free(obj->data.headtail.logfile); + CRIT_ERR(obj, free_at_crash, "%s needs a file as 1st and a number of lines as 2nd argument", type); } }