xref: /dflybsd-src/usr.bin/uname/uname.c (revision 744c01d0dc2aa1481a40e5b0988d15691602f5c9)
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  * $DragonFly: src/usr.bin/uname/uname.c,v 1.5 2007/01/04 14:19:03 y0netan1 Exp $
38  */
39 
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 
43 #include <err.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 
48 #ifndef HW_MACHINE_UNAME
49 #define HW_MACHINE_UNAME	HW_MACHINE
50 #endif
51 
52 #define	MFLAG	0x01
53 #define	NFLAG	0x02
54 #define	PFLAG	0x04
55 #define	RFLAG	0x08
56 #define	SFLAG	0x10
57 #define	VFLAG	0x20
58 #define	IFLAG   0x40
59 
60 typedef void (*get_t)(void);
61 get_t get_ident, get_platform, get_hostname, get_arch, get_release, get_sysname, get_version;
62 
63 void native_ident(void);
64 void native_platform(void);
65 void native_hostname(void);
66 void native_arch(void);
67 void native_release(void);
68 void native_sysname(void);
69 void native_version(void);
70 void print_uname(u_int);
71 void setup_get(void);
72 void usage(void);
73 
74 char *ident, *platform, *hostname, *arch, *release, *sysname, *version;
75 int space;
76 
77 int
78 main(int argc, char *argv[])
79 {
80 	u_int flags;
81 	int ch;
82 
83 	setup_get();
84 	flags = 0;
85 
86 	while ((ch = getopt(argc, argv, "aimnprsv")) != -1)
87 		switch(ch) {
88 		case 'a':
89 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
90 			break;
91 		case 'i':
92 			flags |= IFLAG;
93 			break;
94 		case 'm':
95 			flags |= MFLAG;
96 			break;
97 		case 'n':
98 			flags |= NFLAG;
99 			break;
100 		case 'p':
101 			flags |= PFLAG;
102 			break;
103 		case 'r':
104 			flags |= RFLAG;
105 			break;
106 		case 's':
107 			flags |= SFLAG;
108 			break;
109 		case 'v':
110 			flags |= VFLAG;
111 			break;
112 		case '?':
113 		default:
114 			usage();
115 		}
116 
117 	argc -= optind;
118 	argv += optind;
119 
120 	if (argc)
121 		usage();
122 
123 	if (!flags)
124 		flags |= SFLAG;
125 
126 	print_uname(flags);
127 	exit(0);
128 }
129 
130 #define	CHECK_ENV(opt,var)				\
131 do {							\
132 	if ((var = getenv("UNAME_" opt)) == NULL) {	\
133 		get_##var = native_##var;		\
134 	} else {					\
135 		get_##var = (get_t)NULL;		\
136 	}						\
137 } while (0)
138 
139 void
140 setup_get(void)
141 {
142 	CHECK_ENV("s", sysname);
143 	CHECK_ENV("n", hostname);
144 	CHECK_ENV("r", release);
145 	CHECK_ENV("v", version);
146 	CHECK_ENV("m", platform);
147 	CHECK_ENV("p", arch);
148 	CHECK_ENV("i", ident);
149 }
150 
151 #define	PRINT_FLAG(flags,flag,var)		\
152 	if ((flags & flag) == flag) {		\
153 		if (space)			\
154 			printf(" ");		\
155 		else				\
156 			space++;		\
157 		if (get_##var != NULL)		\
158 			(*get_##var)();		\
159 		printf("%s", var);		\
160 	}
161 
162 void
163 print_uname(u_int flags)
164 {
165 	PRINT_FLAG(flags, SFLAG, sysname);
166 	PRINT_FLAG(flags, NFLAG, hostname);
167 	PRINT_FLAG(flags, RFLAG, release);
168 	PRINT_FLAG(flags, VFLAG, version);
169 	PRINT_FLAG(flags, MFLAG, platform);
170 	PRINT_FLAG(flags, PFLAG, arch);
171 	PRINT_FLAG(flags, IFLAG, ident);
172 	printf("\n");
173 }
174 
175 #define	NATIVE_SYSCTL2_GET(var,mib0,mib1)	\
176 void						\
177 native_##var(void)				\
178 {						\
179 	int mib[] = { (mib0), (mib1) };		\
180 	size_t len;				\
181 	static char buf[1024];			\
182 	char **varp = &(var);			\
183 						\
184 	len = sizeof buf;			\
185 	if (sysctl(mib, sizeof mib / sizeof mib[0],	\
186 	   &buf, &len, NULL, 0) == -1)		\
187 		err(1, "sysctl");
188 
189 #define	NATIVE_SYSCTLNAME_GET(var,name)		\
190 void						\
191 native_##var(void)				\
192 {						\
193 	size_t len;				\
194 	static char buf[1024];			\
195 	char **varp = &(var);			\
196 						\
197 	len = sizeof buf;			\
198 	if (sysctlbyname(name, &buf, &len, NULL,\
199 	    0) == -1)				\
200 		err(1, "sysctlbyname");
201 
202 #define	NATIVE_SET				\
203 	*varp = buf;				\
204 	return;					\
205 }	struct __hack
206 
207 #define	NATIVE_BUFFER	(buf)
208 #define	NATIVE_LENGTH	(len)
209 
210 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
211 } NATIVE_SET;
212 
213 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
214 } NATIVE_SET;
215 
216 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
217 } NATIVE_SET;
218 
219 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
220 	size_t n;
221 	char *p;
222 
223 	p = NATIVE_BUFFER;
224 	n = NATIVE_LENGTH;
225 	for (; n--; ++p)
226 		if (*p == '\n' || *p == '\t')
227 			*p = ' ';
228 } NATIVE_SET;
229 
230 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE_UNAME) {
231 } NATIVE_SET;
232 
233 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
234 } NATIVE_SET;
235 
236 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
237 } NATIVE_SET;
238 
239 void
240 usage(void)
241 {
242 	fprintf(stderr, "usage: uname [-aimnprsv]\n");
243 	exit(1);
244 }
245