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