uname.c revision 1.5 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.5 1994/01/29 01:43:43 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
41 static void usage();
42
43 #define PRINT_SYSNAME 0x01
44 #define PRINT_NODENAME 0x02
45 #define PRINT_RELEASE 0x04
46 #define PRINT_VERSION 0x08
47 #define PRINT_MACHINE 0x10
48 #define PRINT_ALL 0x1f
49
50 int
51 main(argc, argv)
52 int argc;
53 char **argv;
54 {
55 struct utsname u;
56 int c;
57 int space = 0;
58 int print_mask = 0;
59
60 setlocale(LC_ALL, "");
61
62 while ((c = getopt(argc,argv,"amnrsv")) != -1 ) {
63 switch ( c ) {
64 case 'a':
65 print_mask = PRINT_ALL;
66 break;
67 case 'm':
68 print_mask = PRINT_MACHINE;
69 break;
70 case 'n':
71 print_mask = PRINT_NODENAME;
72 break;
73 case 'r':
74 print_mask = PRINT_RELEASE;
75 break;
76 case 's':
77 print_mask = PRINT_SYSNAME;
78 break;
79 case 'v':
80 print_mask = PRINT_VERSION;
81 break;
82 default:
83 usage();
84 /* NOTREACHED */
85 }
86 }
87
88 if (optind != argc) {
89 usage();
90 /* NOTREACHED */
91 }
92
93 if (!print_mask) {
94 print_mask = PRINT_SYSNAME;
95 }
96
97 if (uname(&u)) {
98 err(1, NULL);
99 /* NOTREACHED */
100 }
101
102 if (print_mask & PRINT_SYSNAME) {
103 space++;
104 fputs(u.sysname, stdout);
105 }
106 if (print_mask & PRINT_NODENAME) {
107 if (space++) putchar(' ');
108 fputs(u.nodename, stdout);
109 }
110 if (print_mask & PRINT_RELEASE) {
111 if (space++) putchar(' ');
112 fputs(u.release, stdout);
113 }
114 if (print_mask & PRINT_VERSION) {
115 if (space++) putchar(' ');
116 fputs(u.version, stdout);
117 }
118 if (print_mask & PRINT_MACHINE) {
119 if (space++) putchar(' ');
120 fputs(u.machine, stdout);
121 }
122 putchar('\n');
123
124 exit(0);
125 /* NOTREACHED */
126 }
127
128 static void
129 usage()
130 {
131 fprintf(stderr, "usage: uname [-amnrsv]\n");
132 exit(1);
133 }
134