Remove doubtful structure definition with variable array members.
I believe this is not a valid C99 construct. Use directly calculated offsets into the supplied buffer, using specified members length, to fill appropriate structure. Either use sysctl, or copy the value of the UNAME_x environment variable, instead of unconditionally doing sysctl, and then overriding a returned value with user-specified one. Noted and tested by: rdivacky
This commit is contained in:
parent
ff80967485
commit
16511f59a9
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=184177
@ -45,94 +45,102 @@ __xuname(int namesize, void *namebuf)
|
||||
{
|
||||
int mib[2], rval;
|
||||
size_t len;
|
||||
char *p;
|
||||
char *p, *q;
|
||||
int oerrno;
|
||||
struct xutsname {
|
||||
char sysname[namesize]; /* Name of this OS. */
|
||||
char nodename[namesize]; /* Name of this network node. */
|
||||
char release[namesize]; /* Release level. */
|
||||
char version[namesize]; /* Version level. */
|
||||
char machine[namesize]; /* Hardware type. */
|
||||
} *name;
|
||||
|
||||
name = (struct xutsname *)namebuf;
|
||||
rval = 0;
|
||||
q = (char *)namebuf;
|
||||
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_OSTYPE;
|
||||
len = sizeof(name->sysname);
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &name->sysname, &len, NULL, 0) == -1) {
|
||||
if(errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
name->sysname[sizeof(name->sysname) - 1] = '\0';
|
||||
|
||||
if ((p = getenv("UNAME_s")))
|
||||
strlcpy(name->sysname, p, sizeof(name->sysname));
|
||||
strlcpy(q, p, namesize);
|
||||
else {
|
||||
mib[1] = KERN_OSTYPE;
|
||||
len = namesize;
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
q[namesize - 1] = '\0';
|
||||
}
|
||||
q += namesize;
|
||||
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_HOSTNAME;
|
||||
len = sizeof(name->nodename);
|
||||
len = namesize;
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &name->nodename, &len, NULL, 0) == -1) {
|
||||
if(errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
name->nodename[sizeof(name->nodename) - 1] = '\0';
|
||||
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_OSRELEASE;
|
||||
len = sizeof(name->release);
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &name->release, &len, NULL, 0) == -1) {
|
||||
if(errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
name->release[sizeof(name->release) - 1] = '\0';
|
||||
if ((p = getenv("UNAME_r")))
|
||||
strlcpy(name->release, p, sizeof(name->release));
|
||||
|
||||
/* The version may have newlines in it, turn them into spaces. */
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_VERSION;
|
||||
len = sizeof(name->version);
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &name->version, &len, NULL, 0) == -1) {
|
||||
if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
name->version[sizeof(name->version) - 1] = '\0';
|
||||
for (p = name->version; len--; ++p) {
|
||||
if (*p == '\n' || *p == '\t') {
|
||||
if (len > 1)
|
||||
*p = ' ';
|
||||
q[namesize - 1] = '\0';
|
||||
q += namesize;
|
||||
|
||||
if ((p = getenv("UNAME_r")))
|
||||
strlcpy(q, p, namesize);
|
||||
else {
|
||||
mib[1] = KERN_OSRELEASE;
|
||||
len = namesize;
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
*p = '\0';
|
||||
rval = -1;
|
||||
}
|
||||
q[namesize - 1] = '\0';
|
||||
}
|
||||
q += namesize;
|
||||
|
||||
if ((p = getenv("UNAME_v")))
|
||||
strlcpy(q, p, namesize);
|
||||
else {
|
||||
|
||||
/*
|
||||
* The version may have newlines in it, turn them into
|
||||
* spaces.
|
||||
*/
|
||||
mib[1] = KERN_VERSION;
|
||||
len = namesize;
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
q[namesize - 1] = '\0';
|
||||
for (p = q; len--; ++p) {
|
||||
if (*p == '\n' || *p == '\t') {
|
||||
if (len > 1)
|
||||
*p = ' ';
|
||||
else
|
||||
*p = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((p = getenv("UNAME_v")))
|
||||
strlcpy(name->version, p, sizeof(name->version));
|
||||
q += namesize;
|
||||
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_MACHINE;
|
||||
len = sizeof(name->machine);
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &name->machine, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
name->machine[sizeof(name->machine) - 1] = '\0';
|
||||
if ((p = getenv("UNAME_m")))
|
||||
strlcpy(name->machine, p, sizeof(name->machine));
|
||||
strlcpy(q, p, namesize);
|
||||
else {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_MACHINE;
|
||||
len = namesize;
|
||||
oerrno = errno;
|
||||
if (sysctl(mib, 2, &q, &len, NULL, 0) == -1) {
|
||||
if (errno == ENOMEM)
|
||||
errno = oerrno;
|
||||
else
|
||||
rval = -1;
|
||||
}
|
||||
q[namesize - 1] = '\0';
|
||||
}
|
||||
|
||||
return (rval);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user