xref: /illumos-gate/usr/src/cmd/uname/uname.c (revision 069e6b7e31ba5dcbc5441b98af272714d9a5455c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Copyright (c) 2012 Nexenta Systems, Inc. All rights reserved.
22  * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26 /*	  All Rights Reserved	*/
27 
28 /*
29  * University Copyright- Copyright (c) 1982, 1986, 1988
30  * The Regents of the University of California
31  * All Rights Reserved
32  *
33  * University Acknowledgment- Portions of this document are derived from
34  * software developed by the University of California, Berkeley, and its
35  * contributors.
36  */
37 
38 #define	__EXTENSIONS__
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <locale.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <errno.h>
47 #include <sys/fcntl.h>
48 #include <sys/stat.h>
49 #include <sys/utsname.h>
50 #include <sys/systeminfo.h>
51 
52 #define	OS_NAME "illumos"
53 
54 static void usage(void);
55 
56 /* ARGSUSED */
57 int
58 main(int argc, char *argv[], char *envp[])
59 {
60 	char *nodename;
61 	char *optstring = "asnrpvmioS:X";
62 	int sflg = 0, nflg = 0, rflg = 0, vflg = 0, mflg = 0;
63 	int pflg = 0, iflg = 0, oflg = 0, Sflg = 0;
64 	int errflg = 0, optlet;
65 	int Xflg = 0;
66 	struct utsname  unstr, *un;
67 	char fmt_string[] = " %.*s";
68 	char *fs = &fmt_string[1];
69 	char procbuf[SYS_NMLN];
70 
71 	(void) umask(~(S_IRWXU|S_IRGRP|S_IROTH) & S_IAMB);
72 	un = &unstr;
73 	(void) uname(un);
74 
75 	(void) setlocale(LC_ALL, "");
76 #if !defined(TEXT_DOMAIN)
77 #define	TEXT_DOMAIN "SYS_TEST"
78 #endif
79 	(void) textdomain(TEXT_DOMAIN);
80 
81 	while ((optlet = getopt(argc, argv, optstring)) != EOF)
82 		switch (optlet) {
83 		case 'a':
84 			sflg++; nflg++; rflg++; vflg++; mflg++;
85 			pflg++;
86 			iflg++;
87 			break;
88 		case 's':
89 			sflg++;
90 			break;
91 		case 'n':
92 			nflg++;
93 			break;
94 		case 'r':
95 			rflg++;
96 			break;
97 		case 'v':
98 			vflg++;
99 			break;
100 		case 'm':
101 			mflg++;
102 			break;
103 		case 'p':
104 			pflg++;
105 			break;
106 		case 'i':
107 			iflg++;
108 			break;
109 		case 'o':
110 			oflg++;
111 			break;
112 		case 'S':
113 			Sflg++;
114 			nodename = optarg;
115 			break;
116 		case 'X':
117 			Xflg++;
118 			break;
119 
120 		case '?':
121 			errflg++;
122 		}
123 
124 	if (errflg || (optind != argc))
125 		usage();
126 
127 	if ((Sflg > 1) ||
128 	    (Sflg && (sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
129 	    oflg || Xflg))) {
130 		usage();
131 	}
132 
133 	/* If we're changing the system name */
134 	if (Sflg) {
135 		int len = strlen(nodename);
136 
137 		if (len > SYS_NMLN - 1) {
138 			(void) fprintf(stderr, gettext(
139 			    "uname: name must be <= %d letters\n"),
140 			    SYS_NMLN-1);
141 			exit(1);
142 		}
143 		if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) {
144 			int err = errno;
145 			(void) fprintf(stderr, gettext(
146 			    "uname: error in setting name: %s\n"),
147 			    strerror(err));
148 			exit(1);
149 		}
150 		return (0);
151 	}
152 
153 	/*
154 	 * "uname -s" is the default
155 	 */
156 	if (!(sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
157 	    oflg || Xflg))
158 		sflg++;
159 	if (sflg) {
160 		(void) fprintf(stdout, fs, sizeof (un->sysname),
161 		    un->sysname);
162 		fs = fmt_string;
163 	}
164 	if (nflg) {
165 		(void) fprintf(stdout, fs, sizeof (un->nodename), un->nodename);
166 		fs = fmt_string;
167 	}
168 	if (rflg) {
169 		(void) fprintf(stdout, fs, sizeof (un->release), un->release);
170 		fs = fmt_string;
171 	}
172 	if (vflg) {
173 		(void) fprintf(stdout, fs, sizeof (un->version), un->version);
174 		fs = fmt_string;
175 	}
176 	if (mflg) {
177 		(void) fprintf(stdout, fs, sizeof (un->machine), un->machine);
178 		fs = fmt_string;
179 	}
180 	if (pflg) {
181 		if (sysinfo(SI_ARCHITECTURE, procbuf, sizeof (procbuf)) == -1) {
182 			(void) fprintf(stderr, gettext(
183 			    "uname: sysinfo failed\n"));
184 			exit(1);
185 		}
186 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
187 		fs = fmt_string;
188 	}
189 	if (iflg) {
190 		if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1) {
191 			(void) fprintf(stderr, gettext(
192 			    "uname: sysinfo failed\n"));
193 			exit(1);
194 		}
195 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
196 		fs = fmt_string;
197 	}
198 	if (oflg) {
199 		(void) fprintf(stdout, fs, strlen(OS_NAME), OS_NAME);
200 		fs = fmt_string;
201 	}
202 	if (Xflg) {
203 		int	val;
204 
205 		(void) fprintf(stdout, "System = %.*s\n", sizeof (un->sysname),
206 		    un->sysname);
207 		(void) fprintf(stdout, "Node = %.*s\n", sizeof (un->nodename),
208 		    un->nodename);
209 		(void) fprintf(stdout, "Release = %.*s\n", sizeof (un->release),
210 		    un->release);
211 		(void) fprintf(stdout, "KernelID = %.*s\n",
212 		    sizeof (un->version), un->version);
213 		(void) fprintf(stdout, "Machine = %.*s\n", sizeof (un->machine),
214 		    un->machine);
215 
216 		/* Not availible on Solaris so hardcode the output */
217 		(void) fprintf(stdout, "BusType = <unknown>\n");
218 
219 		/* Serialization is not supported in 2.6, so hard code output */
220 		(void) fprintf(stdout, "Serial = <unknown>\n");
221 		(void) fprintf(stdout, "Users = <unknown>\n");
222 		(void) fprintf(stdout, "OEM# = 0\n");
223 		(void) fprintf(stdout, "Origin# = 1\n");
224 
225 		val = sysconf(_SC_NPROCESSORS_CONF);
226 		(void) fprintf(stdout, "NumCPU = %d\n", val);
227 	}
228 	(void) putchar('\n');
229 	return (0);
230 }
231 
232 static void
233 usage(void)
234 {
235 	{
236 		(void) fprintf(stderr, gettext(
237 		    "usage:	uname [-snrvmapioX]\n"
238 		    "	uname [-S system_name]\n"));
239 	}
240 	exit(1);
241 }
242