xref: /dragonfly/usr.bin/uname/uname.c (revision cecb9aae)
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  * @(#) Copyright (c) 1993 The Regents of the University of California.  All rights reserved.
35  * @(#)uname.c	8.2 (Berkeley) 5/4/95
36  * $FreeBSD: src/usr.bin/uname/uname.c,v 1.4.6.2 2002/10/17 07:47:29 jmallett Exp $
37  */
38 
39 #include <sys/param.h>
40 #include <sys/sysctl.h>
41 #include <sys/varsym.h>
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <string.h>
48 
49 #define	MFLAG	0x01
50 #define	NFLAG	0x02
51 #define	PFLAG	0x04
52 #define	RFLAG	0x08
53 #define	SFLAG	0x10
54 #define	VFLAG	0x20
55 #define	IFLAG   0x40
56 
57 typedef void (*get_t)(void);
58 get_t get_ident, get_machine, get_hostname, get_arch;
59 get_t get_release, get_sysname, get_version;
60 
61 void native_ident(void);
62 void native_machine(void);
63 void native_hostname(void);
64 void native_arch(void);
65 void native_release(void);
66 void native_sysname(void);
67 void native_version(void);
68 void print_uname(u_int);
69 void setup_get(void);
70 void usage(void);
71 
72 char *ident, *machine, *hostname, *arch;
73 char *release, *sysname, *version;
74 int space;
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	u_int flags;
80 	int ch;
81 
82 	setup_get();
83 	flags = 0;
84 
85 	while ((ch = getopt(argc, argv, "aimnprsv")) != -1)
86 		switch(ch) {
87 		case 'a':
88 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
89 			break;
90 		case 'i':
91 			flags |= IFLAG;
92 			break;
93 		case 'm':
94 			flags |= MFLAG;
95 			break;
96 		case 'n':
97 			flags |= NFLAG;
98 			break;
99 		case 'p':
100 			flags |= PFLAG;
101 			break;
102 		case 'r':
103 			flags |= RFLAG;
104 			break;
105 		case 's':
106 			flags |= SFLAG;
107 			break;
108 		case 'v':
109 			flags |= VFLAG;
110 			break;
111 		case '?':
112 		default:
113 			usage();
114 		}
115 
116 	argc -= optind;
117 	argv += optind;
118 
119 	if (argc)
120 		usage();
121 
122 	if (!flags)
123 		flags |= SFLAG;
124 
125 	print_uname(flags);
126 	exit(0);
127 }
128 
129 /*
130  * Overrides.
131  *
132  * UNAME_x env variables have the highest priority
133  * UNAME_x varsyms have the next highest priority
134  * values retrieved from sysctls have the lowest priority
135  */
136 static
137 void
138 CHECK_ENV(const char *envname, get_t *getp, get_t nativep, char **varp)
139 {
140 	char buf[1024];
141 
142 	if ((*varp = getenv(envname)) == NULL) {
143 		if (varsym_get(VARSYM_ALL_MASK, envname,
144 			       buf, sizeof(buf)) < 0) {
145 			*getp = nativep;
146 			return;
147 		}
148 		*varp = strdup(buf);
149 	}
150 	*getp = NULL;
151 }
152 
153 void
154 setup_get(void)
155 {
156 	CHECK_ENV("UNAME_s", &get_sysname, native_sysname, &sysname);
157 	CHECK_ENV("UNAME_n", &get_hostname, native_hostname, &hostname);
158 	CHECK_ENV("UNAME_r", &get_release, native_release, &release);
159 	CHECK_ENV("UNAME_v", &get_version, native_version, &version);
160 	CHECK_ENV("UNAME_m", &get_machine, native_machine, &machine);
161 	CHECK_ENV("UNAME_p", &get_arch, native_arch, &arch);
162 	CHECK_ENV("UNAME_i", &get_ident, native_ident, &ident);
163 }
164 
165 #define	PRINT_FLAG(flags,flag,var)		\
166 	if ((flags & flag) == flag) {		\
167 		if (space)			\
168 			printf(" ");		\
169 		else				\
170 			space++;		\
171 		if (get_##var != NULL)		\
172 			(*get_##var)();		\
173 		printf("%s", var);		\
174 	}
175 
176 void
177 print_uname(u_int flags)
178 {
179 	PRINT_FLAG(flags, SFLAG, sysname);
180 	PRINT_FLAG(flags, NFLAG, hostname);
181 	PRINT_FLAG(flags, RFLAG, release);
182 	PRINT_FLAG(flags, VFLAG, version);
183 	PRINT_FLAG(flags, MFLAG, machine);
184 	PRINT_FLAG(flags, PFLAG, arch);
185 	PRINT_FLAG(flags, IFLAG, ident);
186 	printf("\n");
187 }
188 
189 #define	NATIVE_SYSCTL2_GET(var,mib0,mib1)	\
190 void						\
191 native_##var(void)				\
192 {						\
193 	int mib[] = { (mib0), (mib1) };		\
194 	size_t len;				\
195 	static char buf[1024];			\
196 	char **varp = &(var);			\
197 						\
198 	len = sizeof buf;			\
199 	if (sysctl(mib, sizeof mib / sizeof mib[0],	\
200 	   &buf, &len, NULL, 0) == -1)		\
201 		err(1, "sysctl");
202 
203 #define	NATIVE_SYSCTLNAME_GET(var,name)		\
204 void						\
205 native_##var(void)				\
206 {						\
207 	size_t len;				\
208 	static char buf[1024];			\
209 	char **varp = &(var);			\
210 						\
211 	len = sizeof buf;			\
212 	if (sysctlbyname(name, &buf, &len, NULL,\
213 	    0) == -1)				\
214 		err(1, "sysctlbyname");
215 
216 #define	NATIVE_SET				\
217 	*varp = buf;				\
218 	return;					\
219 }	struct __hack
220 
221 #define	NATIVE_BUFFER	(buf)
222 #define	NATIVE_LENGTH	(len)
223 
224 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
225 } NATIVE_SET;
226 
227 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
228 } NATIVE_SET;
229 
230 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
231 } NATIVE_SET;
232 
233 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
234 	size_t n;
235 	char *p;
236 
237 	p = NATIVE_BUFFER;
238 	n = NATIVE_LENGTH;
239 	for (; n--; ++p)
240 		if (*p == '\n' || *p == '\t')
241 			*p = ' ';
242 } NATIVE_SET;
243 
244 NATIVE_SYSCTL2_GET(machine, CTL_HW, HW_MACHINE) {
245 } NATIVE_SET;
246 
247 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
248 } NATIVE_SET;
249 
250 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
251 } NATIVE_SET;
252 
253 void
254 usage(void)
255 {
256 	fprintf(stderr, "usage: uname [-aimnprsv]\n");
257 	exit(1);
258 }
259