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 <chr...@mellanox.com> > --- > 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.