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