xref: /freebsd/usr.bin/uname/uname.c (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 2002 Juli Mallett.
5  * Copyright (c) 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)uname.c	8.2 (Berkeley) 5/4/95";
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/sysctl.h>
50 
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 
56 #include <osreldate.h>
57 
58 #define	MFLAG	0x01
59 #define	NFLAG	0x02
60 #define	PFLAG	0x04
61 #define	RFLAG	0x08
62 #define	SFLAG	0x10
63 #define	VFLAG	0x20
64 #define	IFLAG	0x40
65 #define	UFLAG	0x80
66 #define	KFLAG	0x100
67 #define	BFLAG	0x200
68 
69 typedef void (*get_t)(void);
70 static get_t get_buildid, get_ident, get_platform, get_hostname, get_arch,
71     get_release, get_sysname, get_kernvers, get_uservers, get_version;
72 
73 static void native_ident(void);
74 static void native_platform(void);
75 static void native_hostname(void);
76 static void native_arch(void);
77 static void native_release(void);
78 static void native_sysname(void);
79 static void native_version(void);
80 static void native_kernvers(void);
81 static void native_uservers(void);
82 static void native_buildid(void);
83 static void print_uname(u_int);
84 static void setup_get(void);
85 static void usage(void) __dead2;
86 
87 static char *buildid, *ident, *platform, *hostname, *arch, *release, *sysname,
88     *version, *kernvers, *uservers;
89 static int space;
90 
91 int
92 main(int argc, char *argv[])
93 {
94 	u_int flags;
95 	int ch;
96 
97 	setup_get();
98 	flags = 0;
99 
100 	while ((ch = getopt(argc, argv, "abiKmnoprsUv")) != -1)
101 		switch(ch) {
102 		case 'a':
103 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
104 			break;
105 		case 'b':
106 			flags |= BFLAG;
107 			break;
108 		case 'i':
109 			flags |= IFLAG;
110 			break;
111 		case 'K':
112 			flags |= KFLAG;
113 			break;
114 		case 'm':
115 			flags |= MFLAG;
116 			break;
117 		case 'n':
118 			flags |= NFLAG;
119 			break;
120 		case 'p':
121 			flags |= PFLAG;
122 			break;
123 		case 'r':
124 			flags |= RFLAG;
125 			break;
126 		case 's':
127 		case 'o':
128 			flags |= SFLAG;
129 			break;
130 		case 'U':
131 			flags |= UFLAG;
132 			break;
133 		case 'v':
134 			flags |= VFLAG;
135 			break;
136 		case '?':
137 		default:
138 			usage();
139 		}
140 
141 	argc -= optind;
142 	argv += optind;
143 
144 	if (argc)
145 		usage();
146 
147 	if (!flags)
148 		flags |= SFLAG;
149 
150 	print_uname(flags);
151 	exit(0);
152 }
153 
154 #define	CHECK_ENV(opt,var)				\
155 do {							\
156 	if ((var = getenv("UNAME_" opt)) == NULL) {	\
157 		get_##var = native_##var;		\
158 	} else {					\
159 		get_##var = (get_t)NULL;		\
160 	}						\
161 } while (0)
162 
163 static void
164 setup_get(void)
165 {
166 	CHECK_ENV("s", sysname);
167 	CHECK_ENV("n", hostname);
168 	CHECK_ENV("r", release);
169 	CHECK_ENV("v", version);
170 	CHECK_ENV("m", platform);
171 	CHECK_ENV("p", arch);
172 	CHECK_ENV("i", ident);
173 	CHECK_ENV("K", kernvers);
174 	CHECK_ENV("U", uservers);
175 	CHECK_ENV("b", buildid);
176 }
177 
178 #define	PRINT_FLAG(flags,flag,var)		\
179 	if ((flags & flag) == flag) {		\
180 		if (space)			\
181 			printf(" ");		\
182 		else				\
183 			space++;		\
184 		if (get_##var != NULL)		\
185 			(*get_##var)();		\
186 		printf("%s", var);		\
187 	}
188 
189 static void
190 print_uname(u_int flags)
191 {
192 	PRINT_FLAG(flags, SFLAG, sysname);
193 	PRINT_FLAG(flags, NFLAG, hostname);
194 	PRINT_FLAG(flags, RFLAG, release);
195 	PRINT_FLAG(flags, VFLAG, version);
196 	PRINT_FLAG(flags, MFLAG, platform);
197 	PRINT_FLAG(flags, PFLAG, arch);
198 	PRINT_FLAG(flags, IFLAG, ident);
199 	PRINT_FLAG(flags, KFLAG, kernvers);
200 	PRINT_FLAG(flags, UFLAG, uservers);
201 	PRINT_FLAG(flags, BFLAG, buildid);
202 	printf("\n");
203 }
204 
205 #define	NATIVE_SYSCTL2_GET(var,mib0,mib1)	\
206 static void					\
207 native_##var(void)				\
208 {						\
209 	int mib[] = { (mib0), (mib1) };		\
210 	size_t len;				\
211 	static char buf[1024];			\
212 	char **varp = &(var);			\
213 						\
214 	len = sizeof buf;			\
215 	if (sysctl(mib, sizeof mib / sizeof mib[0],	\
216 	   &buf, &len, NULL, 0) == -1)		\
217 		err(1, "sysctl");
218 
219 #define	NATIVE_SYSCTLNAME_GET(var,name)		\
220 static void					\
221 native_##var(void)				\
222 {						\
223 	size_t len;				\
224 	static char buf[1024];			\
225 	char **varp = &(var);			\
226 						\
227 	len = sizeof buf;			\
228 	if (sysctlbyname(name, &buf, &len, NULL,\
229 	    0) == -1)				\
230 		err(1, "sysctlbyname");
231 
232 #define	NATIVE_SET				\
233 	*varp = buf;				\
234 	return;					\
235 }	struct __hack
236 
237 #define	NATIVE_BUFFER	(buf)
238 #define	NATIVE_LENGTH	(len)
239 
240 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
241 } NATIVE_SET;
242 
243 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
244 } NATIVE_SET;
245 
246 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
247 } NATIVE_SET;
248 
249 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
250 	size_t n;
251 	char *p;
252 
253 	p = NATIVE_BUFFER;
254 	n = NATIVE_LENGTH;
255 	for (; n--; ++p)
256 		if (*p == '\n' || *p == '\t') {
257 			if (n > 1)
258 				*p = ' ';
259 			else
260 				*p = '\0';
261 		}
262 } NATIVE_SET;
263 
264 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
265 } NATIVE_SET;
266 
267 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
268 } NATIVE_SET;
269 
270 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
271 } NATIVE_SET;
272 
273 NATIVE_SYSCTLNAME_GET(buildid, "kern.build_id") {
274 } NATIVE_SET;
275 
276 static void
277 native_uservers(void)
278 {
279 	static char buf[128];
280 
281 	snprintf(buf, sizeof(buf), "%d", __FreeBSD_version);
282 	uservers = buf;
283 }
284 
285 static void
286 native_kernvers(void)
287 {
288 	static char buf[128];
289 
290 	snprintf(buf, sizeof(buf), "%d", getosreldate());
291 	kernvers = buf;
292 }
293 
294 static void
295 usage(void)
296 {
297 	fprintf(stderr, "usage: uname [-abiKmnoprsUv]\n");
298 	exit(1);
299 }
300