/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 19944* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <sys/param.h>32#include <sys/sysctl.h>33#include <sys/utsname.h>34#include <errno.h>35#include <stdlib.h>36#include <string.h>3738int39__xuname(int namesize, void *namebuf)40{41int mib[2], rval;42size_t len;43char *p, *q;44int oerrno;4546rval = 0;47q = (char *)namebuf;4849mib[0] = CTL_KERN;5051if ((p = getenv("UNAME_s")))52strlcpy(q, p, namesize);53else {54mib[1] = KERN_OSTYPE;55len = namesize;56oerrno = errno;57if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {58if (errno == ENOMEM)59errno = oerrno;60else61rval = -1;62}63q[namesize - 1] = '\0';64}65q += namesize;6667mib[1] = KERN_HOSTNAME;68len = namesize;69oerrno = errno;70if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {71if (errno == ENOMEM)72errno = oerrno;73else74rval = -1;75}76q[namesize - 1] = '\0';77q += namesize;7879if ((p = getenv("UNAME_r")))80strlcpy(q, p, namesize);81else {82mib[1] = KERN_OSRELEASE;83len = namesize;84oerrno = errno;85if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {86if (errno == ENOMEM)87errno = oerrno;88else89rval = -1;90}91q[namesize - 1] = '\0';92}93q += namesize;9495if ((p = getenv("UNAME_v")))96strlcpy(q, p, namesize);97else {9899/*100* The version may have newlines in it, turn them into101* spaces.102*/103mib[1] = KERN_VERSION;104len = namesize;105oerrno = errno;106if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {107if (errno == ENOMEM)108errno = oerrno;109else110rval = -1;111}112q[namesize - 1] = '\0';113for (p = q; len--; ++p) {114if (*p == '\n' || *p == '\t') {115if (len > 1)116*p = ' ';117else118*p = '\0';119}120}121}122q += namesize;123124if ((p = getenv("UNAME_m")))125strlcpy(q, p, namesize);126else {127mib[0] = CTL_HW;128mib[1] = HW_MACHINE;129len = namesize;130oerrno = errno;131if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {132if (errno == ENOMEM)133errno = oerrno;134else135rval = -1;136}137q[namesize - 1] = '\0';138}139140return (rval);141}142143144