xref: /openbsd-src/usr.bin/uname/uname.c (revision cb39b41371628601fbe4c618205356d538b9d08a)
1 /*	$OpenBSD: uname.c,v 1.13 2015/01/16 06:40:13 deraadt 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/param.h>	/* MACHINE_ARCH */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <locale.h>
38 #include <unistd.h>
39 #include <sys/utsname.h>
40 #include <err.h>
41 
42 static void usage(void);
43 
44 #define	PRINT_SYSNAME		0x01
45 #define	PRINT_NODENAME		0x02
46 #define	PRINT_RELEASE		0x04
47 #define	PRINT_VERSION		0x08
48 #define	PRINT_MACHINE		0x10
49 #define	PRINT_ALL		0x1f
50 #define PRINT_MACHINE_ARCH	0x20
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	struct utsname u;
56 	int c;
57 	int space = 0;
58 	int print_mask = 0;
59 
60 	setlocale(LC_ALL, "");
61 
62 	while ((c = getopt(argc, argv, "amnrsvp")) != -1 ) {
63 		switch (c) {
64 		case 'a':
65 			print_mask |= PRINT_ALL;
66 			break;
67 		case 'm':
68 			print_mask |= PRINT_MACHINE;
69 			break;
70 		case 'n':
71 			print_mask |= PRINT_NODENAME;
72 			break;
73 		case 'r':
74 			print_mask |= PRINT_RELEASE;
75 			break;
76 		case 's':
77 			print_mask |= PRINT_SYSNAME;
78 			break;
79 		case 'v':
80 			print_mask |= PRINT_VERSION;
81 			break;
82 		case 'p':
83 			print_mask |= PRINT_MACHINE_ARCH;
84 			break;
85 		default:
86 			usage();
87 			/* NOTREACHED */
88 		}
89 	}
90 
91 	if (optind != argc) {
92 		usage();
93 		/* NOTREACHED */
94 	}
95 
96 	if (!print_mask) {
97 		print_mask = PRINT_SYSNAME;
98 	}
99 
100 	if (uname(&u)) {
101 		err(1, NULL);
102 		/* NOTREACHED */
103 	}
104 
105 	if (print_mask & PRINT_SYSNAME) {
106 		space++;
107 		fputs(u.sysname, stdout);
108 	}
109 	if (print_mask & PRINT_NODENAME) {
110 		if (space++) putchar(' ');
111 		fputs(u.nodename, stdout);
112 	}
113 	if (print_mask & PRINT_RELEASE) {
114 		if (space++) putchar(' ');
115 		fputs(u.release, stdout);
116 	}
117 	if (print_mask & PRINT_VERSION) {
118 		if (space++) putchar(' ');
119 		fputs(u.version, stdout);
120 	}
121 	if (print_mask & PRINT_MACHINE) {
122 		if (space++) putchar(' ');
123 		fputs(u.machine, stdout);
124 	}
125 	if (print_mask & PRINT_MACHINE_ARCH) {
126 		if (space++) putchar(' ');
127 		fputs(MACHINE_ARCH, stdout);
128 	}
129 	putchar('\n');
130 
131 	exit(0);
132 	/* NOTREACHED */
133 }
134 
135 static void
136 usage(void)
137 {
138 	fprintf(stderr, "usage: uname [-amnprsv]\n");
139 	exit(1);
140 }
141