xref: /freebsd/usr.bin/uname/uname.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  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 the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 
36 __FBSDID("$FreeBSD$");
37 
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1993\n\
41 	The Regents of the University of California.  All rights reserved.\n";
42 #endif
43 
44 #ifndef lint
45 static const char sccsid[] = "@(#)uname.c	8.2 (Berkeley) 5/4/95";
46 #endif
47 
48 #include <sys/param.h>
49 #include <sys/sysctl.h>
50 
51 #include <err.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 
56 void usage(void);
57 
58 int
59 main(argc, argv)
60 	int argc;
61 	char *argv[];
62 {
63 #define	MFLAG	0x01
64 #define	NFLAG	0x02
65 #define	PFLAG	0x04
66 #define	RFLAG	0x08
67 #define	SFLAG	0x10
68 #define	VFLAG	0x20
69 	u_int flags;
70 	int ch, mib[2];
71 	size_t len, tlen;
72 	char *p, buf[1024];
73 	const char *prefix;
74 
75 	flags = 0;
76 	while ((ch = getopt(argc, argv, "amnprsv")) != -1)
77 		switch(ch) {
78 		case 'a':
79 			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
80 			break;
81 		case 'm':
82 			flags |= MFLAG;
83 			break;
84 		case 'n':
85 			flags |= NFLAG;
86 			break;
87 		case 'p':
88 			flags |= PFLAG;
89 			break;
90 		case 'r':
91 			flags |= RFLAG;
92 			break;
93 		case 's':
94 			flags |= SFLAG;
95 			break;
96 		case 'v':
97 			flags |= VFLAG;
98 			break;
99 		case '?':
100 		default:
101 			usage();
102 		}
103 
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc)
108 		usage();
109 
110 	if (!flags)
111 		flags |= SFLAG;
112 
113 	prefix = "";
114 
115 	if (flags & SFLAG) {
116 		mib[0] = CTL_KERN;
117 		mib[1] = KERN_OSTYPE;
118 		len = sizeof(buf);
119 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
120 			err(1, "sysctl");
121 		(void)printf("%s%.*s", prefix, (int)len, buf);
122 		prefix = " ";
123 	}
124 	if (flags & NFLAG) {
125 		mib[0] = CTL_KERN;
126 		mib[1] = KERN_HOSTNAME;
127 		len = sizeof(buf);
128 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
129 			err(1, "sysctl");
130 		(void)printf("%s%.*s", prefix, (int)len, buf);
131 		prefix = " ";
132 	}
133 	if (flags & RFLAG) {
134 		mib[0] = CTL_KERN;
135 		mib[1] = KERN_OSRELEASE;
136 		len = sizeof(buf);
137 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
138 			err(1, "sysctl");
139 		(void)printf("%s%.*s", prefix, (int)len, buf);
140 		prefix = " ";
141 	}
142 	if (flags & VFLAG) {
143 		mib[0] = CTL_KERN;
144 		mib[1] = KERN_VERSION;
145 		len = sizeof(buf);
146 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
147 			err(1, "sysctl");
148 		for (p = buf, tlen = len; tlen--; ++p)
149 			if (*p == '\n' || *p == '\t')
150 				*p = ' ';
151 		(void)printf("%s%.*s", prefix, (int)len, buf);
152 		prefix = " ";
153 	}
154 	if (flags & MFLAG) {
155 		mib[0] = CTL_HW;
156 		mib[1] = HW_MACHINE;
157 		len = sizeof(buf);
158 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
159 			err(1, "sysctl");
160 		(void)printf("%s%.*s", prefix, (int)len, buf);
161 		prefix = " ";
162 	}
163 	if (flags & PFLAG) {
164 		mib[0] = CTL_HW;
165 		mib[1] = HW_MACHINE_ARCH;
166 		len = sizeof(buf);
167 		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
168 			err(1, "sysctl");
169 		(void)printf("%s%.*s", prefix, (int)len, buf);
170 		prefix = " ";
171 	}
172 	(void)printf("\n");
173 	exit (0);
174 }
175 
176 void
177 usage()
178 {
179 	(void)fprintf(stderr, "usage: uname [-amnprsv]\n");
180 	exit(1);
181 }
182