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