]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/__xuname.c
Import DTS files from Linux 5.4
[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/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
39 #include <sys/utsname.h>
40 #include <errno.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 int
45 __xuname(int namesize, void *namebuf)
46 {
47         int mib[2], rval;
48         size_t len;
49         char *p, *q;
50         int oerrno;
51
52         rval = 0;
53         q = (char *)namebuf;
54
55         mib[0] = CTL_KERN;
56
57         if ((p = getenv("UNAME_s")))
58                 strlcpy(q, p, namesize);
59         else {
60                 mib[1] = KERN_OSTYPE;
61                 len = namesize;
62                 oerrno = errno;
63                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
64                         if (errno == ENOMEM)
65                                 errno = oerrno;
66                         else
67                                 rval = -1;
68                 }
69                 q[namesize - 1] = '\0';
70         }
71         q += namesize;
72
73         mib[1] = KERN_HOSTNAME;
74         len = namesize;
75         oerrno = errno;
76         if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
77                 if (errno == ENOMEM)
78                         errno = oerrno;
79                 else
80                         rval = -1;
81         }
82         q[namesize - 1] = '\0';
83         q += namesize;
84
85         if ((p = getenv("UNAME_r")))
86                 strlcpy(q, p, namesize);
87         else {
88                 mib[1] = KERN_OSRELEASE;
89                 len = namesize;
90                 oerrno = errno;
91                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
92                         if (errno == ENOMEM)
93                                 errno = oerrno;
94                         else
95                                 rval = -1;
96                 }
97                 q[namesize - 1] = '\0';
98         }
99         q += namesize;
100
101         if ((p = getenv("UNAME_v")))
102                 strlcpy(q, p, namesize);
103         else {
104
105                 /*
106                  * The version may have newlines in it, turn them into
107                  * spaces.
108                  */
109                 mib[1] = KERN_VERSION;
110                 len = namesize;
111                 oerrno = errno;
112                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
113                         if (errno == ENOMEM)
114                                 errno = oerrno;
115                         else
116                                 rval = -1;
117                 }
118                 q[namesize - 1] = '\0';
119                 for (p = q; len--; ++p) {
120                         if (*p == '\n' || *p == '\t') {
121                                 if (len > 1)
122                                         *p = ' ';
123                                 else
124                                         *p = '\0';
125                         }
126                 }
127         }
128         q += namesize;
129
130         if ((p = getenv("UNAME_m")))
131                 strlcpy(q, p, namesize);
132         else {
133                 mib[0] = CTL_HW;
134                 mib[1] = HW_MACHINE;
135                 len = namesize;
136                 oerrno = errno;
137                 if (sysctl(mib, 2, q, &len, NULL, 0) == -1) {
138                         if (errno == ENOMEM)
139                                 errno = oerrno;
140                         else
141                                 rval = -1;
142                 }
143                 q[namesize - 1] = '\0';
144         }
145
146         return (rval);
147 }