git: 73a516c6527c - stable/13 - sysctl: Refactor function parsefile()
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 06 Feb 2025 16:09:52 UTC
The branch stable/13 has been updated by zlei: URL: https://cgit.FreeBSD.org/src/commit/?id=73a516c6527c84701921c329d4f08da44522ccc4 commit 73a516c6527c84701921c329d4f08da44522ccc4 Author: Zhenlei Huang <zlei@FreeBSD.org> AuthorDate: 2025-01-30 18:20:41 +0000 Commit: Zhenlei Huang <zlei@FreeBSD.org> CommitDate: 2025-02-06 16:08:52 +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) (cherry picked from commit 4ef48d172bb11f01174fff871d69623eed59d626) --- 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 887de78d269b..41dcf3db9184 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 Jflag, 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); @@ -220,8 +221,13 @@ main(int argc, char **argv) usage(); warncount = 0; - 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); @@ -562,15 +568,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; @@ -606,7 +608,6 @@ parsefile(const char *filename) else warncount += parse(p, lineno); } - fclose(file); return (warncount); }