xref: /illumos-gate/usr/src/cmd/uname/uname.c (revision 84c66da4)
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
5cc22b130SRich Burridge  * Common Development and Distribution License (the "License").
6cc22b130SRich 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
20*84c66da4SAndy Fiddaman  *
21*84c66da4SAndy Fiddaman  * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
22cc22b130SRich 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	*/
26*84c66da4SAndy Fiddaman /*	  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 
52*84c66da4SAndy Fiddaman #define	OS_NAME "illumos"
53*84c66da4SAndy Fiddaman 
547c478bd9Sstevel@tonic-gate static void usage(void);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate /* ARGSUSED */
577c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])587c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	char *nodename;
61*84c66da4SAndy Fiddaman 	char *optstring = "asnrpvmioS:X";
627c478bd9Sstevel@tonic-gate 	int sflg = 0, nflg = 0, rflg = 0, vflg = 0, mflg = 0;
63*84c66da4SAndy Fiddaman 	int pflg = 0, iflg = 0, oflg = 0, Sflg = 0;
647c478bd9Sstevel@tonic-gate 	int errflg = 0, optlet;
657c478bd9Sstevel@tonic-gate 	int Xflg = 0;
667c478bd9Sstevel@tonic-gate 	struct utsname  unstr, *un;
677c478bd9Sstevel@tonic-gate 	char fmt_string[] = " %.*s";
687c478bd9Sstevel@tonic-gate 	char *fs = &fmt_string[1];
697c478bd9Sstevel@tonic-gate 	char procbuf[SYS_NMLN];
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	(void) umask(~(S_IRWXU|S_IRGRP|S_IROTH) & S_IAMB);
727c478bd9Sstevel@tonic-gate 	un = &unstr;
737c478bd9Sstevel@tonic-gate 	(void) uname(un);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
767c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
777c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
787c478bd9Sstevel@tonic-gate #endif
797c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	while ((optlet = getopt(argc, argv, optstring)) != EOF)
827c478bd9Sstevel@tonic-gate 		switch (optlet) {
837c478bd9Sstevel@tonic-gate 		case 'a':
847c478bd9Sstevel@tonic-gate 			sflg++; nflg++; rflg++; vflg++; mflg++;
85cc22b130SRich Burridge 			pflg++;
86cc22b130SRich Burridge 			iflg++;
877c478bd9Sstevel@tonic-gate 			break;
887c478bd9Sstevel@tonic-gate 		case 's':
897c478bd9Sstevel@tonic-gate 			sflg++;
907c478bd9Sstevel@tonic-gate 			break;
917c478bd9Sstevel@tonic-gate 		case 'n':
927c478bd9Sstevel@tonic-gate 			nflg++;
937c478bd9Sstevel@tonic-gate 			break;
947c478bd9Sstevel@tonic-gate 		case 'r':
957c478bd9Sstevel@tonic-gate 			rflg++;
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case 'v':
987c478bd9Sstevel@tonic-gate 			vflg++;
997c478bd9Sstevel@tonic-gate 			break;
1007c478bd9Sstevel@tonic-gate 		case 'm':
1017c478bd9Sstevel@tonic-gate 			mflg++;
1027c478bd9Sstevel@tonic-gate 			break;
1037c478bd9Sstevel@tonic-gate 		case 'p':
1047c478bd9Sstevel@tonic-gate 			pflg++;
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 		case 'i':
1077c478bd9Sstevel@tonic-gate 			iflg++;
1087c478bd9Sstevel@tonic-gate 			break;
109*84c66da4SAndy Fiddaman 		case 'o':
110*84c66da4SAndy Fiddaman 			oflg++;
111*84c66da4SAndy Fiddaman 			break;
1127c478bd9Sstevel@tonic-gate 		case 'S':
1137c478bd9Sstevel@tonic-gate 			Sflg++;
1147c478bd9Sstevel@tonic-gate 			nodename = optarg;
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		case 'X':
1177c478bd9Sstevel@tonic-gate 			Xflg++;
1187c478bd9Sstevel@tonic-gate 			break;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 		case '?':
1217c478bd9Sstevel@tonic-gate 			errflg++;
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	if (errflg || (optind != argc))
1257c478bd9Sstevel@tonic-gate 		usage();
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	if ((Sflg > 1) ||
1287c478bd9Sstevel@tonic-gate 	    (Sflg && (sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
129*84c66da4SAndy Fiddaman 	    oflg || Xflg))) {
1307c478bd9Sstevel@tonic-gate 		usage();
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/* If we're changing the system name */
1347c478bd9Sstevel@tonic-gate 	if (Sflg) {
1357c478bd9Sstevel@tonic-gate 		int len = strlen(nodename);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		if (len > SYS_NMLN - 1) {
1387c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
139cc22b130SRich Burridge 			    "uname: name must be <= %d letters\n"),
140cc22b130SRich Burridge 			    SYS_NMLN-1);
1417c478bd9Sstevel@tonic-gate 			exit(1);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) {
1447c478bd9Sstevel@tonic-gate 			int err = errno;
1457c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
146cc22b130SRich Burridge 			    "uname: error in setting name: %s\n"),
147cc22b130SRich Burridge 			    strerror(err));
1487c478bd9Sstevel@tonic-gate 			exit(1);
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 		return (0);
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * "uname -s" is the default
1557c478bd9Sstevel@tonic-gate 	 */
156*84c66da4SAndy Fiddaman 	if (!(sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
157*84c66da4SAndy Fiddaman 	    oflg || Xflg))
1587c478bd9Sstevel@tonic-gate 		sflg++;
1597c478bd9Sstevel@tonic-gate 	if (sflg) {
1607c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->sysname),
1617c478bd9Sstevel@tonic-gate 		    un->sysname);
1627c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	if (nflg) {
1657c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->nodename), un->nodename);
1667c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 	if (rflg) {
1697c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->release), un->release);
1707c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 	if (vflg) {
1737c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->version), un->version);
1747c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	if (mflg) {
1777c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, sizeof (un->machine), un->machine);
1787c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	if (pflg) {
1817c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_ARCHITECTURE, procbuf, sizeof (procbuf)) == -1) {
1827c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
183cc22b130SRich Burridge 			    "uname: sysinfo failed\n"));
1847c478bd9Sstevel@tonic-gate 			exit(1);
1857c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
1877c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate 	if (iflg) {
1907c478bd9Sstevel@tonic-gate 		if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1) {
1917c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
192cc22b130SRich Burridge 			    "uname: sysinfo failed\n"));
1937c478bd9Sstevel@tonic-gate 			exit(1);
1947c478bd9Sstevel@tonic-gate 		}
1957c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
1967c478bd9Sstevel@tonic-gate 		fs = fmt_string;
1977c478bd9Sstevel@tonic-gate 	}
198*84c66da4SAndy Fiddaman 	if (oflg) {
199*84c66da4SAndy Fiddaman 		(void) fprintf(stdout, fs, strlen(OS_NAME), OS_NAME);
200*84c66da4SAndy Fiddaman 		fs = fmt_string;
201*84c66da4SAndy Fiddaman 	}
2027c478bd9Sstevel@tonic-gate 	if (Xflg) {
2037c478bd9Sstevel@tonic-gate 		int	val;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "System = %.*s\n", sizeof (un->sysname),
2067c478bd9Sstevel@tonic-gate 		    un->sysname);
2077c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Node = %.*s\n", sizeof (un->nodename),
2087c478bd9Sstevel@tonic-gate 		    un->nodename);
2097c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Release = %.*s\n", sizeof (un->release),
2107c478bd9Sstevel@tonic-gate 		    un->release);
2117c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "KernelID = %.*s\n",
2127c478bd9Sstevel@tonic-gate 		    sizeof (un->version), un->version);
2137c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Machine = %.*s\n", sizeof (un->machine),
2147c478bd9Sstevel@tonic-gate 		    un->machine);
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 		/* Not availible on Solaris so hardcode the output */
2177c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "BusType = <unknown>\n");
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		/* Serialization is not supported in 2.6, so hard code output */
2207c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Serial = <unknown>\n");
2217c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Users = <unknown>\n");
2227c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "OEM# = 0\n");
2237c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "Origin# = 1\n");
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		val = sysconf(_SC_NPROCESSORS_CONF);
2267c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "NumCPU = %d\n", val);
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	(void) putchar('\n');
2297c478bd9Sstevel@tonic-gate 	return (0);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate static void
usage(void)2337c478bd9Sstevel@tonic-gate usage(void)
2347c478bd9Sstevel@tonic-gate {
2357c478bd9Sstevel@tonic-gate 	{
2367c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
237*84c66da4SAndy Fiddaman 		    "usage:	uname [-snrvmapioX]\n"
238cc22b130SRich Burridge 		    "	uname [-S system_name]\n"));
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	exit(1);
2417c478bd9Sstevel@tonic-gate }
242