git: c454c5716357 - main - whereis: fix fetching of user.cs_path sysctl variable

From: Stefan Eßer <se_at_FreeBSD.org>
Date: Fri, 04 Feb 2022 22:44:54 UTC
The branch main has been updated by se:

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

commit c454c57163574ace86b49626b06637e93e05d5e6
Author:     Stefan Eßer <se@FreeBSD.org>
AuthorDate: 2022-02-04 22:37:12 +0000
Commit:     Stefan Eßer <se@FreeBSD.org>
CommitDate: 2022-02-04 22:44:34 +0000

    whereis: fix fetching of user.cs_path sysctl variable
    
    The current implementation of sysctlbyname() does not support the user
    sub-tree. This function exits with a return value of 0, but sets the
    passed string buffer to an empty string.
    
    As a result, the whereis program did not use the value of the sysctl
    variable "user.cs_path", but only the value of the environment
    variable "PATH".
    
    This update makes whereis use the sysctl function with a fixed OID,
    which already supports the user sub-tree.
    
    MFC after:      3 days
---
 usr.bin/whereis/whereis.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/usr.bin/whereis/whereis.c b/usr.bin/whereis/whereis.c
index 48426d376181..abf94b7860e2 100644
--- a/usr.bin/whereis/whereis.c
+++ b/usr.bin/whereis/whereis.c
@@ -261,6 +261,7 @@ defaults(void)
 	DIR *dir;
 	struct stat sb;
 	struct dirent *dirp;
+	const int oid[2] = {CTL_USER, USER_CS_PATH};
 
 	/* default to -bms if none has been specified */
 	if (!opt_b && !opt_m && !opt_s)
@@ -269,13 +270,12 @@ defaults(void)
 	/* -b defaults to default path + /usr/libexec +
 	 * user's path */
 	if (!bindirs) {
-		if (sysctlbyname("user.cs_path", (void *)NULL, &s,
-				 (void *)NULL, 0) == -1)
-			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
+		if (sysctl(oid, 2, NULL, &s, NULL, 0) == -1)
+			err(EX_OSERR, "sysctl(\"user.cs_path\")");
 		if ((b = malloc(s + 1)) == NULL)
 			abort();
-		if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
-			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
+		if (sysctl(oid, 2, b, &s, NULL, 0) == -1)
+			err(EX_OSERR, "sysctl(\"user.cs_path\")");
 		nele = 0;
 		decolonify(b, &bindirs, &nele);
 		bindirs = realloc(bindirs, (nele + 2) * sizeof(char *));