uname.c revision 103837
11590Srgrimes/*- 2103518Sjmallett * Copyright (c) 2002 Juli Mallett. 31590Srgrimes * Copyright (c) 1993 41590Srgrimes * The Regents of the University of California. All rights reserved. 51590Srgrimes * 61590Srgrimes * Redistribution and use in source and binary forms, with or without 71590Srgrimes * modification, are permitted provided that the following conditions 81590Srgrimes * are met: 91590Srgrimes * 1. Redistributions of source code must retain the above copyright 101590Srgrimes * notice, this list of conditions and the following disclaimer. 111590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer in the 131590Srgrimes * documentation and/or other materials provided with the distribution. 141590Srgrimes * 3. All advertising materials mentioning features or use of this software 151590Srgrimes * must display the following acknowledgement: 161590Srgrimes * This product includes software developed by the University of 171590Srgrimes * California, Berkeley and its contributors. 181590Srgrimes * 4. Neither the name of the University nor the names of its contributors 191590Srgrimes * may be used to endorse or promote products derived from this software 201590Srgrimes * without specific prior written permission. 211590Srgrimes * 221590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321590Srgrimes * SUCH DAMAGE. 331590Srgrimes */ 341590Srgrimes 3587696Smarkm#include <sys/cdefs.h> 3687696Smarkm 3787696Smarkm__FBSDID("$FreeBSD: head/usr.bin/uname/uname.c 103837 2002-09-23 06:14:13Z jmallett $"); 3887696Smarkm 391590Srgrimes#ifndef lint 4087696Smarkmstatic const char copyright[] = 411590Srgrimes"@(#) Copyright (c) 1993\n\ 421590Srgrimes The Regents of the University of California. All rights reserved.\n"; 4387696Smarkm#endif 441590Srgrimes 451590Srgrimes#ifndef lint 4687696Smarkmstatic const char sccsid[] = "@(#)uname.c 8.2 (Berkeley) 5/4/95"; 4787696Smarkm#endif 481590Srgrimes 491590Srgrimes#include <sys/param.h> 501590Srgrimes#include <sys/sysctl.h> 511590Srgrimes 521590Srgrimes#include <err.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <stdlib.h> 5523690Speter#include <unistd.h> 561590Srgrimes 571590Srgrimes#define MFLAG 0x01 581590Srgrimes#define NFLAG 0x02 5989346Snyan#define PFLAG 0x04 6089346Snyan#define RFLAG 0x08 6189346Snyan#define SFLAG 0x10 6289346Snyan#define VFLAG 0x20 63103518Sjmallett 64103518Sjmalletttypedef void (*get_t)(void); 65103518Sjmallettget_t get_platform, get_hostname, get_arch, get_release, get_sysname, get_version; 66103518Sjmallett 67103518Sjmallettvoid native_platform(void); 68103518Sjmallettvoid native_hostname(void); 69103518Sjmallettvoid native_arch(void); 70103518Sjmallettvoid native_release(void); 71103518Sjmallettvoid native_sysname(void); 72103518Sjmallettvoid native_version(void); 73103518Sjmallettvoid print_uname(u_int); 74103518Sjmallettvoid setup_get(void); 75103518Sjmallettvoid usage(void); 76103518Sjmallett 77103518Sjmallettchar *platform, *hostname, *arch, *release, *sysname, *version; 78103837Sjmallettint space; 79103518Sjmallett 80103518Sjmallettint 81103518Sjmallettmain(int argc, char *argv[]) 82103518Sjmallett{ 831590Srgrimes u_int flags; 84103518Sjmallett int ch; 851590Srgrimes 86103518Sjmallett setup_get(); 87103518Sjmallett 8833792Ssteve while ((ch = getopt(argc, argv, "amnprsv")) != -1) 891590Srgrimes switch(ch) { 901590Srgrimes case 'a': 911590Srgrimes flags |= (MFLAG | NFLAG | RFLAG | SFLAG | VFLAG); 921590Srgrimes break; 931590Srgrimes case 'm': 941590Srgrimes flags |= MFLAG; 951590Srgrimes break; 961590Srgrimes case 'n': 971590Srgrimes flags |= NFLAG; 981590Srgrimes break; 9989346Snyan case 'p': 10089346Snyan flags |= PFLAG; 10189346Snyan break; 1021590Srgrimes case 'r': 1031590Srgrimes flags |= RFLAG; 1041590Srgrimes break; 1051590Srgrimes case 's': 1061590Srgrimes flags |= SFLAG; 1071590Srgrimes break; 1081590Srgrimes case 'v': 1091590Srgrimes flags |= VFLAG; 1101590Srgrimes break; 1111590Srgrimes case '?': 1121590Srgrimes default: 1131590Srgrimes usage(); 1141590Srgrimes } 1151590Srgrimes 1161590Srgrimes argc -= optind; 1171590Srgrimes argv += optind; 1181590Srgrimes 1191590Srgrimes if (argc) 1201590Srgrimes usage(); 1211590Srgrimes 1221590Srgrimes if (!flags) 1231590Srgrimes flags |= SFLAG; 1241590Srgrimes 125103518Sjmallett print_uname(flags); 126103518Sjmallett exit(0); 127103518Sjmallett} 1281590Srgrimes 129103837Sjmallett#define CHECK_ENV(opt,var) \ 130103837Sjmallettdo { \ 131103518Sjmallett if ((var = getenv("UNAME_" opt)) == NULL) { \ 132103837Sjmallett get_##var = native_##var; \ 133103837Sjmallett } else { \ 134103837Sjmallett get_##var = (get_t)NULL; \ 135103837Sjmallett } \ 136103518Sjmallett} while (0) 137103518Sjmallett 138103518Sjmallettvoid 139103518Sjmallettsetup_get(void) 140103518Sjmallett{ 141103518Sjmallett CHECK_ENV("s", sysname); 142103518Sjmallett CHECK_ENV("n", hostname); 143103518Sjmallett CHECK_ENV("r", release); 144103518Sjmallett CHECK_ENV("v", version); 145103518Sjmallett CHECK_ENV("m", platform); 146103518Sjmallett CHECK_ENV("p", arch); 147103518Sjmallett} 148103518Sjmallett 149103518Sjmallett#define PRINT_FLAG(flags,flag,var) \ 150103518Sjmallett if ((flags & flag) == flag) { \ 151103837Sjmallett if (space) \ 152103837Sjmallett printf(" "); \ 153103837Sjmallett else \ 154103837Sjmallett space++; \ 155103518Sjmallett if (get_##var != NULL) \ 156103518Sjmallett (*get_##var)(); \ 157103837Sjmallett printf("%s", var); \ 1581590Srgrimes } 159103518Sjmallett 160103518Sjmallettvoid 161103518Sjmallettprint_uname(u_int flags) 162103518Sjmallett{ 163103518Sjmallett PRINT_FLAG(flags, SFLAG, sysname); 164103518Sjmallett PRINT_FLAG(flags, NFLAG, hostname); 165103518Sjmallett PRINT_FLAG(flags, RFLAG, release); 166103518Sjmallett PRINT_FLAG(flags, VFLAG, version); 167103518Sjmallett PRINT_FLAG(flags, MFLAG, platform); 168103518Sjmallett PRINT_FLAG(flags, PFLAG, arch); 169103518Sjmallett printf("\n"); 1701590Srgrimes} 1711590Srgrimes 1721590Srgrimesvoid 173103518Sjmallettnative_sysname(void) 174103518Sjmallett{ 175103518Sjmallett int mib[2]; 176103518Sjmallett size_t len; 177103518Sjmallett static char buf[1024]; 178103518Sjmallett 179103518Sjmallett mib[0] = CTL_KERN; 180103518Sjmallett mib[1] = KERN_OSTYPE; 181103518Sjmallett len = sizeof(buf); 182103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 183103518Sjmallett err(1, "sysctl"); 184103518Sjmallett sysname = buf; 185103518Sjmallett} 186103518Sjmallett 187103518Sjmallettvoid 188103518Sjmallettnative_hostname(void) 189103518Sjmallett{ 190103518Sjmallett int mib[2]; 191103518Sjmallett size_t len; 192103518Sjmallett static char buf[1024]; 193103518Sjmallett 194103518Sjmallett mib[0] = CTL_KERN; 195103518Sjmallett mib[1] = KERN_HOSTNAME; 196103518Sjmallett len = sizeof(buf); 197103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 198103518Sjmallett err(1, "sysctl"); 199103518Sjmallett hostname = buf; 200103518Sjmallett} 201103518Sjmallett 202103518Sjmallettvoid 203103518Sjmallettnative_release(void) 204103518Sjmallett{ 205103518Sjmallett int mib[2]; 206103518Sjmallett size_t len; 207103518Sjmallett static char buf[1024]; 208103518Sjmallett 209103518Sjmallett mib[0] = CTL_KERN; 210103518Sjmallett mib[1] = KERN_OSRELEASE; 211103518Sjmallett len = sizeof(buf); 212103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 213103518Sjmallett err(1, "sysctl"); 214103518Sjmallett release = buf; 215103518Sjmallett} 216103518Sjmallett 217103518Sjmallettvoid 218103518Sjmallettnative_version(void) 219103518Sjmallett{ 220103518Sjmallett int mib[2]; 221103518Sjmallett size_t len, tlen; 222103518Sjmallett char *p; 223103518Sjmallett static char buf[1024]; 224103518Sjmallett 225103518Sjmallett mib[0] = CTL_KERN; 226103518Sjmallett mib[1] = KERN_VERSION; 227103518Sjmallett len = sizeof(buf); 228103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 229103518Sjmallett err(1, "sysctl"); 230103518Sjmallett for (p = buf, tlen = len; tlen--; ++p) 231103518Sjmallett if (*p == '\n' || *p == '\t') 232103518Sjmallett *p = ' '; 233103518Sjmallett version = buf; 234103518Sjmallett} 235103518Sjmallett 236103518Sjmallettvoid 237103518Sjmallettnative_platform(void) 238103518Sjmallett{ 239103518Sjmallett int mib[2]; 240103518Sjmallett size_t len; 241103518Sjmallett static char buf[1024]; 242103518Sjmallett 243103518Sjmallett mib[0] = CTL_HW; 244103518Sjmallett mib[1] = HW_MACHINE; 245103518Sjmallett len = sizeof(buf); 246103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 247103518Sjmallett err(1, "sysctl"); 248103518Sjmallett platform = buf; 249103518Sjmallett} 250103518Sjmallett 251103518Sjmallettvoid 252103518Sjmallettnative_arch(void) 253103518Sjmallett{ 254103518Sjmallett int mib[2]; 255103518Sjmallett size_t len; 256103518Sjmallett static char buf[1024]; 257103518Sjmallett 258103518Sjmallett mib[0] = CTL_HW; 259103518Sjmallett mib[1] = HW_MACHINE_ARCH; 260103518Sjmallett len = sizeof(buf); 261103518Sjmallett if (sysctl(mib, 2, &buf, &len, NULL, 0) == -1) 262103518Sjmallett err(1, "sysctl"); 263103518Sjmallett arch = buf; 264103518Sjmallett} 265103518Sjmallett 266103518Sjmallettvoid 267102944Sdwmaloneusage(void) 2681590Srgrimes{ 269103518Sjmallett fprintf(stderr, "usage: uname [-amnprsv]\n"); 2701590Srgrimes exit(1); 2711590Srgrimes} 272