From mboxrd@z Thu Jan 1 00:00:00 1970 From: David Ahern Subject: Re: [patch iproute2 v3 2/4] utils: Add a function setcmdlinetotal Date: Wed, 27 Dec 2017 08:34:21 -0600 Message-ID: <695fea40-a786-ce97-c3a8-b5411a6894fd@gmail.com> References: <20171225084658.24076-1-chrism@mellanox.com> <20171225084658.24076-3-chrism@mellanox.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Cc: gerlitz.or@gmail.com, stephen@networkplumber.org To: Chris Mi , netdev@vger.kernel.org Return-path: Received: from mail-pf0-f196.google.com ([209.85.192.196]:43666 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751043AbdL0OeY (ORCPT ); Wed, 27 Dec 2017 09:34:24 -0500 Received: by mail-pf0-f196.google.com with SMTP id e3so19841700pfi.10 for ; Wed, 27 Dec 2017 06:34:24 -0800 (PST) In-Reply-To: <20171225084658.24076-3-chrism@mellanox.com> Content-Language: en-US Sender: netdev-owner@vger.kernel.org List-ID: On 12/25/17 2:46 AM, Chris Mi wrote: > 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) > { > This helper should not be needed. There is no need to read what could be a million+ line file multiple times.