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/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 static const char copyright[] =
41 "@(#) Copyright (c) 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
46 static const char sccsid[] = "@(#)uname.c 8.2 (Berkeley) 5/4/95";
49 #include <sys/param.h>
50 #include <sys/sysctl.h>
57 #include <osreldate.h>
69 typedef void (*get_t)(void);
70 static get_t get_ident, get_platform, get_hostname, get_arch,
71 get_release, get_sysname, get_kernvers, get_uservers, get_version;
73 static void native_ident(void);
74 static void native_platform(void);
75 static void native_hostname(void);
76 static void native_arch(void);
77 static void native_release(void);
78 static void native_sysname(void);
79 static void native_version(void);
80 static void native_kernvers(void);
81 static void native_uservers(void);
82 static void print_uname(u_int);
83 static void setup_get(void);
84 static void usage(void);
86 static char *ident, *platform, *hostname, *arch, *release, *sysname, *version, *kernvers, *uservers;
90 main(int argc, char *argv[])
98 while ((ch = getopt(argc, argv, "aiKmnoprsUv")) != -1)
101 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
149 #define CHECK_ENV(opt,var) \
151 if ((var = getenv("UNAME_" opt)) == NULL) { \
152 get_##var = native_##var; \
154 get_##var = (get_t)NULL; \
161 CHECK_ENV("s", sysname);
162 CHECK_ENV("n", hostname);
163 CHECK_ENV("r", release);
164 CHECK_ENV("v", version);
165 CHECK_ENV("m", platform);
166 CHECK_ENV("p", arch);
167 CHECK_ENV("i", ident);
168 CHECK_ENV("K", kernvers);
169 CHECK_ENV("U", uservers);
172 #define PRINT_FLAG(flags,flag,var) \
173 if ((flags & flag) == flag) { \
178 if (get_##var != NULL) \
184 print_uname(u_int flags)
186 PRINT_FLAG(flags, SFLAG, sysname);
187 PRINT_FLAG(flags, NFLAG, hostname);
188 PRINT_FLAG(flags, RFLAG, release);
189 PRINT_FLAG(flags, VFLAG, version);
190 PRINT_FLAG(flags, MFLAG, platform);
191 PRINT_FLAG(flags, PFLAG, arch);
192 PRINT_FLAG(flags, IFLAG, ident);
193 PRINT_FLAG(flags, KFLAG, kernvers);
194 PRINT_FLAG(flags, UFLAG, uservers);
198 #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \
202 int mib[] = { (mib0), (mib1) }; \
204 static char buf[1024]; \
205 char **varp = &(var); \
208 if (sysctl(mib, sizeof mib / sizeof mib[0], \
209 &buf, &len, NULL, 0) == -1) \
212 #define NATIVE_SYSCTLNAME_GET(var,name) \
217 static char buf[1024]; \
218 char **varp = &(var); \
221 if (sysctlbyname(name, &buf, &len, NULL,\
223 err(1, "sysctlbyname");
230 #define NATIVE_BUFFER (buf)
231 #define NATIVE_LENGTH (len)
233 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
236 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
239 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
242 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
249 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") {
263 native_uservers(void)
265 static char buf[128];
267 snprintf(buf, sizeof(buf), "%d", __FreeBSD_version);
272 native_kernvers(void)
274 static char buf[128];
276 snprintf(buf, sizeof(buf), "%d", getosreldate());
283 fprintf(stderr, "usage: uname [-aiKmnoprsUv]\n");