Use some macro evil to create the functions for using sysctl(3) to get each

variable natively.  It should allow you to use any sysctl mib set, but due
to limitations of what I can do with macros, right now it's limited to two,
which is all this program used anyway.

Sponsored by:	Bright Path Solutions
This commit is contained in:
Juli Mallett 2002-09-23 06:47:39 +00:00
parent fe4f1dfc8f
commit 5a4ba400c1
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=103846

View File

@ -84,6 +84,7 @@ main(int argc, char *argv[])
int ch; int ch;
setup_get(); setup_get();
flags = 0;
while ((ch = getopt(argc, argv, "amnprsv")) != -1) while ((ch = getopt(argc, argv, "amnprsv")) != -1)
switch(ch) { switch(ch) {
@ -169,99 +170,53 @@ print_uname(u_int flags)
printf("\n"); printf("\n");
} }
void #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \
native_sysname(void) void \
{ native_##var(void) \
int mib[2]; { \
size_t len; int mib[] = { (mib0), (mib1) }; \
static char buf[1024]; size_t len; \
static char buf[1024]; \
mib[0] = CTL_KERN; char **varp = &(var); \
mib[1] = KERN_OSTYPE; \
len = sizeof(buf); len = sizeof buf; \
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) if (sysctl(mib, sizeof mib / sizeof mib[0], \
&buf, &len, NULL, 0) == -1) \
err(1, "sysctl"); err(1, "sysctl");
sysname = buf;
}
void #define NATIVE_SET \
native_hostname(void) *varp = buf; \
{ return; \
int mib[2]; } struct __hack
size_t len;
static char buf[1024];
mib[0] = CTL_KERN; #define NATIVE_BUFFER (buf)
mib[1] = KERN_HOSTNAME; #define NATIVE_LENGTH (len)
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
err(1, "sysctl");
hostname = buf;
}
void NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
native_release(void) } NATIVE_SET;
{
int mib[2];
size_t len;
static char buf[1024];
mib[0] = CTL_KERN; NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
mib[1] = KERN_OSRELEASE; } NATIVE_SET;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
err(1, "sysctl");
release = buf;
}
void NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
native_version(void) } NATIVE_SET;
{
int mib[2]; NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
size_t len, tlen; size_t n;
char *p; char *p;
static char buf[1024];
mib[0] = CTL_KERN; p = NATIVE_BUFFER;
mib[1] = KERN_VERSION; n = NATIVE_LENGTH;
len = sizeof(buf); for (; n--; ++p)
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
err(1, "sysctl");
for (p = buf, tlen = len; tlen--; ++p)
if (*p == '\n' || *p == '\t') if (*p == '\n' || *p == '\t')
*p = ' '; *p = ' ';
version = buf; } NATIVE_SET;
}
void NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
native_platform(void) } NATIVE_SET;
{
int mib[2];
size_t len;
static char buf[1024];
mib[0] = CTL_HW; NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
mib[1] = HW_MACHINE; } NATIVE_SET;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
err(1, "sysctl");
platform = buf;
}
void
native_arch(void)
{
int mib[2];
size_t len;
static char buf[1024];
mib[0] = CTL_HW;
mib[1] = HW_MACHINE_ARCH;
len = sizeof(buf);
if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
err(1, "sysctl");
arch = buf;
}
void void
usage(void) usage(void)