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