xref: /freebsd/lib/libc/gen/sysctlbyname.c (revision f3877f2e4eb74864cf6f4d2b0ccdee5dcd6b875b)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  * $Id$
10  *
11  */
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
14 
15 int
16 sysctlbyname(char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen)
17 {
18 	int name2oid_oid[2];
19 	int real_oid[CTL_MAXNAME+2];
20 	int error, oidlen;
21 
22 	name2oid_oid[0] = 0;	/* This is magic & undocumented! */
23 	name2oid_oid[1] = 3;
24 
25 	oidlen = sizeof(real_oid);
26 	error = sysctl(name2oid_oid, 2, real_oid, &oidlen, name, strlen(name));
27 	if (error < 0)
28 		return error;
29 	oidlen /= sizeof (int);
30 	error = sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen);
31 	return (error);
32 }
33 
34