Make vmstat -m understand the new per-cpu aggregation for
[dragonfly.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  * @(#) 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.2 2003/06/17 04:29:33 dillon 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 #define MFLAG   0x01
49 #define NFLAG   0x02
50 #define PFLAG   0x04
51 #define RFLAG   0x08
52 #define SFLAG   0x10
53 #define VFLAG   0x20
54
55 typedef void (*get_t)(void);
56 get_t get_platform, get_hostname, get_arch, get_release, get_sysname, get_version;
57
58 void native_platform(void);
59 void native_hostname(void);
60 void native_arch(void);
61 void native_release(void);
62 void native_sysname(void);
63 void native_version(void);
64 void print_uname(u_int);
65 void setup_get(void);
66 void usage(void);
67
68 char *platform, *hostname, *arch, *release, *sysname, *version;
69 int space;
70
71 int
72 main(int argc, char *argv[])
73 {
74         u_int flags;
75         int ch;
76
77         setup_get();
78         flags = 0;
79
80         while ((ch = getopt(argc, argv, "amnprsv")) != -1)
81                 switch(ch) {
82                 case 'a':
83                         flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
84                         break;
85                 case 'm':
86                         flags |= MFLAG;
87                         break;
88                 case 'n':
89                         flags |= NFLAG;
90                         break;
91                 case 'p':
92                         flags |= PFLAG;
93                         break;
94                 case 'r':
95                         flags |= RFLAG;
96                         break;
97                 case 's':
98                         flags |= SFLAG;
99                         break;
100                 case 'v':
101                         flags |= VFLAG;
102                         break;
103                 case '?':
104                 default:
105                         usage();
106                 }
107
108         argc -= optind;
109         argv += optind;
110
111         if (argc)
112                 usage();
113
114         if (!flags)
115                 flags |= SFLAG;
116
117         print_uname(flags);
118         exit(0);
119 }
120
121 #define CHECK_ENV(opt,var)                              \
122 do {                                                    \
123         if ((var = getenv("UNAME_" opt)) == NULL) {     \
124                 get_##var = native_##var;               \
125         } else {                                        \
126                 get_##var = (get_t)NULL;                \
127         }                                               \
128 } while (0)
129
130 void
131 setup_get(void)
132 {
133         CHECK_ENV("s", sysname);
134         CHECK_ENV("n", hostname);
135         CHECK_ENV("r", release);
136         CHECK_ENV("v", version);
137         CHECK_ENV("m", platform);
138         CHECK_ENV("p", arch);
139 }
140
141 #define PRINT_FLAG(flags,flag,var)              \
142         if ((flags & flag) == flag) {           \
143                 if (space)                      \
144                         printf(" ");            \
145                 else                            \
146                         space++;                \
147                 if (get_##var != NULL)          \
148                         (*get_##var)();         \
149                 printf("%s", var);              \
150         }
151
152 void
153 print_uname(u_int flags)
154 {
155         PRINT_FLAG(flags, SFLAG, sysname);
156         PRINT_FLAG(flags, NFLAG, hostname);
157         PRINT_FLAG(flags, RFLAG, release);
158         PRINT_FLAG(flags, VFLAG, version);
159         PRINT_FLAG(flags, MFLAG, platform);
160         PRINT_FLAG(flags, PFLAG, arch);
161         printf("\n");
162 }
163
164 #define NATIVE_SYSCTL2_GET(var,mib0,mib1)       \
165 void                                            \
166 native_##var(void)                              \
167 {                                               \
168         int mib[] = { (mib0), (mib1) };         \
169         size_t len;                             \
170         static char buf[1024];                  \
171         char **varp = &(var);                   \
172                                                 \
173         len = sizeof buf;                       \
174         if (sysctl(mib, sizeof mib / sizeof mib[0],     \
175            &buf, &len, NULL, 0) == -1)          \
176                 err(1, "sysctl");
177
178 #define NATIVE_SET                              \
179         *varp = buf;                            \
180         return;                                 \
181 }       struct __hack
182
183 #define NATIVE_BUFFER   (buf)
184 #define NATIVE_LENGTH   (len)
185
186 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
187 } NATIVE_SET;
188
189 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
190 } NATIVE_SET;
191
192 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
193 } NATIVE_SET;
194
195 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
196         size_t n;
197         char *p;
198
199         p = NATIVE_BUFFER;
200         n = NATIVE_LENGTH;
201         for (; n--; ++p)
202                 if (*p == '\n' || *p == '\t')
203                         *p = ' ';
204 } NATIVE_SET;
205
206 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
207 } NATIVE_SET;
208
209 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
210 } NATIVE_SET;
211
212 void
213 usage(void)
214 {
215         fprintf(stderr, "usage: uname [-amnprsv]\n");
216         exit(1);
217 }