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: src/usr.bin/uname/uname.c,v 1.4.6.2 2002/10/17 07:47:29 jmallett Exp $");
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>
64 typedef void (*get_t)(void);
65 get_t get_platform, get_hostname, get_arch, get_release, get_sysname, get_version;
67 void native_platform(void);
68 void native_hostname(void);
69 void native_arch(void);
70 void native_release(void);
71 void native_sysname(void);
72 void native_version(void);
73 void print_uname(u_int);
77 char *platform, *hostname, *arch, *release, *sysname, *version;
81 main(int argc, char *argv[])
89 while ((ch = getopt(argc, argv, "amnprsv")) != -1)
92 flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
130 #define CHECK_ENV(opt,var) \
132 if ((var = getenv("UNAME_" opt)) == NULL) { \
133 get_##var = native_##var; \
135 get_##var = (get_t)NULL; \
142 CHECK_ENV("s", sysname);
143 CHECK_ENV("n", hostname);
144 CHECK_ENV("r", release);
145 CHECK_ENV("v", version);
146 CHECK_ENV("m", platform);
147 CHECK_ENV("p", arch);
150 #define PRINT_FLAG(flags,flag,var) \
151 if ((flags & flag) == flag) { \
156 if (get_##var != NULL) \
162 print_uname(u_int flags)
164 PRINT_FLAG(flags, SFLAG, sysname);
165 PRINT_FLAG(flags, NFLAG, hostname);
166 PRINT_FLAG(flags, RFLAG, release);
167 PRINT_FLAG(flags, VFLAG, version);
168 PRINT_FLAG(flags, MFLAG, platform);
169 PRINT_FLAG(flags, PFLAG, arch);
173 #define NATIVE_SYSCTL2_GET(var,mib0,mib1) \
177 int mib[] = { (mib0), (mib1) }; \
179 static char buf[1024]; \
180 char **varp = &(var); \
183 if (sysctl(mib, sizeof mib / sizeof mib[0], \
184 &buf, &len, NULL, 0) == -1) \
192 #define NATIVE_BUFFER (buf)
193 #define NATIVE_LENGTH (len)
195 NATIVE_SYSCTL2_GET(sysname, CTL_KERN, KERN_OSTYPE) {
198 NATIVE_SYSCTL2_GET(hostname, CTL_KERN, KERN_HOSTNAME) {
201 NATIVE_SYSCTL2_GET(release, CTL_KERN, KERN_OSRELEASE) {
204 NATIVE_SYSCTL2_GET(version, CTL_KERN, KERN_VERSION) {
211 if (*p == '\n' || *p == '\t')
215 NATIVE_SYSCTL2_GET(platform, CTL_HW, HW_MACHINE) {
218 NATIVE_SYSCTL2_GET(arch, CTL_HW, HW_MACHINE_ARCH) {
224 fprintf(stderr, "usage: uname [-amnprsv]\n");