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