]> git.saurik.com Git - apple/shell_cmds.git/blob - uname/uname.c
shell_cmds-118.tar.gz
[apple/shell_cmds.git] / uname / uname.c
1 /* $NetBSD: uname.c,v 1.10 1998/11/09 13:24:05 kleink Exp $ */
2
3 /*
4 * Copyright (c) 1994 Winning Strategies, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Winning Strategies, Inc.
18 * 4. The name of Winning Strategies, Inc. may not be used to endorse or
19 * promote products derived from this software without specific prior
20 * written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: uname.c,v 1.10 1998/11/09 13:24:05 kleink Exp $");
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <limits.h>
41 #include <locale.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <err.h>
46
47 #include <sys/sysctl.h>
48 #include <sys/utsname.h>
49
50 #ifdef __APPLE__
51 #include <get_compat.h>
52 #else /* !__APPLE__ */
53 #define COMPAT_MODE(a,b) (1)
54 #endif /* __APPLE__ */
55
56 int main __P((int, char **));
57 static void usage __P((void));
58
59 /* Note that PRINT_MACHINE_ARCH is excluded from PRINT_ALL! */
60 #define PRINT_SYSNAME 0x01
61 #define PRINT_NODENAME 0x02
62 #define PRINT_RELEASE 0x04
63 #define PRINT_VERSION 0x08
64 #define PRINT_MACHINE 0x10
65 #define PRINT_MACHINE_ARCH 0x20
66 #define PRINT_ALL \
67 (PRINT_SYSNAME|PRINT_NODENAME|PRINT_RELEASE|PRINT_VERSION|PRINT_MACHINE)
68
69 int
70 main(argc, argv)
71 int argc;
72 char **argv;
73 {
74 struct utsname u;
75 #ifndef __APPLE__
76 char machine_arch[SYS_NMLN];
77 #endif /* !__APPLE__ */
78 int c;
79 int space = 0;
80 int print_mask = 0;
81
82 (void)setlocale(LC_ALL, "");
83
84 while ((c = getopt(argc,argv,"amnprsv")) != -1) {
85 switch (c) {
86 case 'a':
87 print_mask |= PRINT_ALL;
88 #ifdef __APPLE__
89 if (!COMPAT_MODE("bin/uname", "Unix2003")) {
90 print_mask |= PRINT_MACHINE_ARCH;
91 }
92 #endif /* __APPLE__ */
93 break;
94 case 'm':
95 print_mask |= PRINT_MACHINE;
96 break;
97 case 'n':
98 print_mask |= PRINT_NODENAME;
99 break;
100 case 'p':
101 print_mask |= PRINT_MACHINE_ARCH;
102 break;
103 case 'r':
104 print_mask |= PRINT_RELEASE;
105 break;
106 case 's':
107 print_mask |= PRINT_SYSNAME;
108 break;
109 case 'v':
110 print_mask |= PRINT_VERSION;
111 break;
112 default:
113 usage();
114 /* NOTREACHED */
115 }
116 }
117
118 if (optind != argc) {
119 usage();
120 /* NOTREACHED */
121 }
122
123 if (!print_mask) {
124 print_mask = PRINT_SYSNAME;
125 }
126
127 if (uname(&u) != 0) {
128 err(EXIT_FAILURE, "uname");
129 /* NOTREACHED */
130 }
131 #ifndef __APPLE__
132 if (print_mask & PRINT_MACHINE_ARCH) {
133 int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
134 size_t len = sizeof (machine_arch);
135
136 if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch,
137 &len, NULL, 0) < 0)
138 err(EXIT_FAILURE, "sysctl");
139 }
140 #else /* __APPLE__ */
141 /*
142 * Let's allow the user to override the output of uname via the shell environment.
143 * This is a useful feature for cross-compiling (eg. during an OS bringup).
144 * Otherwise, you have to hack your kernel with the desired strings.
145 */
146 {
147 char *s;
148 s = getenv ("UNAME_SYSNAME"); if (s) strncpy (u.sysname, s, sizeof (u.sysname));
149 s = getenv ("UNAME_NODENAME"); if (s) strncpy (u.nodename, s, sizeof (u.nodename));
150 s = getenv ("UNAME_RELEASE"); if (s) strncpy (u.release, s, sizeof (u.release));
151 s = getenv ("UNAME_VERSION"); if (s) strncpy (u.version, s, sizeof (u.version));
152 s = getenv ("UNAME_MACHINE"); if (s) strncpy (u.machine, s, sizeof (u.machine));
153 }
154 #endif /* !__APPLE__ */
155
156 if (print_mask & PRINT_SYSNAME) {
157 space++;
158 fputs(u.sysname, stdout);
159 }
160 if (print_mask & PRINT_NODENAME) {
161 if (space++) putchar(' ');
162 fputs(u.nodename, stdout);
163 }
164 if (print_mask & PRINT_RELEASE) {
165 if (space++) putchar(' ');
166 fputs(u.release, stdout);
167 }
168 if (print_mask & PRINT_VERSION) {
169 if (space++) putchar(' ');
170 fputs(u.version, stdout);
171 }
172 if (print_mask & PRINT_MACHINE) {
173 if (space++) putchar(' ');
174 fputs(u.machine, stdout);
175 }
176 if (print_mask & PRINT_MACHINE_ARCH) {
177 if (space++) putchar(' ');
178 #ifndef __APPLE__
179 fputs(machine_arch, stdout);
180 #else
181 #if defined(__ppc__)
182 fputs("powerpc", stdout);
183 #elif defined(__i386__)
184 fputs("i386", stdout);
185 #else
186 fputs("unknown", stdout);
187 #endif
188 #endif /* __APPLE__ */
189 }
190 putchar('\n');
191
192 exit(EXIT_SUCCESS);
193 /* NOTREACHED */
194 }
195
196 static void
197 usage()
198 {
199 fprintf(stderr, "usage: uname [-amnprsv]\n");
200 exit(EXIT_FAILURE);
201 }