tools/llvm: Do not build with symbols
[minix3.git] / usr.bin / uname / uname.c
blobc8f9022583032b3ea08345f03b36ac7db8b26ce2
1 /* $NetBSD: uname.c,v 1.11 2011/09/06 18:35:13 joerg Exp $ */
3 /*
4 * Copyright (c) 1994 Winning Strategies, Inc.
5 * All rights reserved.
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.
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.
34 #include <sys/cdefs.h>
35 #ifndef lint
36 __RCSID("$NetBSD: uname.c,v 1.11 2011/09/06 18:35:13 joerg Exp $");
37 #endif /* not lint */
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>
47 #if defined(__minix)
48 #include <string.h>
49 #else
50 #include <sys/sysctl.h>
51 #endif /* !defined(__minix) */
52 #include <sys/utsname.h>
54 __dead static void usage(void);
56 /* Note that PRINT_MACHINE_ARCH is excluded from PRINT_ALL! */
57 #define PRINT_SYSNAME 0x01
58 #define PRINT_NODENAME 0x02
59 #define PRINT_RELEASE 0x04
60 #define PRINT_VERSION 0x08
61 #define PRINT_MACHINE 0x10
62 #define PRINT_MACHINE_ARCH 0x20
63 #define PRINT_ALL \
64 (PRINT_SYSNAME|PRINT_NODENAME|PRINT_RELEASE|PRINT_VERSION|PRINT_MACHINE)
66 int
67 main(int argc, char **argv)
69 struct utsname u;
70 char machine_arch[SYS_NMLN];
71 int c;
72 int space = 0;
73 int print_mask = 0;
75 (void)setlocale(LC_ALL, "");
77 while ((c = getopt(argc,argv,"amnprsv")) != -1) {
78 switch (c) {
79 case 'a':
80 print_mask |= PRINT_ALL;
81 break;
82 case 'm':
83 print_mask |= PRINT_MACHINE;
84 break;
85 case 'n':
86 print_mask |= PRINT_NODENAME;
87 break;
88 case 'p':
89 print_mask |= PRINT_MACHINE_ARCH;
90 break;
91 case 'r':
92 print_mask |= PRINT_RELEASE;
93 break;
94 case 's':
95 print_mask |= PRINT_SYSNAME;
96 break;
97 case 'v':
98 print_mask |= PRINT_VERSION;
99 break;
100 default:
101 usage();
102 /* NOTREACHED */
106 if (optind != argc) {
107 usage();
108 /* NOTREACHED */
111 if (!print_mask) {
112 print_mask = PRINT_SYSNAME;
115 if (uname(&u) != 0) {
116 err(EXIT_FAILURE, "uname");
117 /* NOTREACHED */
119 if (print_mask & PRINT_MACHINE_ARCH) {
120 #if defined(__minix)
121 strlcpy(machine_arch, u.arch, sizeof(machine_arch));
122 #else
123 int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
124 size_t len = sizeof (machine_arch);
126 if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch,
127 &len, NULL, 0) < 0)
128 err(EXIT_FAILURE, "sysctl");
129 #endif /* defined(__minix) */
132 if (print_mask & PRINT_SYSNAME) {
133 space++;
134 fputs(u.sysname, stdout);
136 if (print_mask & PRINT_NODENAME) {
137 if (space++) putchar(' ');
138 fputs(u.nodename, stdout);
140 if (print_mask & PRINT_RELEASE) {
141 if (space++) putchar(' ');
142 fputs(u.release, stdout);
144 if (print_mask & PRINT_VERSION) {
145 if (space++) putchar(' ');
146 fputs(u.version, stdout);
148 if (print_mask & PRINT_MACHINE) {
149 if (space++) putchar(' ');
150 fputs(u.machine, stdout);
152 if (print_mask & PRINT_MACHINE_ARCH) {
153 if (space++) putchar(' ');
154 fputs(machine_arch, stdout);
156 putchar('\n');
158 exit(EXIT_SUCCESS);
159 /* NOTREACHED */
162 static void
163 usage(void)
165 fprintf(stderr, "usage: uname [-amnprsv]\n");
166 exit(EXIT_FAILURE);