From mboxrd@z Thu Jan 1 00:00:00 1970 From: Chris Mi Subject: [patch iproute2 v3 2/4] utils: Add a function setcmdlinetotal Date: Mon, 25 Dec 2017 17:46:56 +0900 Message-ID: <20171225084658.24076-3-chrism@mellanox.com> References: <20171225084658.24076-1-chrism@mellanox.com> Cc: gerlitz.or@gmail.com, stephen@networkplumber.org, dsahern@gmail.com To: netdev@vger.kernel.org Return-path: Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:56456 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1750882AbdLYIrJ (ORCPT ); Mon, 25 Dec 2017 03:47:09 -0500 In-Reply-To: <20171225084658.24076-1-chrism@mellanox.com> Sender: netdev-owner@vger.kernel.org List-ID: This function calculates how many commands a batch file has and set it to global variable cmdlinetotal. Signed-off-by: Chris Mi --- include/utils.h | 4 ++++ lib/utils.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/include/utils.h b/include/utils.h index d3895d56..113a8c31 100644 --- a/include/utils.h +++ b/include/utils.h @@ -235,6 +235,10 @@ void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n); extern int cmdlineno; ssize_t getcmdline(char **line, size_t *len, FILE *in); + +extern int cmdlinetotal; +void setcmdlinetotal(const char *name); + int makeargs(char *line, char *argv[], int maxargs); int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6); diff --git a/lib/utils.c b/lib/utils.c index 7ced8c06..53ca389f 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -1202,6 +1202,26 @@ ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) return cc; } +int cmdlinetotal; + +void setcmdlinetotal(const char *name) +{ + char *line = NULL; + size_t len = 0; + + if (name && strcmp(name, "-") != 0) { + if (freopen(name, "r", stdin) == NULL) { + fprintf(stderr, "Cannot open file \"%s\" for reading: %s\n", + name, strerror(errno)); + return; + } + } + + cmdlinetotal = 0; + while (getcmdline(&line, &len, stdin) != -1) + cmdlinetotal++; +} + /* split command line into argument vector */ int makeargs(char *line, char *argv[], int maxargs) { -- 2.14.3