From f93d36fd9232ce878aa8df6fa10fedecf218fb34 Mon Sep 17 00:00:00 2001
From: Robert Watson <rwatson@FreeBSD.org>
Date: Thu, 15 Sep 2005 16:08:04 +0000
Subject: [PATCH] Add "-q" argument to sysctl(8), which suppresses a limited
 set of warnings/ errors generated.  In particular, it suppresses "unknown
 oid" when attempting to get or set a sysctl not present in the kernel.

MFC after:	1 week
---
 sbin/sysctl/sysctl.8 |  6 +++++-
 sbin/sysctl/sysctl.c | 16 ++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/sbin/sysctl/sysctl.8 b/sbin/sysctl/sysctl.8
index d6fde98df1bb..e7db649212ce 100644
--- a/sbin/sysctl/sysctl.8
+++ b/sbin/sysctl/sysctl.8
@@ -40,7 +40,7 @@
 .Ar name Ns Op = Ns Ar value
 .Ar ...
 .Nm
-.Op Fl bdehNnox
+.Op Fl bdehNnoqx
 .Fl a
 .Sh DESCRIPTION
 The
@@ -111,6 +111,10 @@ use:
 Show opaque variables (which are normally suppressed).
 The format and length are printed, as well as a hex dump of the first
 sixteen bytes of the value.
+.It Fl q
+Suppress some warnings generated by
+.Nm
+to standard error.
 .It Fl X
 Equivalent to
 .Fl x a
diff --git a/sbin/sysctl/sysctl.c b/sbin/sysctl/sysctl.c
index ad35769d81f8..fd38ceaa56a9 100644
--- a/sbin/sysctl/sysctl.c
+++ b/sbin/sysctl/sysctl.c
@@ -60,7 +60,8 @@ static const char rcsid[] =
 #include <string.h>
 #include <unistd.h>
 
-static int	aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag, xflag;
+static int	aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag;
+static int	qflag, xflag;
 
 static int	oidfmt(int *, int, char *, u_int *);
 static void	parse(char *);
@@ -89,7 +90,7 @@ main(int argc, char **argv)
 	setbuf(stdout,0);
 	setbuf(stderr,0);
 
-	while ((ch = getopt(argc, argv, "AabdehNnowxX")) != -1) {
+	while ((ch = getopt(argc, argv, "AabdehNnoqwxX")) != -1) {
 		switch (ch) {
 		case 'A':
 			/* compatibility */
@@ -119,6 +120,9 @@ main(int argc, char **argv)
 		case 'o':
 			oflag = 1;
 			break;
+		case 'q':
+			qflag = 1;
+			break;
 		case 'w':
 			/* compatibility */
 			/* ignored */
@@ -181,8 +185,12 @@ parse(char *string)
 	}
 	len = name2oid(bufp, mib);
 
-	if (len < 0)
-		errx(1, "unknown oid '%s'", bufp);
+	if (len < 0) {
+		if (qflag)
+			exit(1);
+		else
+			errx(1, "unknown oid '%s'", bufp);
+	}
 
 	if (oidfmt(mib, len, fmt, &kind))
 		err(1, "couldn't find format of oid '%s'", bufp);