uname.c revision 87696
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 87696 2001-12-11 23:16:25Z markm $"); 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 651590Srgrimes#define RFLAG 0x04 661590Srgrimes#define SFLAG 0x08 671590Srgrimes#define VFLAG 0x10 681590Srgrimes u_int flags; 691590Srgrimes int ch, mib[2]; 701590Srgrimes size_t len, tlen; 7187696Smarkm char *p, buf[1024]; 7287696Smarkm const char *prefix; 731590Srgrimes 741590Srgrimes flags = 0; 7533792Ssteve while ((ch = getopt(argc, argv, "amnprsv")) != -1) 761590Srgrimes switch(ch) { 771590Srgrimes case 'a': 781590Srgrimes flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 791590Srgrimes break; 8033792Ssteve case 'p': 811590Srgrimes case 'm': 821590Srgrimes flags |= MFLAG; 831590Srgrimes break; 841590Srgrimes case 'n': 851590Srgrimes flags |= NFLAG; 861590Srgrimes break; 871590Srgrimes case 'r': 881590Srgrimes flags |= RFLAG; 891590Srgrimes break; 901590Srgrimes case 's': 911590Srgrimes flags |= SFLAG; 921590Srgrimes break; 931590Srgrimes case 'v': 941590Srgrimes flags |= VFLAG; 951590Srgrimes break; 961590Srgrimes case '?': 971590Srgrimes default: 981590Srgrimes usage(); 991590Srgrimes } 1001590Srgrimes 1011590Srgrimes argc -= optind; 1021590Srgrimes argv += optind; 1031590Srgrimes 1041590Srgrimes if (argc) 1051590Srgrimes usage(); 1061590Srgrimes 1071590Srgrimes if (!flags) 1081590Srgrimes flags |= SFLAG; 1091590Srgrimes 1101590Srgrimes prefix = ""; 1111590Srgrimes 1121590Srgrimes if (flags & SFLAG) { 1131590Srgrimes mib[0] = CTL_KERN; 1141590Srgrimes mib[1] = KERN_OSTYPE; 1151590Srgrimes len = sizeof(buf); 1161590Srgrimes if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 1171590Srgrimes err(1, "sysctl"); 11837453Sbde (void)printf("%s%.*s", prefix, (int)len, buf); 1191590Srgrimes prefix = " "; 1201590Srgrimes } 1211590Srgrimes if (flags & NFLAG) { 1221590Srgrimes mib[0] = CTL_KERN; 1231590Srgrimes mib[1] = KERN_HOSTNAME; 1241590Srgrimes len = sizeof(buf); 1251590Srgrimes if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 1261590Srgrimes err(1, "sysctl"); 12737453Sbde (void)printf("%s%.*s", prefix, (int)len, buf); 1281590Srgrimes prefix = " "; 1291590Srgrimes } 1301590Srgrimes if (flags & RFLAG) { 1311590Srgrimes mib[0] = CTL_KERN; 1321590Srgrimes mib[1] = KERN_OSRELEASE; 1331590Srgrimes len = sizeof(buf); 1341590Srgrimes if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 1351590Srgrimes err(1, "sysctl"); 13637453Sbde (void)printf("%s%.*s", prefix, (int)len, buf); 1371590Srgrimes prefix = " "; 1381590Srgrimes } 1391590Srgrimes if (flags & VFLAG) { 1401590Srgrimes mib[0] = CTL_KERN; 1411590Srgrimes mib[1] = KERN_VERSION; 1421590Srgrimes len = sizeof(buf); 1431590Srgrimes if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 1441590Srgrimes err(1, "sysctl"); 1451590Srgrimes for (p = buf, tlen = len; tlen--; ++p) 1461590Srgrimes if (*p == '\n' || *p == '\t') 1471590Srgrimes *p = ' '; 14837453Sbde (void)printf("%s%.*s", prefix, (int)len, buf); 1491590Srgrimes prefix = " "; 1501590Srgrimes } 1511590Srgrimes if (flags & MFLAG) { 1521590Srgrimes mib[0] = CTL_HW; 1531590Srgrimes mib[1] = HW_MACHINE; 1541590Srgrimes len = sizeof(buf); 1551590Srgrimes if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 1561590Srgrimes err(1, "sysctl"); 15737453Sbde (void)printf("%s%.*s", prefix, (int)len, buf); 1581590Srgrimes prefix = " "; 1591590Srgrimes } 1601590Srgrimes (void)printf("\n"); 1611590Srgrimes exit (0); 1621590Srgrimes} 1631590Srgrimes 1641590Srgrimesvoid 1651590Srgrimesusage() 1661590Srgrimes{ 1671590Srgrimes (void)fprintf(stderr, "usage: uname [-amnrsv]\n"); 1681590Srgrimes exit(1); 1691590Srgrimes} 170