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