1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 33*7c478bd9Sstevel@tonic-gate * All Rights Reserved 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*7c478bd9Sstevel@tonic-gate * contributors. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #define __EXTENSIONS__ 43*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 44*7c478bd9Sstevel@tonic-gate #include <stdio.h> 45*7c478bd9Sstevel@tonic-gate #include <stdio.h> 46*7c478bd9Sstevel@tonic-gate #include <string.h> 47*7c478bd9Sstevel@tonic-gate #include <locale.h> 48*7c478bd9Sstevel@tonic-gate #include <unistd.h> 49*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 50*7c478bd9Sstevel@tonic-gate #include <errno.h> 51*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 52*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 53*7c478bd9Sstevel@tonic-gate #include <sys/utsname.h> 54*7c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate static void usage(void); 57*7c478bd9Sstevel@tonic-gate 58*7c478bd9Sstevel@tonic-gate #ifdef _iBCS2 59*7c478bd9Sstevel@tonic-gate char *sysv3_env; 60*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 61*7c478bd9Sstevel@tonic-gate 62*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 63*7c478bd9Sstevel@tonic-gate int 64*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[]) 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate char *nodename; 67*7c478bd9Sstevel@tonic-gate char *optstring = "asnrpvmiS:X"; 68*7c478bd9Sstevel@tonic-gate int sflg = 0, nflg = 0, rflg = 0, vflg = 0, mflg = 0; 69*7c478bd9Sstevel@tonic-gate int pflg = 0, iflg = 0, Sflg = 0; 70*7c478bd9Sstevel@tonic-gate int errflg = 0, optlet; 71*7c478bd9Sstevel@tonic-gate int Xflg = 0; 72*7c478bd9Sstevel@tonic-gate #ifdef _iBCS2 73*7c478bd9Sstevel@tonic-gate char *ptr; 74*7c478bd9Sstevel@tonic-gate char *newptr; 75*7c478bd9Sstevel@tonic-gate int cnt; 76*7c478bd9Sstevel@tonic-gate int done; 77*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 78*7c478bd9Sstevel@tonic-gate struct utsname unstr, *un; 79*7c478bd9Sstevel@tonic-gate char fmt_string[] = " %.*s"; 80*7c478bd9Sstevel@tonic-gate char *fs = &fmt_string[1]; 81*7c478bd9Sstevel@tonic-gate char procbuf[SYS_NMLN]; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate (void) umask(~(S_IRWXU|S_IRGRP|S_IROTH) & S_IAMB); 84*7c478bd9Sstevel@tonic-gate un = &unstr; 85*7c478bd9Sstevel@tonic-gate (void) uname(un); 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate #ifdef _iBCS2 88*7c478bd9Sstevel@tonic-gate /* Find out if user wants SYS V behavior */ 89*7c478bd9Sstevel@tonic-gate if (sysv3_env = getenv("SYSV3")) { 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate /* 92*7c478bd9Sstevel@tonic-gate * Now figure out what values are encoded in sysv3 93*7c478bd9Sstevel@tonic-gate * Tokens are comma separated: 94*7c478bd9Sstevel@tonic-gate * os, sysname, nodename, release, version, machtype 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate cnt = 0; 97*7c478bd9Sstevel@tonic-gate ptr = sysv3_env; 98*7c478bd9Sstevel@tonic-gate done = 0; 99*7c478bd9Sstevel@tonic-gate while (!done && *ptr) { 100*7c478bd9Sstevel@tonic-gate if ((newptr = strchr(ptr, ',')) == (char *)0) 101*7c478bd9Sstevel@tonic-gate done = 1; 102*7c478bd9Sstevel@tonic-gate else 103*7c478bd9Sstevel@tonic-gate /* Null out the comma */ 104*7c478bd9Sstevel@tonic-gate *newptr = '\0'; 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate /* If ptr == newptr, there was no data for this token */ 107*7c478bd9Sstevel@tonic-gate if (ptr == newptr) { 108*7c478bd9Sstevel@tonic-gate /* Step over null token and go around again */ 109*7c478bd9Sstevel@tonic-gate ptr = newptr + 1; 110*7c478bd9Sstevel@tonic-gate cnt ++; 111*7c478bd9Sstevel@tonic-gate continue; 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate switch (cnt++) { 115*7c478bd9Sstevel@tonic-gate case 0: 116*7c478bd9Sstevel@tonic-gate /* Ignore the os token for now. */ 117*7c478bd9Sstevel@tonic-gate break; 118*7c478bd9Sstevel@tonic-gate case 1: 119*7c478bd9Sstevel@tonic-gate strcpy(un->sysname, ptr); 120*7c478bd9Sstevel@tonic-gate break; 121*7c478bd9Sstevel@tonic-gate case 2: 122*7c478bd9Sstevel@tonic-gate strcpy(un->nodename, ptr); 123*7c478bd9Sstevel@tonic-gate break; 124*7c478bd9Sstevel@tonic-gate case 3: 125*7c478bd9Sstevel@tonic-gate strcpy(un->release, ptr); 126*7c478bd9Sstevel@tonic-gate break; 127*7c478bd9Sstevel@tonic-gate case 4: 128*7c478bd9Sstevel@tonic-gate strcpy(un->version, ptr); 129*7c478bd9Sstevel@tonic-gate break; 130*7c478bd9Sstevel@tonic-gate case 5: 131*7c478bd9Sstevel@tonic-gate strcpy(un->machine, ptr); 132*7c478bd9Sstevel@tonic-gate break; 133*7c478bd9Sstevel@tonic-gate default: 134*7c478bd9Sstevel@tonic-gate done = 1; 135*7c478bd9Sstevel@tonic-gate break; 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate ptr = newptr + 1; 138*7c478bd9Sstevel@tonic-gate } 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate /* 141*7c478bd9Sstevel@tonic-gate * If SYSV3 is set to an empty string, fill in the structure 142*7c478bd9Sstevel@tonic-gate * with reasonable default values. 143*7c478bd9Sstevel@tonic-gate */ 144*7c478bd9Sstevel@tonic-gate if (!cnt) { 145*7c478bd9Sstevel@tonic-gate strcpy(un->sysname, un->nodename); 146*7c478bd9Sstevel@tonic-gate strcpy(un->release, "3.2"); 147*7c478bd9Sstevel@tonic-gate strcpy(un->version, "2"); 148*7c478bd9Sstevel@tonic-gate strcpy(un->machine, "i386"); 149*7c478bd9Sstevel@tonic-gate } 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 155*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 156*7c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 157*7c478bd9Sstevel@tonic-gate #endif 158*7c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate while ((optlet = getopt(argc, argv, optstring)) != EOF) 161*7c478bd9Sstevel@tonic-gate switch (optlet) { 162*7c478bd9Sstevel@tonic-gate case 'a': 163*7c478bd9Sstevel@tonic-gate sflg++; nflg++; rflg++; vflg++; mflg++; 164*7c478bd9Sstevel@tonic-gate #ifdef _iBCS2 165*7c478bd9Sstevel@tonic-gate /* 166*7c478bd9Sstevel@tonic-gate * If compat mode, don't print things ISC 167*7c478bd9Sstevel@tonic-gate * doesn't have 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate if (!sysv3_env) 170*7c478bd9Sstevel@tonic-gate #endif /* _iBCS2 */ 171*7c478bd9Sstevel@tonic-gate { 172*7c478bd9Sstevel@tonic-gate pflg++; 173*7c478bd9Sstevel@tonic-gate iflg++; 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate break; 176*7c478bd9Sstevel@tonic-gate case 's': 177*7c478bd9Sstevel@tonic-gate sflg++; 178*7c478bd9Sstevel@tonic-gate break; 179*7c478bd9Sstevel@tonic-gate case 'n': 180*7c478bd9Sstevel@tonic-gate nflg++; 181*7c478bd9Sstevel@tonic-gate break; 182*7c478bd9Sstevel@tonic-gate case 'r': 183*7c478bd9Sstevel@tonic-gate rflg++; 184*7c478bd9Sstevel@tonic-gate break; 185*7c478bd9Sstevel@tonic-gate case 'v': 186*7c478bd9Sstevel@tonic-gate vflg++; 187*7c478bd9Sstevel@tonic-gate break; 188*7c478bd9Sstevel@tonic-gate case 'm': 189*7c478bd9Sstevel@tonic-gate mflg++; 190*7c478bd9Sstevel@tonic-gate break; 191*7c478bd9Sstevel@tonic-gate case 'p': 192*7c478bd9Sstevel@tonic-gate pflg++; 193*7c478bd9Sstevel@tonic-gate break; 194*7c478bd9Sstevel@tonic-gate case 'i': 195*7c478bd9Sstevel@tonic-gate iflg++; 196*7c478bd9Sstevel@tonic-gate break; 197*7c478bd9Sstevel@tonic-gate case 'S': 198*7c478bd9Sstevel@tonic-gate Sflg++; 199*7c478bd9Sstevel@tonic-gate nodename = optarg; 200*7c478bd9Sstevel@tonic-gate break; 201*7c478bd9Sstevel@tonic-gate case 'X': 202*7c478bd9Sstevel@tonic-gate Xflg++; 203*7c478bd9Sstevel@tonic-gate break; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate case '?': 206*7c478bd9Sstevel@tonic-gate errflg++; 207*7c478bd9Sstevel@tonic-gate } 208*7c478bd9Sstevel@tonic-gate 209*7c478bd9Sstevel@tonic-gate if (errflg || (optind != argc)) 210*7c478bd9Sstevel@tonic-gate usage(); 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate if ((Sflg > 1) || 213*7c478bd9Sstevel@tonic-gate (Sflg && (sflg || nflg || rflg || vflg || mflg || pflg || iflg || 214*7c478bd9Sstevel@tonic-gate Xflg))) { 215*7c478bd9Sstevel@tonic-gate usage(); 216*7c478bd9Sstevel@tonic-gate } 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate /* If we're changing the system name */ 219*7c478bd9Sstevel@tonic-gate if (Sflg) { 220*7c478bd9Sstevel@tonic-gate int len = strlen(nodename); 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate if (len > SYS_NMLN - 1) { 223*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 224*7c478bd9Sstevel@tonic-gate "uname: name must be <= %d letters\n"), 225*7c478bd9Sstevel@tonic-gate SYS_NMLN-1); 226*7c478bd9Sstevel@tonic-gate exit(1); 227*7c478bd9Sstevel@tonic-gate } 228*7c478bd9Sstevel@tonic-gate if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) { 229*7c478bd9Sstevel@tonic-gate int err = errno; 230*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 231*7c478bd9Sstevel@tonic-gate "uname: error in setting name: %s\n"), 232*7c478bd9Sstevel@tonic-gate strerror(err)); 233*7c478bd9Sstevel@tonic-gate exit(1); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate return (0); 236*7c478bd9Sstevel@tonic-gate } 237*7c478bd9Sstevel@tonic-gate 238*7c478bd9Sstevel@tonic-gate /* 239*7c478bd9Sstevel@tonic-gate * "uname -s" is the default 240*7c478bd9Sstevel@tonic-gate */ 241*7c478bd9Sstevel@tonic-gate if (!(sflg || nflg || rflg || vflg || mflg || pflg || iflg || Xflg)) 242*7c478bd9Sstevel@tonic-gate sflg++; 243*7c478bd9Sstevel@tonic-gate if (sflg) { 244*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, sizeof (un->sysname), 245*7c478bd9Sstevel@tonic-gate un->sysname); 246*7c478bd9Sstevel@tonic-gate fs = fmt_string; 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate if (nflg) { 249*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, sizeof (un->nodename), un->nodename); 250*7c478bd9Sstevel@tonic-gate fs = fmt_string; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate if (rflg) { 253*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, sizeof (un->release), un->release); 254*7c478bd9Sstevel@tonic-gate fs = fmt_string; 255*7c478bd9Sstevel@tonic-gate } 256*7c478bd9Sstevel@tonic-gate if (vflg) { 257*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, sizeof (un->version), un->version); 258*7c478bd9Sstevel@tonic-gate fs = fmt_string; 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate if (mflg) { 261*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, sizeof (un->machine), un->machine); 262*7c478bd9Sstevel@tonic-gate fs = fmt_string; 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate if (pflg) { 265*7c478bd9Sstevel@tonic-gate if (sysinfo(SI_ARCHITECTURE, procbuf, sizeof (procbuf)) == -1) { 266*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 267*7c478bd9Sstevel@tonic-gate "uname: sysinfo failed\n")); 268*7c478bd9Sstevel@tonic-gate exit(1); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, strlen(procbuf), procbuf); 271*7c478bd9Sstevel@tonic-gate fs = fmt_string; 272*7c478bd9Sstevel@tonic-gate } 273*7c478bd9Sstevel@tonic-gate if (iflg) { 274*7c478bd9Sstevel@tonic-gate if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1) { 275*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 276*7c478bd9Sstevel@tonic-gate "uname: sysinfo failed\n")); 277*7c478bd9Sstevel@tonic-gate exit(1); 278*7c478bd9Sstevel@tonic-gate } 279*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, fs, strlen(procbuf), procbuf); 280*7c478bd9Sstevel@tonic-gate fs = fmt_string; 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate if (Xflg) { 283*7c478bd9Sstevel@tonic-gate int val; 284*7c478bd9Sstevel@tonic-gate 285*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "System = %.*s\n", sizeof (un->sysname), 286*7c478bd9Sstevel@tonic-gate un->sysname); 287*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Node = %.*s\n", sizeof (un->nodename), 288*7c478bd9Sstevel@tonic-gate un->nodename); 289*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Release = %.*s\n", sizeof (un->release), 290*7c478bd9Sstevel@tonic-gate un->release); 291*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "KernelID = %.*s\n", 292*7c478bd9Sstevel@tonic-gate sizeof (un->version), un->version); 293*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Machine = %.*s\n", sizeof (un->machine), 294*7c478bd9Sstevel@tonic-gate un->machine); 295*7c478bd9Sstevel@tonic-gate 296*7c478bd9Sstevel@tonic-gate /* Not availible on Solaris so hardcode the output */ 297*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "BusType = <unknown>\n"); 298*7c478bd9Sstevel@tonic-gate 299*7c478bd9Sstevel@tonic-gate /* Serialization is not supported in 2.6, so hard code output */ 300*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Serial = <unknown>\n"); 301*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Users = <unknown>\n"); 302*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "OEM# = 0\n"); 303*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "Origin# = 1\n"); 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate val = sysconf(_SC_NPROCESSORS_CONF); 306*7c478bd9Sstevel@tonic-gate (void) fprintf(stdout, "NumCPU = %d\n", val); 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate (void) putchar('\n'); 309*7c478bd9Sstevel@tonic-gate return (0); 310*7c478bd9Sstevel@tonic-gate } 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate static void 313*7c478bd9Sstevel@tonic-gate usage(void) 314*7c478bd9Sstevel@tonic-gate { 315*7c478bd9Sstevel@tonic-gate { 316*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 317*7c478bd9Sstevel@tonic-gate "usage: uname [-snrvmapiX]\n" 318*7c478bd9Sstevel@tonic-gate " uname [-S system_name]\n")); 319*7c478bd9Sstevel@tonic-gate } 320*7c478bd9Sstevel@tonic-gate exit(1); 321*7c478bd9Sstevel@tonic-gate } 322