1*c0c51139SThomas Cort /* $NetBSD: uname.c,v 1.11 2011/09/06 18:35:13 joerg Exp $ */
2*c0c51139SThomas Cort
3*c0c51139SThomas Cort /*
4*c0c51139SThomas Cort * Copyright (c) 1994 Winning Strategies, Inc.
5*c0c51139SThomas Cort * All rights reserved.
6*c0c51139SThomas Cort *
7*c0c51139SThomas Cort * Redistribution and use in source and binary forms, with or without
8*c0c51139SThomas Cort * modification, are permitted provided that the following conditions
9*c0c51139SThomas Cort * are met:
10*c0c51139SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*c0c51139SThomas Cort * notice, this list of conditions and the following disclaimer.
12*c0c51139SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*c0c51139SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*c0c51139SThomas Cort * documentation and/or other materials provided with the distribution.
15*c0c51139SThomas Cort * 3. All advertising materials mentioning features or use of this software
16*c0c51139SThomas Cort * must display the following acknowledgement:
17*c0c51139SThomas Cort * This product includes software developed by Winning Strategies, Inc.
18*c0c51139SThomas Cort * 4. The name of Winning Strategies, Inc. may not be used to endorse or
19*c0c51139SThomas Cort * promote products derived from this software without specific prior
20*c0c51139SThomas Cort * written permission.
21*c0c51139SThomas Cort *
22*c0c51139SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23*c0c51139SThomas Cort * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24*c0c51139SThomas Cort * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25*c0c51139SThomas Cort * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26*c0c51139SThomas Cort * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27*c0c51139SThomas Cort * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*c0c51139SThomas Cort * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*c0c51139SThomas Cort * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*c0c51139SThomas Cort * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31*c0c51139SThomas Cort * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*c0c51139SThomas Cort */
33*c0c51139SThomas Cort
34*c0c51139SThomas Cort #include <sys/cdefs.h>
35*c0c51139SThomas Cort #ifndef lint
36*c0c51139SThomas Cort __RCSID("$NetBSD: uname.c,v 1.11 2011/09/06 18:35:13 joerg Exp $");
37*c0c51139SThomas Cort #endif /* not lint */
38*c0c51139SThomas Cort
39*c0c51139SThomas Cort #include <sys/param.h>
40*c0c51139SThomas Cort #include <limits.h>
41*c0c51139SThomas Cort #include <locale.h>
42*c0c51139SThomas Cort #include <stdio.h>
43*c0c51139SThomas Cort #include <stdlib.h>
44*c0c51139SThomas Cort #include <unistd.h>
45*c0c51139SThomas Cort #include <err.h>
46*c0c51139SThomas Cort
47*c0c51139SThomas Cort #include <sys/sysctl.h>
48*c0c51139SThomas Cort #include <sys/utsname.h>
49*c0c51139SThomas Cort
50*c0c51139SThomas Cort __dead static void usage(void);
51*c0c51139SThomas Cort
52*c0c51139SThomas Cort /* Note that PRINT_MACHINE_ARCH is excluded from PRINT_ALL! */
53*c0c51139SThomas Cort #define PRINT_SYSNAME 0x01
54*c0c51139SThomas Cort #define PRINT_NODENAME 0x02
55*c0c51139SThomas Cort #define PRINT_RELEASE 0x04
56*c0c51139SThomas Cort #define PRINT_VERSION 0x08
57*c0c51139SThomas Cort #define PRINT_MACHINE 0x10
58*c0c51139SThomas Cort #define PRINT_MACHINE_ARCH 0x20
59*c0c51139SThomas Cort #define PRINT_ALL \
60*c0c51139SThomas Cort (PRINT_SYSNAME|PRINT_NODENAME|PRINT_RELEASE|PRINT_VERSION|PRINT_MACHINE)
61*c0c51139SThomas Cort
62*c0c51139SThomas Cort int
main(int argc,char ** argv)63*c0c51139SThomas Cort main(int argc, char **argv)
64*c0c51139SThomas Cort {
65*c0c51139SThomas Cort struct utsname u;
66*c0c51139SThomas Cort char machine_arch[SYS_NMLN];
67*c0c51139SThomas Cort int c;
68*c0c51139SThomas Cort int space = 0;
69*c0c51139SThomas Cort int print_mask = 0;
70*c0c51139SThomas Cort
71*c0c51139SThomas Cort (void)setlocale(LC_ALL, "");
72*c0c51139SThomas Cort
73*c0c51139SThomas Cort while ((c = getopt(argc,argv,"amnprsv")) != -1) {
74*c0c51139SThomas Cort switch (c) {
75*c0c51139SThomas Cort case 'a':
76*c0c51139SThomas Cort print_mask |= PRINT_ALL;
77*c0c51139SThomas Cort break;
78*c0c51139SThomas Cort case 'm':
79*c0c51139SThomas Cort print_mask |= PRINT_MACHINE;
80*c0c51139SThomas Cort break;
81*c0c51139SThomas Cort case 'n':
82*c0c51139SThomas Cort print_mask |= PRINT_NODENAME;
83*c0c51139SThomas Cort break;
84*c0c51139SThomas Cort case 'p':
85*c0c51139SThomas Cort print_mask |= PRINT_MACHINE_ARCH;
86*c0c51139SThomas Cort break;
87*c0c51139SThomas Cort case 'r':
88*c0c51139SThomas Cort print_mask |= PRINT_RELEASE;
89*c0c51139SThomas Cort break;
90*c0c51139SThomas Cort case 's':
91*c0c51139SThomas Cort print_mask |= PRINT_SYSNAME;
92*c0c51139SThomas Cort break;
93*c0c51139SThomas Cort case 'v':
94*c0c51139SThomas Cort print_mask |= PRINT_VERSION;
95*c0c51139SThomas Cort break;
96*c0c51139SThomas Cort default:
97*c0c51139SThomas Cort usage();
98*c0c51139SThomas Cort /* NOTREACHED */
99*c0c51139SThomas Cort }
100*c0c51139SThomas Cort }
101*c0c51139SThomas Cort
102*c0c51139SThomas Cort if (optind != argc) {
103*c0c51139SThomas Cort usage();
104*c0c51139SThomas Cort /* NOTREACHED */
105*c0c51139SThomas Cort }
106*c0c51139SThomas Cort
107*c0c51139SThomas Cort if (!print_mask) {
108*c0c51139SThomas Cort print_mask = PRINT_SYSNAME;
109*c0c51139SThomas Cort }
110*c0c51139SThomas Cort
111*c0c51139SThomas Cort if (uname(&u) != 0) {
112*c0c51139SThomas Cort err(EXIT_FAILURE, "uname");
113*c0c51139SThomas Cort /* NOTREACHED */
114*c0c51139SThomas Cort }
115*c0c51139SThomas Cort if (print_mask & PRINT_MACHINE_ARCH) {
116*c0c51139SThomas Cort int mib[2] = { CTL_HW, HW_MACHINE_ARCH };
117*c0c51139SThomas Cort size_t len = sizeof (machine_arch);
118*c0c51139SThomas Cort
119*c0c51139SThomas Cort if (sysctl(mib, sizeof (mib) / sizeof (mib[0]), machine_arch,
120*c0c51139SThomas Cort &len, NULL, 0) < 0)
121*c0c51139SThomas Cort err(EXIT_FAILURE, "sysctl");
122*c0c51139SThomas Cort }
123*c0c51139SThomas Cort
124*c0c51139SThomas Cort if (print_mask & PRINT_SYSNAME) {
125*c0c51139SThomas Cort space++;
126*c0c51139SThomas Cort fputs(u.sysname, stdout);
127*c0c51139SThomas Cort }
128*c0c51139SThomas Cort if (print_mask & PRINT_NODENAME) {
129*c0c51139SThomas Cort if (space++) putchar(' ');
130*c0c51139SThomas Cort fputs(u.nodename, stdout);
131*c0c51139SThomas Cort }
132*c0c51139SThomas Cort if (print_mask & PRINT_RELEASE) {
133*c0c51139SThomas Cort if (space++) putchar(' ');
134*c0c51139SThomas Cort fputs(u.release, stdout);
135*c0c51139SThomas Cort }
136*c0c51139SThomas Cort if (print_mask & PRINT_VERSION) {
137*c0c51139SThomas Cort if (space++) putchar(' ');
138*c0c51139SThomas Cort fputs(u.version, stdout);
139*c0c51139SThomas Cort }
140*c0c51139SThomas Cort if (print_mask & PRINT_MACHINE) {
141*c0c51139SThomas Cort if (space++) putchar(' ');
142*c0c51139SThomas Cort fputs(u.machine, stdout);
143*c0c51139SThomas Cort }
144*c0c51139SThomas Cort if (print_mask & PRINT_MACHINE_ARCH) {
145*c0c51139SThomas Cort if (space++) putchar(' ');
146*c0c51139SThomas Cort fputs(machine_arch, stdout);
147*c0c51139SThomas Cort }
148*c0c51139SThomas Cort putchar('\n');
149*c0c51139SThomas Cort
150*c0c51139SThomas Cort exit(EXIT_SUCCESS);
151*c0c51139SThomas Cort /* NOTREACHED */
152*c0c51139SThomas Cort }
153*c0c51139SThomas Cort
154*c0c51139SThomas Cort static void
usage(void)155*c0c51139SThomas Cort usage(void)
156*c0c51139SThomas Cort {
157*c0c51139SThomas Cort fprintf(stderr, "usage: uname [-amnprsv]\n");
158*c0c51139SThomas Cort exit(EXIT_FAILURE);
159*c0c51139SThomas Cort }
160