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