xref: /freebsd/usr.bin/uname/uname.c (revision 5b31cc94b10d4bb7109c6b27940a0fc76a44a331)
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 
45 #include <sys/param.h>
46 #include <sys/sysctl.h>
47 
48 #include <err.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 
53 #include <osreldate.h>
54 
55 #define	MFLAG	0x01
56 #define	NFLAG	0x02
57 #define	PFLAG	0x04
58 #define	RFLAG	0x08
59 #define	SFLAG	0x10
60 #define	VFLAG	0x20
61 #define	IFLAG	0x40
62 #define	UFLAG	0x80
63 #define	KFLAG	0x100
64 #define	BFLAG	0x200
65 
66 typedef void (*get_t)(void);
67 static get_t get_buildid, get_ident, get_platform, get_hostname, get_arch,
68     get_release, get_sysname, get_kernvers, get_uservers, get_version;
69 
70 static void native_ident(void);
71 static void native_platform(void);
72 static void native_hostname(void);
73 static void native_arch(void);
74 static void native_release(void);
75 static void native_sysname(void);
76 static void native_version(void);
77 static void native_kernvers(void);
78 static void native_uservers(void);
79 static void native_buildid(void);
80 static void print_uname(u_int);
81 static void setup_get(void);
82 static void usage(void) __dead2;
83 
84 static char *buildid, *ident, *platform, *hostname, *arch, *release, *sysname,
85     *version, *kernvers, *uservers;
86 static int space;
87 
88 int
89 main(int argc, char *argv[])
90 {
91 	u_int flags;
92 	int ch;
93 
94 	setup_get();
95 	flags = 0;
96 
97 	while ((ch = getopt(argc, argv, "abiKmnoprsUv")) != -1)
98 		switch(ch) {
99 		case 'a':
100 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
101 			break;
102 		case 'b':
103 			flags |= BFLAG;
104 			break;
105 		case 'i':
106 			flags |= IFLAG;
107 			break;
108 		case 'K':
109 			flags |= KFLAG;
110 			break;
111 		case 'm':
112 			flags |= MFLAG;
113 			break;
114 		case 'n':
115 			flags |= NFLAG;
116 			break;
117 		case 'p':
118 			flags |= PFLAG;
119 			break;
120 		case 'r':
121 			flags |= RFLAG;
122 			break;
123 		case 's':
124 		case 'o':
125 			flags |= SFLAG;
126 			break;
127 		case 'U':
128 			flags |= UFLAG;
129 			break;
130 		case 'v':
131 			flags |= VFLAG;
132 			break;
133 		case '?':
134 		default:
135 			usage();
136 		}
137 
138 	argc -= optind;
139 	argv += optind;
140 
141 	if (argc)
142 		usage();
143 
144 	if (!flags)
145 		flags |= SFLAG;
146 
147 	print_uname(flags);
148 	exit(0);
149 }
150 
151 #define	CHECK_ENV(opt,var)				\
152 do {							\
153 	if ((var = getenv("UNAME_" opt)) == NULL) {	\
154 		get_##var = native_##var;		\
155 	} else {					\
156 		get_##var = (get_t)NULL;		\
157 	}						\
158 } while (0)
159 
160 static void
161 setup_get(void)
162 {
163 	CHECK_ENV("s", sysname);
164 	CHECK_ENV("n", hostname);
165 	CHECK_ENV("r", release);
166 	CHECK_ENV("v", version);
167 	CHECK_ENV("m", platform);
168 	CHECK_ENV("p", arch);
169 	CHECK_ENV("i", ident);
170 	CHECK_ENV("K", kernvers);
171 	CHECK_ENV("U", uservers);
172 	CHECK_ENV("b", buildid);
173 }
174 
175 #define	PRINT_FLAG(flags,flag,var)		\
176 	if ((flags & flag) == flag) {		\
177 		if (space)			\
178 			printf(" ");		\
179 		else				\
180 			space++;		\
181 		if (get_##var != NULL)		\
182 			(*get_##var)();		\
183 		printf("%s", var);		\
184 	}
185 
186 static void
187 print_uname(u_int flags)
188 {
189 	PRINT_FLAG(flags, SFLAG, sysname);
190 	PRINT_FLAG(flags, NFLAG, hostname);
191 	PRINT_FLAG(flags, RFLAG, release);
192 	PRINT_FLAG(flags, VFLAG, version);
193 	PRINT_FLAG(flags, MFLAG, platform);
194 	PRINT_FLAG(flags, PFLAG, arch);
195 	PRINT_FLAG(flags, IFLAG, ident);
196 	PRINT_FLAG(flags, KFLAG, kernvers);
197 	PRINT_FLAG(flags, UFLAG, uservers);
198 	PRINT_FLAG(flags, BFLAG, buildid);
199 	printf("\n");
200 }
201 
202 #define	NATIVE_SYSCTL2_GET(var,mib0,mib1)	\
203 static void					\
204 native_##var(void)				\
205 {						\
206 	int mib[] = { (mib0), (mib1) };		\
207 	size_t len;				\
208 	static char buf[1024];			\
209 	char **varp = &(var);			\
210 						\
211 	len = sizeof buf;			\
212 	if (sysctl(mib, sizeof mib / sizeof mib[0],	\
213 	   &buf, &len, NULL, 0) == -1)		\
214 		err(1, "sysctl");
215 
216 #define	NATIVE_SYSCTLNAME_GET(var,name)		\
217 static void					\
218 native_##var(void)				\
219 {						\
220 	size_t len;				\
221 	static char buf[1024];			\
222 	char **varp = &(var);			\
223 						\
224 	len = sizeof buf;			\
225 	if (sysctlbyname(name, &buf, &len, NULL,\
226 	    0) == -1)				\
227 		err(1, "sysctlbyname");
228 
229 #define	NATIVE_SET				\
230 	*varp = buf;				\
231 	return;					\
232 }	struct __hack
233 
234 #define	NATIVE_BUFFER	(buf)
235 #define	NATIVE_LENGTH	(len)
236 
237 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
238 } NATIVE_SET;
239 
240 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
241 } NATIVE_SET;
242 
243 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
244 } NATIVE_SET;
245 
246 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
247 	size_t n;
248 	char *p;
249 
250 	p = NATIVE_BUFFER;
251 	n = NATIVE_LENGTH;
252 	for (; n--; ++p)
253 		if (*p == '\n' || *p == '\t') {
254 			if (n > 1)
255 				*p = ' ';
256 			else
257 				*p = '\0';
258 		}
259 } NATIVE_SET;
260 
261 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
262 } NATIVE_SET;
263 
264 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
265 } NATIVE_SET;
266 
267 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
268 } NATIVE_SET;
269 
270 NATIVE_SYSCTLNAME_GET(buildid, "kern.build_id") {
271 } NATIVE_SET;
272 
273 static void
274 native_uservers(void)
275 {
276 	static char buf[128];
277 
278 	snprintf(buf, sizeof(buf), "%d", __FreeBSD_version);
279 	uservers = buf;
280 }
281 
282 static void
283 native_kernvers(void)
284 {
285 	static char buf[128];
286 
287 	snprintf(buf, sizeof(buf), "%d", getosreldate());
288 	kernvers = buf;
289 }
290 
291 static void
292 usage(void)
293 {
294 	fprintf(stderr, "usage: uname [-abiKmnoprsUv]\n");
295 	exit(1);
296 }
297