xref: /netbsd-src/usr.bin/uname/uname.c (revision 4b30c543a0b21e3ba94f2c569e9a82b4fdb2075f)
1 /*
2  * uname - print system information. Jeff Comstock - Bloomington, MN USA 1992
3  * Usage: uname [-asnrvm]
4  * -s prints system name
5  * -n prints nodename
6  * -r prints software release
7  * -v prints os version
8  * -m prints machine name
9  * -a prinst all the above information
10  */
11 
12 #ifndef lint
13 static char rcsid[] = "$Id: uname.c,v 1.3 1993/08/07 07:58:19 cgd Exp $";
14 #endif /* not lint */
15 
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <sysexits.h>
19 #include <sys/utsname.h>
20 
21 #define SYSNAME 	0
22 #define NODENAME 	1
23 #define RELEASE 	2
24 #define VERSION 	3
25 #define MACHINE 	4
26 
27 struct utsname u;
28 
29 struct utstab {
30 	char *str;
31 	int requested;
32 } uttab[] = {
33 	{ u.sysname, 	0 },
34 	{ u.nodename, 	0 },
35 	{ u.release, 	0 },
36 	{ u.version, 	0 },
37 	{ u.machine, 	0 }
38 };
39 
40 main(int argc, char **argv) {
41 char *opts="amnrsv";
42 register int c,space, all=0;
43 
44 	if ( ! uname(&u) ) {
45 		if ( argc == 1 ) {
46 			puts(u.sysname);
47 		} else {
48 			while ( (c = getopt(argc,argv,opts)) != -1 ) {
49 					switch ( c ) {
50 					case 'a' : all++;
51 						break;
52 					case 'm' : uttab[MACHINE].requested++;
53 						break;
54 					case 'n' : uttab[NODENAME].requested++;
55 						break;
56 					case 'r' : uttab[RELEASE].requested++;
57 						break;
58 					case 's' : uttab[SYSNAME].requested++;
59 						break;
60 					case 'v' : uttab[VERSION].requested++;
61 						break;
62 				}
63 			}
64 			space=0;
65 			for(c=0; c <= MACHINE; c++) {
66 				if ( uttab[c].requested || all ) {
67 					if ( space )
68 						putchar(' ');
69 					printf("%s", uttab[c].str);
70 					space++;
71 				}
72 			}
73 			puts("");
74 		}
75 		exit (EX_OK);
76 	} else {
77 		perror("uname");
78 		exit (EX_OSERR);
79 	}
80 }
81