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