uname.c revision 102944
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: head/usr.bin/uname/uname.c 102944 2002-09-04 23:29:10Z dwmalone $");
37
38#ifndef lint
39static 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
45static 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
56void usage(void);
57
58int
59main(int argc, char *argv[])
60{
61#define	MFLAG	0x01
62#define	NFLAG	0x02
63#define	PFLAG	0x04
64#define	RFLAG	0x08
65#define	SFLAG	0x10
66#define	VFLAG	0x20
67	u_int flags;
68	int ch, mib[2];
69	size_t len, tlen;
70	char *p, buf[1024];
71	const char *prefix;
72
73	flags = 0;
74	while ((ch = getopt(argc, argv, "amnprsv")) != -1)
75		switch(ch) {
76		case 'a':
77			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
78			break;
79		case 'm':
80			flags |= MFLAG;
81			break;
82		case 'n':
83			flags |= NFLAG;
84			break;
85		case 'p':
86			flags |= PFLAG;
87			break;
88		case 'r':
89			flags |= RFLAG;
90			break;
91		case 's':
92			flags |= SFLAG;
93			break;
94		case 'v':
95			flags |= VFLAG;
96			break;
97		case '?':
98		default:
99			usage();
100		}
101
102	argc -= optind;
103	argv += optind;
104
105	if (argc)
106		usage();
107
108	if (!flags)
109		flags |= SFLAG;
110
111	prefix = "";
112
113	if (flags & SFLAG) {
114		mib[0] = CTL_KERN;
115		mib[1] = KERN_OSTYPE;
116		len = sizeof(buf);
117		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
118			err(1, "sysctl");
119		(void)printf("%s%.*s", prefix, (int)len, buf);
120		prefix = " ";
121	}
122	if (flags & NFLAG) {
123		mib[0] = CTL_KERN;
124		mib[1] = KERN_HOSTNAME;
125		len = sizeof(buf);
126		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
127			err(1, "sysctl");
128		(void)printf("%s%.*s", prefix, (int)len, buf);
129		prefix = " ";
130	}
131	if (flags & RFLAG) {
132		mib[0] = CTL_KERN;
133		mib[1] = KERN_OSRELEASE;
134		len = sizeof(buf);
135		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
136			err(1, "sysctl");
137		(void)printf("%s%.*s", prefix, (int)len, buf);
138		prefix = " ";
139	}
140	if (flags & VFLAG) {
141		mib[0] = CTL_KERN;
142		mib[1] = KERN_VERSION;
143		len = sizeof(buf);
144		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
145			err(1, "sysctl");
146		for (p = buf, tlen = len; tlen--; ++p)
147			if (*p == '\n' || *p == '\t')
148				*p = ' ';
149		(void)printf("%s%.*s", prefix, (int)len, buf);
150		prefix = " ";
151	}
152	if (flags & MFLAG) {
153		mib[0] = CTL_HW;
154		mib[1] = HW_MACHINE;
155		len = sizeof(buf);
156		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
157			err(1, "sysctl");
158		(void)printf("%s%.*s", prefix, (int)len, buf);
159		prefix = " ";
160	}
161	if (flags & PFLAG) {
162		mib[0] = CTL_HW;
163		mib[1] = HW_MACHINE_ARCH;
164		len = sizeof(buf);
165		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
166			err(1, "sysctl");
167		(void)printf("%s%.*s", prefix, (int)len, buf);
168		prefix = " ";
169	}
170	(void)printf("\n");
171	exit (0);
172}
173
174void
175usage(void)
176{
177	(void)fprintf(stderr, "usage: uname [-amnprsv]\n");
178	exit(1);
179}
180