]>
Commit | Line | Data |
---|---|---|
e9ce8d39 A |
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 | * | |
e9ce8d39 | 9 | */ |
9385eb3d A |
10 | |
11 | #include <sys/cdefs.h> | |
12 | __FBSDID("$FreeBSD: src/lib/libc/gen/sysctlbyname.c,v 1.5 2002/02/01 00:57:29 obrien Exp $"); | |
13 | ||
e9ce8d39 A |
14 | #include <sys/types.h> |
15 | #include <sys/sysctl.h> | |
16 | #include <string.h> | |
23e20b00 A |
17 | #include <sys/errno.h> |
18 | #include <TargetConditionals.h> | |
19 | ||
20 | extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, | |
21 | void *newp, size_t newlen); | |
22 | extern int | |
23 | __sysctlbyname(const char *name, size_t namelen, void *oldp, size_t *oldlenp, void *newp, | |
24 | size_t newlen); | |
e9ce8d39 A |
25 | |
26 | int | |
27 | sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, | |
28 | size_t newlen) | |
29 | { | |
30 | int name2oid_oid[2]; | |
31 | int real_oid[CTL_MAXNAME+2]; | |
32 | int error; | |
33 | size_t oidlen; | |
34 | ||
23e20b00 A |
35 | #if !TARGET_IPHONE_SIMULATOR |
36 | /* Try primary system call first, fall back if not supported */ | |
37 | error = __sysctlbyname(name, strlen(name), oldp, oldlenp, newp, newlen); | |
38 | if ((error == 0) || (errno != ENOSYS)) | |
39 | return error; | |
40 | #endif /* !TARGET_IPHONE_SIMULATOR */ | |
41 | ||
e9ce8d39 A |
42 | name2oid_oid[0] = 0; /* This is magic & undocumented! */ |
43 | name2oid_oid[1] = 3; | |
44 | ||
45 | oidlen = sizeof(real_oid); | |
23e20b00 | 46 | error = __sysctl(name2oid_oid, 2, real_oid, &oidlen, (void *)name, |
e9ce8d39 A |
47 | strlen(name)); |
48 | if (error < 0) | |
49 | return error; | |
50 | oidlen /= sizeof (int); | |
23e20b00 | 51 | error = __sysctl(real_oid, oidlen, oldp, oldlenp, newp, newlen); |
e9ce8d39 A |
52 | return (error); |
53 | } | |
54 |