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