diff options
author | kib <kib@FreeBSD.org> | 2008-10-22 19:55:12 +0000 |
---|---|---|
committer | kib <kib@FreeBSD.org> | 2008-10-22 19:55:12 +0000 |
commit | f1194ec0c51a3911510c9cbd19a18f7564224c7a (patch) | |
tree | f1ec3afc3635284710c709eb0c669d94f4199f33 /lib/libc/gen/__xuname.c | |
parent | bd95a9894955cf48b97e948aef037427d577dde2 (diff) | |
download | FreeBSD-src-f1194ec0c51a3911510c9cbd19a18f7564224c7a.zip FreeBSD-src-f1194ec0c51a3911510c9cbd19a18f7564224c7a.tar.gz |
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
Diffstat (limited to 'lib/libc/gen/__xuname.c')
-rw-r--r-- | lib/libc/gen/__xuname.c | 142 |
1 files changed, 75 insertions, 67 deletions
diff --git a/lib/libc/gen/__xuname.c b/lib/libc/gen/__xuname.c index cf8b21f..f385eb2 100644 --- a/lib/libc/gen/__xuname.c +++ b/lib/libc/gen/__xuname.c @@ -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) + if (sysctl(mib, 2, q, &len, NULL, 0) == -1) { + if (errno == ENOMEM) errno = oerrno; else rval = -1; } - name->nodename[sizeof(name->nodename) - 1] = '\0'; + q[namesize - 1] = '\0'; + q += namesize; - 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 (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 = ' '; + 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(name->version, p, sizeof(name->version)); + strlcpy(q, p, namesize); + else { - 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; + /* + * 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'; + } + } } - name->machine[sizeof(name->machine) - 1] = '\0'; + q += namesize; + 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); } |