xref: /titanic_51/usr/src/cmd/uname/uname.c (revision cc22b130832529204c03214239a57aaadd05101f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*cc22b130SRich Burridge  * Common Development and Distribution License (the "License").
6*cc22b130SRich Burridge  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*cc22b130SRich Burridge  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #define	__EXTENSIONS__
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <errno.h>
477c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/stat.h>
497c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
507c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void usage(void);
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* ARGSUSED */
557c478bd9Sstevel@tonic-gate int
567c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	char *nodename;
597c478bd9Sstevel@tonic-gate 	char *optstring = "asnrpvmiS:X";
607c478bd9Sstevel@tonic-gate 	int sflg = 0, nflg = 0, rflg = 0, vflg = 0, mflg = 0;
617c478bd9Sstevel@tonic-gate 	int pflg = 0, iflg = 0, Sflg = 0;
627c478bd9Sstevel@tonic-gate 	int errflg = 0, optlet;
637c478bd9Sstevel@tonic-gate 	int Xflg = 0;
647c478bd9Sstevel@tonic-gate 	struct utsname  unstr, *un;
657c478bd9Sstevel@tonic-gate 	char fmt_string[] = " %.*s";
667c478bd9Sstevel@tonic-gate 	char *fs = &fmt_string[1];
677c478bd9Sstevel@tonic-gate 	char procbuf[SYS_NMLN];
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	(void) umask(~(S_IRWXU|S_IRGRP|S_IROTH) & S_IAMB);
707c478bd9Sstevel@tonic-gate 	un = &unstr;
717c478bd9Sstevel@tonic-gate 	(void) uname(un);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
747c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
757c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	while ((optlet = getopt(argc, argv, optstring)) != EOF)
807c478bd9Sstevel@tonic-gate 		switch (optlet) {
817c478bd9Sstevel@tonic-gate 		case 'a':
827c478bd9Sstevel@tonic-gate 			sflg++; nflg++; rflg++; vflg++; mflg++;
837c478bd9Sstevel@tonic-gate 			pflg++;
847c478bd9Sstevel@tonic-gate 			iflg++;
857c478bd9Sstevel@tonic-gate 			break;
867c478bd9Sstevel@tonic-gate 		case 's':
877c478bd9Sstevel@tonic-gate 			sflg++;
887c478bd9Sstevel@tonic-gate 			break;
897c478bd9Sstevel@tonic-gate 		case 'n':
907c478bd9Sstevel@tonic-gate 			nflg++;
917c478bd9Sstevel@tonic-gate 			break;
927c478bd9Sstevel@tonic-gate 		case 'r':
937c478bd9Sstevel@tonic-gate 			rflg++;
947c478bd9Sstevel@tonic-gate 			break;
957c478bd9Sstevel@tonic-gate 		case 'v':
967c478bd9Sstevel@tonic-gate 			vflg++;
977c478bd9Sstevel@tonic-gate 			break;
987c478bd9Sstevel@tonic-gate 		case 'm':
997c478bd9Sstevel@tonic-gate 			mflg++;
1007c478bd9Sstevel@tonic-gate 			break;
1017c478bd9Sstevel@tonic-gate 		case 'p':
1027c478bd9Sstevel@tonic-gate 			pflg++;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		case 'i':
1057c478bd9Sstevel@tonic-gate 			iflg++;
1067c478bd9Sstevel@tonic-gate 			break;
1077c478bd9Sstevel@tonic-gate 		case 'S':
1087c478bd9Sstevel@tonic-gate 			Sflg++;
1097c478bd9Sstevel@tonic-gate 			nodename = optarg;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case 'X':
1127c478bd9Sstevel@tonic-gate 			Xflg++;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		case '?':
1167c478bd9Sstevel@tonic-gate 			errflg++;
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (errflg || (optind != argc))
1207c478bd9Sstevel@tonic-gate 		usage();
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	if ((Sflg > 1) ||
1237c478bd9Sstevel@tonic-gate 	    (Sflg && (sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
1247c478bd9Sstevel@tonic-gate 	    Xflg))) {
1257c478bd9Sstevel@tonic-gate 		usage();
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	/* If we're changing the system name */
1297c478bd9Sstevel@tonic-gate 	if (Sflg) {
1307c478bd9Sstevel@tonic-gate 		int len = strlen(nodename);
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 		if (len > SYS_NMLN - 1) {
1337c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1347c478bd9Sstevel@tonic-gate 			    "uname: name must be <= %d letters\n"),
1357c478bd9Sstevel@tonic-gate 			    SYS_NMLN-1);
1367c478bd9Sstevel@tonic-gate 			exit(1);
1377c478bd9Sstevel@tonic-gate 		}
1387c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) {
1397c478bd9Sstevel@tonic-gate 			int err = errno;
1407c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1417c478bd9Sstevel@tonic-gate 			    "uname: error in setting name: %s\n"),
1427c478bd9Sstevel@tonic-gate 			    strerror(err));
1437c478bd9Sstevel@tonic-gate 			exit(1);
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		return (0);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 	/*
1497c478bd9Sstevel@tonic-gate 	 * "uname -s" is the default
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 	if (!(sflg || nflg || rflg || vflg || mflg || pflg || iflg || Xflg))
1527c478bd9Sstevel@tonic-gate 		sflg++;
1537c478bd9Sstevel@tonic-gate 	if (sflg) {
1547c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->sysname),
1557c478bd9Sstevel@tonic-gate 		    un->sysname);
1567c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 	if (nflg) {
1597c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->nodename), un->nodename);
1607c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	if (rflg) {
1637c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->release), un->release);
1647c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 	if (vflg) {
1677c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->version), un->version);
1687c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	if (mflg) {
1717c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->machine), un->machine);
1727c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 	if (pflg) {
1757c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, procbuf, sizeof (procbuf)) == -1) {
1767c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1777c478bd9Sstevel@tonic-gate 			    "uname: sysinfo failed\n"));
1787c478bd9Sstevel@tonic-gate 			exit(1);
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
1817c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate 	if (iflg) {
1847c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1) {
1857c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
1867c478bd9Sstevel@tonic-gate 			    "uname: sysinfo failed\n"));
1877c478bd9Sstevel@tonic-gate 			exit(1);
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
1907c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 	if (Xflg) {
1937c478bd9Sstevel@tonic-gate 		int	val;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "System = %.*s\n", sizeof (un->sysname),
1967c478bd9Sstevel@tonic-gate 		    un->sysname);
1977c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Node = %.*s\n", sizeof (un->nodename),
1987c478bd9Sstevel@tonic-gate 		    un->nodename);
1997c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Release = %.*s\n", sizeof (un->release),
2007c478bd9Sstevel@tonic-gate 		    un->release);
2017c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "KernelID = %.*s\n",
2027c478bd9Sstevel@tonic-gate 		    sizeof (un->version), un->version);
2037c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Machine = %.*s\n", sizeof (un->machine),
2047c478bd9Sstevel@tonic-gate 		    un->machine);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		/* Not availible on Solaris so hardcode the output */
2077c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "BusType = <unknown>\n");
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 		/* Serialization is not supported in 2.6, so hard code output */
2107c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Serial = <unknown>\n");
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Users = <unknown>\n");
2127c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "OEM# = 0\n");
2137c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Origin# = 1\n");
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		val = sysconf(_SC_NPROCESSORS_CONF);
2167c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "NumCPU = %d\n", val);
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
2197c478bd9Sstevel@tonic-gate 	return (0);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate static void
2237c478bd9Sstevel@tonic-gate usage(void)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 	{
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
2277c478bd9Sstevel@tonic-gate 		    "usage:	uname [-snrvmapiX]\n"
2287c478bd9Sstevel@tonic-gate 		    "	uname [-S system_name]\n"));
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 	exit(1);
2317c478bd9Sstevel@tonic-gate }
232