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