uname.c revision 89346
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3487696Smarkm#include <sys/cdefs.h>
3587696Smarkm
3687696Smarkm__FBSDID("$FreeBSD: head/usr.bin/uname/uname.c 89346 2002-01-14 12:49:46Z nyan $");
3787696Smarkm
381590Srgrimes#ifndef lint
3987696Smarkmstatic const char copyright[] =
401590Srgrimes"@(#) Copyright (c) 1993\n\
411590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
4287696Smarkm#endif
431590Srgrimes
441590Srgrimes#ifndef lint
4587696Smarkmstatic const char sccsid[] = "@(#)uname.c	8.2 (Berkeley) 5/4/95";
4687696Smarkm#endif
471590Srgrimes
481590Srgrimes#include <sys/param.h>
491590Srgrimes#include <sys/sysctl.h>
501590Srgrimes
511590Srgrimes#include <err.h>
521590Srgrimes#include <stdio.h>
531590Srgrimes#include <stdlib.h>
5423690Speter#include <unistd.h>
551590Srgrimes
561590Srgrimesvoid usage __P((void));
571590Srgrimes
581590Srgrimesint
591590Srgrimesmain(argc, argv)
601590Srgrimes	int argc;
611590Srgrimes	char *argv[];
621590Srgrimes{
631590Srgrimes#define	MFLAG	0x01
641590Srgrimes#define	NFLAG	0x02
6589346Snyan#define	PFLAG	0x04
6689346Snyan#define	RFLAG	0x08
6789346Snyan#define	SFLAG	0x10
6889346Snyan#define	VFLAG	0x20
691590Srgrimes	u_int flags;
701590Srgrimes	int ch, mib[2];
711590Srgrimes	size_t len, tlen;
7287696Smarkm	char *p, buf[1024];
7387696Smarkm	const char *prefix;
741590Srgrimes
751590Srgrimes	flags = 0;
7633792Ssteve	while ((ch = getopt(argc, argv, "amnprsv")) != -1)
771590Srgrimes		switch(ch) {
781590Srgrimes		case 'a':
791590Srgrimes			flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG);
801590Srgrimes			break;
811590Srgrimes		case 'm':
821590Srgrimes			flags |= MFLAG;
831590Srgrimes			break;
841590Srgrimes		case 'n':
851590Srgrimes			flags |= NFLAG;
861590Srgrimes			break;
8789346Snyan		case 'p':
8889346Snyan			flags |= PFLAG;
8989346Snyan			break;
901590Srgrimes		case 'r':
911590Srgrimes			flags |= RFLAG;
921590Srgrimes			break;
931590Srgrimes		case 's':
941590Srgrimes			flags |= SFLAG;
951590Srgrimes			break;
961590Srgrimes		case 'v':
971590Srgrimes			flags |= VFLAG;
981590Srgrimes			break;
991590Srgrimes		case '?':
1001590Srgrimes		default:
1011590Srgrimes			usage();
1021590Srgrimes		}
1031590Srgrimes
1041590Srgrimes	argc -= optind;
1051590Srgrimes	argv += optind;
1061590Srgrimes
1071590Srgrimes	if (argc)
1081590Srgrimes		usage();
1091590Srgrimes
1101590Srgrimes	if (!flags)
1111590Srgrimes		flags |= SFLAG;
1121590Srgrimes
1131590Srgrimes	prefix = "";
1141590Srgrimes
1151590Srgrimes	if (flags & SFLAG) {
1161590Srgrimes		mib[0] = CTL_KERN;
1171590Srgrimes		mib[1] = KERN_OSTYPE;
1181590Srgrimes		len = sizeof(buf);
1191590Srgrimes		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
1201590Srgrimes			err(1, "sysctl");
12137453Sbde		(void)printf("%s%.*s", prefix, (int)len, buf);
1221590Srgrimes		prefix = " ";
1231590Srgrimes	}
1241590Srgrimes	if (flags & NFLAG) {
1251590Srgrimes		mib[0] = CTL_KERN;
1261590Srgrimes		mib[1] = KERN_HOSTNAME;
1271590Srgrimes		len = sizeof(buf);
1281590Srgrimes		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
1291590Srgrimes			err(1, "sysctl");
13037453Sbde		(void)printf("%s%.*s", prefix, (int)len, buf);
1311590Srgrimes		prefix = " ";
1321590Srgrimes	}
1331590Srgrimes	if (flags & RFLAG) {
1341590Srgrimes		mib[0] = CTL_KERN;
1351590Srgrimes		mib[1] = KERN_OSRELEASE;
1361590Srgrimes		len = sizeof(buf);
1371590Srgrimes		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
1381590Srgrimes			err(1, "sysctl");
13937453Sbde		(void)printf("%s%.*s", prefix, (int)len, buf);
1401590Srgrimes		prefix = " ";
1411590Srgrimes	}
1421590Srgrimes	if (flags & VFLAG) {
1431590Srgrimes		mib[0] = CTL_KERN;
1441590Srgrimes		mib[1] = KERN_VERSION;
1451590Srgrimes		len = sizeof(buf);
1461590Srgrimes		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
1471590Srgrimes			err(1, "sysctl");
1481590Srgrimes		for (p = buf, tlen = len; tlen--; ++p)
1491590Srgrimes			if (*p == '\n' || *p == '\t')
1501590Srgrimes				*p = ' ';
15137453Sbde		(void)printf("%s%.*s", prefix, (int)len, buf);
1521590Srgrimes		prefix = " ";
1531590Srgrimes	}
1541590Srgrimes	if (flags & MFLAG) {
1551590Srgrimes		mib[0] = CTL_HW;
1561590Srgrimes		mib[1] = HW_MACHINE;
1571590Srgrimes		len = sizeof(buf);
1581590Srgrimes		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
1591590Srgrimes			err(1, "sysctl");
16037453Sbde		(void)printf("%s%.*s", prefix, (int)len, buf);
1611590Srgrimes		prefix = " ";
1621590Srgrimes	}
16389346Snyan	if (flags & PFLAG) {
16489346Snyan		mib[0] = CTL_HW;
16589346Snyan		mib[1] = HW_MACHINE_ARCH;
16689346Snyan		len = sizeof(buf);
16789346Snyan		if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1)
16889346Snyan			err(1, "sysctl");
16989346Snyan		(void)printf("%s%.*s", prefix, (int)len, buf);
17089346Snyan		prefix = " ";
17189346Snyan	}
1721590Srgrimes	(void)printf("\n");
1731590Srgrimes	exit (0);
1741590Srgrimes}
1751590Srgrimes
1761590Srgrimesvoid
1771590Srgrimesusage()
1781590Srgrimes{
17989346Snyan	(void)fprintf(stderr, "usage: uname [-amnprsv]\n");
1801590Srgrimes	exit(1);
1811590Srgrimes}
182