2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 2002 Juli Mallett.
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
37 #include <sys/param.h>
38 #include <sys/sysctl.h>
45 #include <osreldate.h>
58 typedef void (*get_t)(void);
59 static get_t get_buildid, get_ident, get_platform, get_hostname, get_arch,
60 get_release, get_sysname, get_kernvers, get_uservers, get_version;
62 static void native_ident(void);
63 static void native_platform(void);
64 static void native_hostname(void);
65 static void native_arch(void);
66 static void native_release(void);
67 static void native_sysname(void);
68 static void native_version(void);
69 static void native_kernvers(void);
70 static void native_uservers(void);
71 static void native_buildid(void);
72 static void print_uname(u_int);
73 static void setup_get(void);
74 static void usage(void) __dead2;
76 static char *buildid, *ident, *platform, *hostname, *arch, *release, *sysname,
77 *version, *kernvers, *uservers;
81 main(int argc, char *argv[])
89 while ((ch = getopt(argc, argv, "abiKmnoprsUv")) != -1)
92 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
143 #define CHECK_ENV(opt,var) \
145 if ((var = getenv("UNAME_" opt)) == NULL) { \
146 get_##var = native_##var; \
148 get_##var = (get_t)NULL; \
155 CHECK_ENV("s", sysname);
156 CHECK_ENV("n", hostname);
157 CHECK_ENV("r", release);
158 CHECK_ENV("v", version);
159 CHECK_ENV("m", platform);
160 CHECK_ENV("p", arch);
161 CHECK_ENV("i", ident);
162 CHECK_ENV("K", kernvers);
163 CHECK_ENV("U", uservers);
164 CHECK_ENV("b", buildid);
167 #define PRINT_FLAG(flags,flag,var) \
168 if ((flags & flag) == flag) { \
173 if (get_##var != NULL) \
179 print_uname(u_int flags)
181 PRINT_FLAG(flags, SFLAG, sysname);
182 PRINT_FLAG(flags, NFLAG, hostname);
183 PRINT_FLAG(flags, RFLAG, release);
184 PRINT_FLAG(flags, VFLAG, version);
185 PRINT_FLAG(flags, MFLAG, platform);
186 PRINT_FLAG(flags, PFLAG, arch);
187 PRINT_FLAG(flags, IFLAG, ident);
188 PRINT_FLAG(flags, KFLAG, kernvers);
189 PRINT_FLAG(flags, UFLAG, uservers);
190 PRINT_FLAG(flags, BFLAG, buildid);
194 #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \
198 int mib[] = { (mib0), (mib1) }; \
200 static char buf[1024]; \
201 char **varp = &(var); \
204 if (sysctl(mib, sizeof mib / sizeof mib[0], \
205 &buf, &len, NULL, 0) == -1) \
208 #define NATIVE_SYSCTLNAME_GET(var,name) \
213 static char buf[1024]; \
214 char **varp = &(var); \
217 if (sysctlbyname(name, &buf, &len, NULL,\
219 err(1, "sysctlbyname");
226 #define NATIVE_BUFFER (buf)
227 #define NATIVE_LENGTH (len)
229 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
232 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
235 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
238 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
245 if (*p == '\n' || *p == '\t') {
253 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
256 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
259 NATIVE_SYSCTLNAME_GET(ident, "kern.ident") {
262 NATIVE_SYSCTLNAME_GET(buildid, "kern.build_id") {
266 native_uservers(void)
268 static char buf[128];
270 snprintf(buf, sizeof(buf), "%d", __FreeBSD_version);
275 native_kernvers(void)
277 static char buf[128];
279 snprintf(buf, sizeof(buf), "%d", getosreldate());
286 fprintf(stderr, "usage: uname [-abiKmnoprsUv]\n");