xref: /dragonfly/usr.bin/uname/uname.c (revision 75a74ed8)
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. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * @(#) Copyright (c) 1993 The Regents of the University of California.  All rights reserved.
31  * @(#)uname.c	8.2 (Berkeley) 5/4/95
32  * $FreeBSD: src/usr.bin/uname/uname.c,v 1.4.6.2 2002/10/17 07:47:29 jmallett Exp $
33  */
34 
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/varsym.h>
38 
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 
45 #define	MFLAG	0x0001
46 #define	NFLAG	0x0002
47 #define	PFLAG	0x0004
48 #define	RFLAG	0x0008
49 #define	SFLAG	0x0010
50 #define	VFLAG	0x0020
51 #define	IFLAG   0x0040
52 #define	GFLAG   0x0080
53 #define	GFLAG2  0x0100
54 
55 typedef void (*get_t)(void);
56 static get_t get_ident, get_machine, get_hostname, get_arch;
57 static get_t get_release, get_sysname, get_version, get_pkgabi;
58 
59 static void native_ident(void);
60 static void native_machine(void);
61 static void native_hostname(void);
62 static void native_arch(void);
63 static void native_release(void);
64 static void native_sysname(void);
65 static void native_version(void);
66 static void native_pkgabi(void);
67 static void print_uname(void);
68 static void setup_get(void);
69 static void usage(void) __dead2;
70 
71 static char *ident, *machine, *hostname, *arch;
72 static char *release, *sysname, *version, *pkgabi;
73 static int space;
74 static u_int flags;
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	int ch;
80 
81 	setup_get();
82 
83 	while ((ch = getopt(argc, argv, "aimnprsvP")) != -1) {
84 		switch(ch) {
85 		case 'a':
86 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
87 			break;
88 		case 'i':
89 			flags |= IFLAG;
90 			break;
91 		case 'm':
92 			flags |= MFLAG;
93 			break;
94 		case 'n':
95 			flags |= NFLAG;
96 			break;
97 		case 'p':
98 			flags |= PFLAG;
99 			break;
100 		case 'r':
101 			flags |= RFLAG;
102 			break;
103 		case 's':
104 			flags |= SFLAG;
105 			break;
106 		case 'v':
107 			flags |= VFLAG;
108 			break;
109 		case 'P':
110 			if (flags & GFLAG)	/* don't adjust odd numbers */
111 				flags |= GFLAG2;
112 			flags |= GFLAG;
113 			break;
114 		case '?':
115 		default:
116 			usage();
117 		}
118 	}
119 
120 	argc -= optind;
121 	argv += optind;
122 
123 	if (argc)
124 		usage();
125 
126 	if (!flags)
127 		flags |= SFLAG;
128 
129 	print_uname();
130 	exit(0);
131 }
132 
133 /*
134  * Overrides.
135  *
136  * UNAME_x env variables have the highest priority
137  * UNAME_x varsyms have the next highest priority
138  * values retrieved from sysctls have the lowest priority
139  */
140 static
141 void
142 CHECK_ENV(const char *envname, get_t *getp, get_t nativep, char **varp)
143 {
144 	char buf[MAXVARSYM_DATA];
145 
146 	if ((*varp = getenv(envname)) == NULL) {
147 		if (varsym_get(VARSYM_ALL_MASK, envname,
148 			       buf, sizeof(buf)) < 0) {
149 			*getp = nativep;
150 			return;
151 		}
152 		*varp = strdup(buf);
153 	}
154 	*getp = NULL;
155 }
156 
157 static void
158 setup_get(void)
159 {
160 	CHECK_ENV("UNAME_s", &get_sysname, native_sysname, &sysname);
161 	CHECK_ENV("UNAME_n", &get_hostname, native_hostname, &hostname);
162 	CHECK_ENV("UNAME_r", &get_release, native_release, &release);
163 	CHECK_ENV("UNAME_v", &get_version, native_version, &version);
164 	CHECK_ENV("UNAME_m", &get_machine, native_machine, &machine);
165 	CHECK_ENV("UNAME_p", &get_arch, native_arch, &arch);
166 	CHECK_ENV("UNAME_i", &get_ident, native_ident, &ident);
167 	CHECK_ENV("UNAME_G", &get_pkgabi, native_pkgabi, &pkgabi);
168 }
169 
170 #define	PRINT_FLAG(flags,flag,var)		\
171 	if ((flags & flag) == flag) {		\
172 		if (space)			\
173 			printf(" ");		\
174 		else				\
175 			space++;		\
176 		if (get_##var != NULL)		\
177 			(*get_##var)();		\
178 		printf("%s", var);		\
179 	}
180 
181 static void
182 print_uname(void)
183 {
184 	PRINT_FLAG(flags, SFLAG, sysname);
185 	PRINT_FLAG(flags, NFLAG, hostname);
186 	PRINT_FLAG(flags, RFLAG, release);
187 	PRINT_FLAG(flags, VFLAG, version);
188 	PRINT_FLAG(flags, MFLAG, machine);
189 	PRINT_FLAG(flags, PFLAG, arch);
190 	PRINT_FLAG(flags, IFLAG, ident);
191 	PRINT_FLAG(flags, GFLAG, pkgabi);
192 	printf("\n");
193 }
194 
195 #define	NATIVE_SYSCTL2_GET(var,mib0,mib1)	\
196 static void						\
197 native_##var(void)				\
198 {						\
199 	int mib[] = { (mib0), (mib1) };		\
200 	size_t len;				\
201 	static char buf[1024];			\
202 	char **varp = &(var);			\
203 						\
204 	len = sizeof buf;			\
205 	if (sysctl(mib, NELEM(mib),		\
206 	   &buf, &len, NULL, 0) == -1)		\
207 		err(1, "sysctl");
208 
209 #define	NATIVE_SYSCTLNAME_GET(var,name)		\
210 static void						\
211 native_##var(void)				\
212 {						\
213 	size_t len;				\
214 	static char buf[1024];			\
215 	char **varp = &(var);			\
216 						\
217 	len = sizeof buf;			\
218 	if (sysctlbyname(name, &buf, &len, NULL,\
219 	    0) == -1)				\
220 		err(1, "sysctlbyname");
221 
222 #define	NATIVE_SET				\
223 	*varp = buf;				\
224 	return;					\
225 }	struct __hack
226 
227 #define	NATIVE_BUFFER	(buf)
228 #define	NATIVE_LENGTH	(len)
229 
230 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
231 } NATIVE_SET;
232 
233 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
234 } NATIVE_SET;
235 
236 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
237 } NATIVE_SET;
238 
239 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
240 	size_t n;
241 	char *p;
242 
243 	p = NATIVE_BUFFER;
244 	n = NATIVE_LENGTH;
245 	for (; n--; ++p)
246 		if (*p == '\n' || *p == '\t')
247 			*p = ' ';
248 } NATIVE_SET;
249 
250 NATIVE_SYSCTL2_GET(machine, CTL_HW, HW_MACHINE) {
251 } NATIVE_SET;
252 
253 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
254 } NATIVE_SET;
255 
256 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
257 } NATIVE_SET;
258 
259 static void						\
260 native_pkgabi(void)				\
261 {
262 	char osrel[64];
263 	char mach[64];
264 	size_t len;
265 	double d;
266 
267 	len = sizeof(osrel);
268 	if (sysctlbyname("kern.osrelease", osrel, &len, NULL, 0) == -1)
269 		err(1, "sysctlbyname");
270 	len = sizeof(mach);
271 	if (sysctlbyname("hw.machine", mach, &len, NULL, 0) == -1)
272 		err(1, "sysctlbyname");
273 
274 	/*
275 	 * Current convention is to adjust odd release numbers to even.
276 	 */
277 	d = strtod(osrel, NULL);
278 	if ((flags & GFLAG2) == 0) {
279 		if ((int)(d * 10) & 1)
280 			d = d + 0.1;	/* force to nearest even release */
281 	}
282 
283 	/*
284 	 * pkgng expects the ABI in a different form
285 	 */
286 	if (strcmp(mach, "x86_64") == 0)
287 		snprintf(mach, sizeof(mach), "x86:64");
288 	else if (strcmp(mach, "i386") == 0)
289 		snprintf(mach, sizeof(mach), "x86:32");
290 
291 	asprintf(&pkgabi, "dragonfly:%3.1f:%s", d, mach);
292 }
293 
294 static void
295 usage(void)
296 {
297 	fprintf(stderr, "usage: uname [-aimnprsv]\n");
298 	exit(1);
299 }
300