xref: /netbsd-src/usr.bin/uname/uname.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: uname.c,v 1.8 1997/01/09 20:23:13 tls 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[] = "$NetBSD: uname.c,v 1.8 1997/01/09 20:23:13 tls Exp $";
36 #endif /* not lint */
37 
38 #include <stdio.h>
39 #include <locale.h>
40 #include <unistd.h>
41 #include <sys/utsname.h>
42 #include <err.h>
43 
44 static void usage();
45 
46 #define	PRINT_SYSNAME	0x01
47 #define	PRINT_NODENAME	0x02
48 #define	PRINT_RELEASE	0x04
49 #define	PRINT_VERSION	0x08
50 #define	PRINT_MACHINE	0x10
51 #define	PRINT_ALL	0x1f
52 
53 int
54 main(argc, argv)
55 	int argc;
56 	char **argv;
57 {
58 	struct utsname u;
59 	int c;
60 	int space = 0;
61 	int print_mask = 0;
62 
63 	setlocale(LC_ALL, "");
64 
65 	while ((c = getopt(argc,argv,"amnrsv")) != -1 ) {
66 		switch ( c ) {
67 		case 'a':
68 			print_mask |= PRINT_ALL;
69 			break;
70 		case 'm':
71 			print_mask |= PRINT_MACHINE;
72 			break;
73 		case 'n':
74 			print_mask |= PRINT_NODENAME;
75 			break;
76 		case 'r':
77 			print_mask |= PRINT_RELEASE;
78 			break;
79 		case 's':
80 			print_mask |= PRINT_SYSNAME;
81 			break;
82 		case 'v':
83 			print_mask |= PRINT_VERSION;
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 	putchar('\n');
126 
127 	exit(0);
128 	/* NOTREACHED */
129 }
130 
131 static void
132 usage()
133 {
134 	fprintf(stderr, "usage: uname [-amnrsv]\n");
135 	exit(1);
136 }
137