xref: /titanic_44/usr/src/cmd/uname/uname.c (revision bde3d612a7c090234c60e6e4578821237a5db135)
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 /*
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 static void usage(void);
53 
54 /* ARGSUSED */
55 int
56 main(int argc, char *argv[], char *envp[])
57 {
58 	char *nodename;
59 	char *optstring = "asnrpvmiS:X";
60 	int sflg = 0, nflg = 0, rflg = 0, vflg = 0, mflg = 0;
61 	int pflg = 0, iflg = 0, Sflg = 0;
62 	int errflg = 0, optlet;
63 	int Xflg = 0;
64 	struct utsname  unstr, *un;
65 	char fmt_string[] = " %.*s";
66 	char *fs = &fmt_string[1];
67 	char procbuf[SYS_NMLN];
68 
69 	(void) umask(~(S_IRWXU|S_IRGRP|S_IROTH) & S_IAMB);
70 	un = &unstr;
71 	(void) uname(un);
72 
73 	(void) setlocale(LC_ALL, "");
74 #if !defined(TEXT_DOMAIN)
75 #define	TEXT_DOMAIN "SYS_TEST"
76 #endif
77 	(void) textdomain(TEXT_DOMAIN);
78 
79 	while ((optlet = getopt(argc, argv, optstring)) != EOF)
80 		switch (optlet) {
81 		case 'a':
82 			sflg++; nflg++; rflg++; vflg++; mflg++;
83 			pflg++;
84 			iflg++;
85 			break;
86 		case 's':
87 			sflg++;
88 			break;
89 		case 'n':
90 			nflg++;
91 			break;
92 		case 'r':
93 			rflg++;
94 			break;
95 		case 'v':
96 			vflg++;
97 			break;
98 		case 'm':
99 			mflg++;
100 			break;
101 		case 'p':
102 			pflg++;
103 			break;
104 		case 'i':
105 			iflg++;
106 			break;
107 		case 'S':
108 			Sflg++;
109 			nodename = optarg;
110 			break;
111 		case 'X':
112 			Xflg++;
113 			break;
114 
115 		case '?':
116 			errflg++;
117 		}
118 
119 	if (errflg || (optind != argc))
120 		usage();
121 
122 	if ((Sflg > 1) ||
123 	    (Sflg && (sflg || nflg || rflg || vflg || mflg || pflg || iflg ||
124 	    Xflg))) {
125 		usage();
126 	}
127 
128 	/* If we're changing the system name */
129 	if (Sflg) {
130 		int len = strlen(nodename);
131 
132 		if (len > SYS_NMLN - 1) {
133 			(void) fprintf(stderr, gettext(
134 			    "uname: name must be <= %d letters\n"),
135 			    SYS_NMLN-1);
136 			exit(1);
137 		}
138 		if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) {
139 			int err = errno;
140 			(void) fprintf(stderr, gettext(
141 			    "uname: error in setting name: %s\n"),
142 			    strerror(err));
143 			exit(1);
144 		}
145 		return (0);
146 	}
147 
148 	/*
149 	 * "uname -s" is the default
150 	 */
151 	if (!(sflg || nflg || rflg || vflg || mflg || pflg || iflg || Xflg))
152 		sflg++;
153 	if (sflg) {
154 		(void) fprintf(stdout, fs, sizeof (un->sysname),
155 		    un->sysname);
156 		fs = fmt_string;
157 	}
158 	if (nflg) {
159 		(void) fprintf(stdout, fs, sizeof (un->nodename), un->nodename);
160 		fs = fmt_string;
161 	}
162 	if (rflg) {
163 		(void) fprintf(stdout, fs, sizeof (un->release), un->release);
164 		fs = fmt_string;
165 	}
166 	if (vflg) {
167 		(void) fprintf(stdout, fs, sizeof (un->version), un->version);
168 		fs = fmt_string;
169 	}
170 	if (mflg) {
171 		(void) fprintf(stdout, fs, sizeof (un->machine), un->machine);
172 		fs = fmt_string;
173 	}
174 	if (pflg) {
175 		if (sysinfo(SI_ARCHITECTURE, procbuf, sizeof (procbuf)) == -1) {
176 			(void) fprintf(stderr, gettext(
177 			    "uname: sysinfo failed\n"));
178 			exit(1);
179 		}
180 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
181 		fs = fmt_string;
182 	}
183 	if (iflg) {
184 		if (sysinfo(SI_PLATFORM, procbuf, sizeof (procbuf)) == -1) {
185 			(void) fprintf(stderr, gettext(
186 			    "uname: sysinfo failed\n"));
187 			exit(1);
188 		}
189 		(void) fprintf(stdout, fs, strlen(procbuf), procbuf);
190 		fs = fmt_string;
191 	}
192 	if (Xflg) {
193 		int	val;
194 
195 		(void) fprintf(stdout, "System = %.*s\n", sizeof (un->sysname),
196 		    un->sysname);
197 		(void) fprintf(stdout, "Node = %.*s\n", sizeof (un->nodename),
198 		    un->nodename);
199 		(void) fprintf(stdout, "Release = %.*s\n", sizeof (un->release),
200 		    un->release);
201 		(void) fprintf(stdout, "KernelID = %.*s\n",
202 		    sizeof (un->version), un->version);
203 		(void) fprintf(stdout, "Machine = %.*s\n", sizeof (un->machine),
204 		    un->machine);
205 
206 		/* Not availible on Solaris so hardcode the output */
207 		(void) fprintf(stdout, "BusType = <unknown>\n");
208 
209 		/* Serialization is not supported in 2.6, so hard code output */
210 		(void) fprintf(stdout, "Serial = <unknown>\n");
211 		(void) fprintf(stdout, "Users = <unknown>\n");
212 		(void) fprintf(stdout, "OEM# = 0\n");
213 		(void) fprintf(stdout, "Origin# = 1\n");
214 
215 		val = sysconf(_SC_NPROCESSORS_CONF);
216 		(void) fprintf(stdout, "NumCPU = %d\n", val);
217 	}
218 	(void) putchar('\n');
219 	return (0);
220 }
221 
222 static void
223 usage(void)
224 {
225 	{
226 		(void) fprintf(stderr, gettext(
227 		    "usage:	uname [-snrvmapiX]\n"
228 		    "	uname [-S system_name]\n"));
229 	}
230 	exit(1);
231 }
232