xref: /netbsd-src/usr.bin/uname/uname.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*
2  * Copyright (c) 1994 Winning Strategies, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by Winning Strategies, Inc.
16  * 4. The name of Winning Strategies, Inc. may not be used to endorse or
17  *    promote products derived from this software without specific prior
18  *    written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static char rcsid[] = "$Id: uname.c,v 1.7 1994/12/20 01:28:57 jtc Exp $";
34 #endif /* not lint */
35 
36 #include <stdio.h>
37 #include <locale.h>
38 #include <unistd.h>
39 #include <sys/utsname.h>
40 #include <err.h>
41 
42 static void usage();
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 
51 int
52 main(argc, argv)
53 	int argc;
54 	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,"amnrsv")) != -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 		default:
84 			usage();
85 			/* NOTREACHED */
86 		}
87 	}
88 
89 	if (optind != argc) {
90 		usage();
91 		/* NOTREACHED */
92 	}
93 
94 	if (!print_mask) {
95 		print_mask = PRINT_SYSNAME;
96 	}
97 
98 	if (uname(&u)) {
99 		err(1, NULL);
100 		/* NOTREACHED */
101 	}
102 
103 	if (print_mask & PRINT_SYSNAME) {
104 		space++;
105 		fputs(u.sysname, stdout);
106 	}
107 	if (print_mask & PRINT_NODENAME) {
108 		if (space++) putchar(' ');
109 		fputs(u.nodename, stdout);
110 	}
111 	if (print_mask & PRINT_RELEASE) {
112 		if (space++) putchar(' ');
113 		fputs(u.release, stdout);
114 	}
115 	if (print_mask & PRINT_VERSION) {
116 		if (space++) putchar(' ');
117 		fputs(u.version, stdout);
118 	}
119 	if (print_mask & PRINT_MACHINE) {
120 		if (space++) putchar(' ');
121 		fputs(u.machine, stdout);
122 	}
123 	putchar('\n');
124 
125 	exit(0);
126 	/* NOTREACHED */
127 }
128 
129 static void
130 usage()
131 {
132 	fprintf(stderr, "usage: uname [-amnrsv]\n");
133 	exit(1);
134 }
135