]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/__xuname.c
libevent: Import libevent 2.1.12
[FreeBSD/FreeBSD.git] / lib / libc / gen / __xuname.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * From: @(#)uname.c    8.1 (Berkeley) 1/4/94
32  */
33
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <sys/utsname.h>
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 int
42 __xuname(int namesize, void *namebuf)
43 {
44         int mib[2], rval;
45         size_t len;
46         char *p, *q;
47         int oerrno;
48
49         rval = 0;
50         q = (char *)namebuf;
51
52         mib[0] = CTL_KERN;
53
54         if ((p = getenv("UNAME_s")))
55                 strlcpy(q, p, namesize);
56         else {
57                 mib[1] = KERN_OSTYPE;
58                 len = namesize;
59                 oerrno = errno;
60                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
61                         if (errno == ENOMEM)
62                                 errno = oerrno;
63                         else
64                                 rval = -1;
65                 }
66                 q[namesize - 1] = '\0';
67         }
68         q += namesize;
69
70         mib[1] = KERN_HOSTNAME;
71         len = namesize;
72         oerrno = errno;
73         if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
74                 if (errno == ENOMEM)
75                         errno = oerrno;
76                 else
77                         rval = -1;
78         }
79         q[namesize - 1] = '\0';
80         q += namesize;
81
82         if ((p = getenv("UNAME_r")))
83                 strlcpy(q, p, namesize);
84         else {
85                 mib[1] = KERN_OSRELEASE;
86                 len = namesize;
87                 oerrno = errno;
88                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
89                         if (errno == ENOMEM)
90                                 errno = oerrno;
91                         else
92                                 rval = -1;
93                 }
94                 q[namesize - 1] = '\0';
95         }
96         q += namesize;
97
98         if ((p = getenv("UNAME_v")))
99                 strlcpy(q, p, namesize);
100         else {
101
102                 /*
103                  * The version may have newlines in it, turn them into
104                  * spaces.
105                  */
106                 mib[1] = KERN_VERSION;
107                 len = namesize;
108                 oerrno = errno;
109                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
110                         if (errno == ENOMEM)
111                                 errno = oerrno;
112                         else
113                                 rval = -1;
114                 }
115                 q[namesize - 1] = '\0';
116                 for (p = q; len--; ++p) {
117                         if (*p == '\n' || *p == '\t') {
118                                 if (len > 1)
119                                         *p = ' ';
120                                 else
121                                         *p = '\0';
122                         }
123                 }
124         }
125         q += namesize;
126
127         if ((p = getenv("UNAME_m")))
128                 strlcpy(q, p, namesize);
129         else {
130                 mib[0] = CTL_HW;
131                 mib[1] = HW_MACHINE;
132                 len = namesize;
133                 oerrno = errno;
134                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
135                         if (errno == ENOMEM)
136                                 errno = oerrno;
137                         else
138                                 rval = -1;
139                 }
140                 q[namesize - 1] = '\0';
141         }
142
143         return (rval);
144 }