sysctl: Refactor function parsefile()

Let the caller open the file and pass in the file handler. This can
benefit an upcoming change so that we will have cleaner logic.

No functional change intended.

Suggested by:	markj
MFC after:	1 week

(cherry picked from commit 6193855fc76c591ffabe6168cd674e6ec0dafa8e)
This commit is contained in:
Zhenlei Huang 2025-01-31 02:20:41 +08:00
parent b709f7b38c
commit 4ef48d172b

View file

@ -67,7 +67,7 @@ static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
static bool Fflag, Jflag, lflag, Vflag;
static int oidfmt(int *, int, char *, u_int *);
static int parsefile(const char *);
static int parsefile(FILE *);
static int parse(const char *, int);
static int show_var(int *, int, bool);
static int sysctl_all(int *, int);
@ -132,6 +132,7 @@ main(int argc, char **argv)
{
int ch;
int warncount = 0;
FILE *file = NULL;
setlocale(LC_NUMERIC, "");
setbuf(stdout,0);
@ -227,8 +228,13 @@ main(int argc, char **argv)
if (argc == 0 && conffile == NULL)
usage();
if (conffile != NULL)
warncount += parsefile(conffile);
if (conffile != NULL) {
file = fopen(conffile, "r");
if (file == NULL)
err(EX_NOINPUT, "%s", conffile);
warncount += parsefile(file);
fclose(file);
}
while (argc-- > 0)
warncount += parse(*argv++, 0);
@ -569,15 +575,11 @@ parse(const char *string, int lineno)
}
static int
parsefile(const char *filename)
parsefile(FILE *file)
{
FILE *file;
char line[BUFSIZ], *p, *pq, *pdq;
int warncount = 0, lineno = 0;
file = fopen(filename, "r");
if (file == NULL)
err(EX_NOINPUT, "%s", filename);
while (fgets(line, sizeof(line), file) != NULL) {
lineno++;
p = line;
@ -613,7 +615,6 @@ parsefile(const char *filename)
else
warncount += parse(p, lineno);
}
fclose(file);
return (warncount);
}