xref: /original-bsd/usr.bin/uname/uname.c (revision d45ebeed)
1 /*-
2  * Copyright (c) 1993 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1993 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)uname.c	5.4 (Berkeley) 04/28/93";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/sysctl.h>
20 
21 #include <err.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 void usage __P((void));
26 
27 int
28 main(argc, argv)
29 	int argc;
30 	char *argv[];
31 {
32 #define	MFLAG	0x01
33 #define	NFLAG	0x02
34 #define	RFLAG	0x04
35 #define	SFLAG	0x08
36 #define	VFLAG	0x10
37 	u_int flags;
38 	int ch, mib[2];
39 	size_t len, tlen;
40 	char *p, *prefix, buf[1024];
41 
42 	flags = 0;
43 	while ((ch = getopt(argc, argv, "amnrsv")) != EOF)
44 		switch(ch) {
45 		case 'a':
46 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
47 			break;
48 		case 'm':
49 			flags |= MFLAG;
50 			break;
51 		case 'n':
52 			flags |= NFLAG;
53 			break;
54 		case 'r':
55 			flags |= RFLAG;
56 			break;
57 		case 's':
58 			flags |= SFLAG;
59 			break;
60 		case 'v':
61 			flags |= VFLAG;
62 			break;
63 		case '?':
64 		default:
65 			usage();
66 		}
67 
68 	argc -= optind;
69 	argv += optind;
70 
71 	if (argc)
72 		usage();
73 
74 	if (!flags)
75 		flags |= SFLAG;
76 
77 	prefix = "";
78 
79 	if (flags & SFLAG) {
80 		mib[0] = CTL_KERN;
81 		mib[1] = KERN_OSTYPE;
82 		len = sizeof(buf);
83 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
84 			err(1, "sysctl");
85 		(void)printf("%s%.*s", prefix, len, buf);
86 		prefix = " ";
87 	}
88 	if (flags & NFLAG) {
89 		mib[0] = CTL_KERN;
90 		mib[1] = KERN_HOSTNAME;
91 		len = sizeof(buf);
92 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
93 			err(1, "sysctl");
94 		(void)printf("%s%.*s", prefix, len, buf);
95 		prefix = " ";
96 	}
97 	if (flags & RFLAG) {
98 		mib[0] = CTL_KERN;
99 		mib[1] = KERN_OSRELEASE;
100 		len = sizeof(buf);
101 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
102 			err(1, "sysctl");
103 		(void)printf("%s%.*s", prefix, len, buf);
104 		prefix = " ";
105 	}
106 	if (flags & VFLAG) {
107 		mib[0] = CTL_KERN;
108 		mib[1] = KERN_VERSION;
109 		len = sizeof(buf);
110 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
111 			err(1, "sysctl");
112 		for (p = buf, tlen = len; tlen--; ++p)
113 			if (*p == '\n' || *p == '\t')
114 				*p = ' ';
115 		(void)printf("%s%.*s", prefix, len, buf);
116 		prefix = " ";
117 	}
118 	if (flags & MFLAG) {
119 		mib[0] = CTL_HW;
120 		mib[1] = HW_MACHINE;
121 		len = sizeof(buf);
122 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
123 			err(1, "sysctl");
124 		(void)printf("%s%.*s", prefix, len, buf);
125 		prefix = " ";
126 	}
127 	(void)printf("\n");
128 	exit (0);
129 }
130 
131 void
132 usage()
133 {
134 	(void)fprintf(stderr, "usage: uname [-amnrsv]\n");
135 	exit(1);
136 }
137