2 * Copyright (c) 2002 Juli Mallett.
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 #include <sys/varsym.h>
53 typedef void (*get_t)(void);
54 get_t get_ident, get_machine, get_hostname, get_arch;
55 get_t get_release, get_sysname, get_version;
57 void native_ident(void);
58 void native_machine(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);
68 char *ident, *machine, *hostname, *arch;
69 char *release, *sysname, *version;
73 main(int argc, char *argv[])
81 while ((ch = getopt(argc, argv, "aimnprsv")) != -1)
84 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
128 * UNAME_x env variables have the highest priority
129 * UNAME_x varsyms have the next highest priority
130 * values retrieved from sysctls have the lowest priority
134 CHECK_ENV(const char *envname, get_t *getp, get_t nativep, char **varp)
138 if ((*varp = getenv(envname)) == NULL) {
139 if (varsym_get(VARSYM_ALL_MASK, envname,
140 buf, sizeof(buf)) < 0) {
152 CHECK_ENV("UNAME_s", &get_sysname, native_sysname, &sysname);
153 CHECK_ENV("UNAME_n", &get_hostname, native_hostname, &hostname);
154 CHECK_ENV("UNAME_r", &get_release, native_release, &release);
155 CHECK_ENV("UNAME_v", &get_version, native_version, &version);
156 CHECK_ENV("UNAME_m", &get_machine, native_machine, &machine);
157 CHECK_ENV("UNAME_p", &get_arch, native_arch, &arch);
158 CHECK_ENV("UNAME_i", &get_ident, native_ident, &ident);
161 #define PRINT_FLAG(flags,flag,var) \
162 if ((flags & flag) == flag) { \
167 if (get_##var != NULL) \
173 print_uname(u_int flags)
175 PRINT_FLAG(flags, SFLAG, sysname);
176 PRINT_FLAG(flags, NFLAG, hostname);
177 PRINT_FLAG(flags, RFLAG, release);
178 PRINT_FLAG(flags, VFLAG, version);
179 PRINT_FLAG(flags, MFLAG, machine);
180 PRINT_FLAG(flags, PFLAG, arch);
181 PRINT_FLAG(flags, IFLAG, ident);
185 #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \
189 int mib[] = { (mib0), (mib1) }; \
191 static char buf[1024]; \
192 char **varp = &(var); \
195 if (sysctl(mib, sizeof mib / sizeof mib[0], \
196 &buf, &len, NULL, 0) == -1) \
199 #define NATIVE_SYSCTLNAME_GET(var,name) \
204 static char buf[1024]; \
205 char **varp = &(var); \
208 if (sysctlbyname(name, &buf, &len, NULL,\
210 err(1, "sysctlbyname");
217 #define NATIVE_BUFFER (buf)
218 #define NATIVE_LENGTH (len)
220 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
223 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
226 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
229 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
236 if (*p == '\n' || *p == '\t')
240 NATIVE_SYSCTL2_GET(machine, CTL_HW, HW_MACHINE) {
243 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
246 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
252 fprintf(stderr, "usage: uname [-aimnprsv]\n");