Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Feb 2025 15:38:52 GMT
From:      Zhenlei Huang <zlei@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 4ef48d172bb1 - stable/14 - sysctl: Refactor function parsefile()
Message-ID:  <202502061538.516FcqqH002427@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/14 has been updated by zlei:

URL: https://cgit.FreeBSD.org/src/commit/?id=4ef48d172bb11f01174fff871d69623eed59d626

commit 4ef48d172bb11f01174fff871d69623eed59d626
Author:     Zhenlei Huang <zlei@FreeBSD.org>
AuthorDate: 2025-01-30 18:20:41 +0000
Commit:     Zhenlei Huang <zlei@FreeBSD.org>
CommitDate: 2025-02-06 15:38:04 +0000

    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)
---
 sbin/sysctl/sysctl.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)

diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
index 13161e9a3be4..883923b4a50f 100644
--- a/sbin/sysctl/sysctl.c
+++ b/sbin/sysctl/sysctl.c
@@ -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);
 }



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202502061538.516FcqqH002427>