The branch stable/14 has been updated by zlei:

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

commit 4ef48d172bb11f01174fff871d69623eed59d626
Author:     Zhenlei Huang <z...@freebsd.org>
AuthorDate: 2025-01-30 18:20:41 +0000
Commit:     Zhenlei Huang <z...@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);
 }

Reply via email to