1 /*
2  * uname - get system name
3  *
4  * Gunnar Ritter, Freiburg i. Br., Germany, August 2002.
5  */
6 /*
7  * Copyright (c) 2003 Gunnar Ritter
8  *
9  * This software is provided 'as-is', without any express or implied
10  * warranty. In no event will the authors be held liable for any damages
11  * arising from the use of this software.
12  *
13  * Permission is granted to anyone to use this software for any purpose,
14  * including commercial applications, and to alter it and redistribute
15  * it freely, subject to the following restrictions:
16  *
17  * 1. The origin of this software must not be misrepresented; you must not
18  *    claim that you wrote the original software. If you use this software
19  *    in a product, an acknowledgment in the product documentation would be
20  *    appreciated but is not required.
21  *
22  * 2. Altered source versions must be plainly marked as such, and must not be
23  *    misrepresented as being the original software.
24  *
25  * 3. This notice may not be removed or altered from any source distribution.
26  */
27 
28 #if __GNUC__ >= 3 && __GNUC_MINOR__ >= 4 || __GNUC__ >= 4
29 #define	USED	__attribute__ ((used))
30 #elif defined __GNUC__
31 #define	USED	__attribute__ ((unused))
32 #else
33 #define	USED
34 #endif
35 static const char sccsid[] USED = "@(#)uname.sl	1.16 (gritter) 5/29/05";
36 
37 #include	<sys/utsname.h>
38 #include	<unistd.h>
39 #include	<stdio.h>
40 #include	<string.h>
41 #include	<stdlib.h>
42 #include	<errno.h>
43 #include	<libgen.h>
44 
45 #if defined (__i386__)
46 #define	PROCESSOR	"ia32"
47 #elif defined (__x86_64__)
48 #define	PROCESSOR	"x86_64"
49 #elif defined (__itanium__)
50 #define	PROCESSOR	"ia64"
51 #elif defined (__sparc)
52 #define	PROCESSOR	"sparc"
53 #elif defined (__R5900)
54 #define	PROCESSOR	"r5900"
55 #else
56 #define	PROCESSOR	"unknown"
57 #endif
58 
59 static enum {
60 	FL_M = 0001,
61 	FL_N = 0002,
62 	FL_R = 0004,
63 	FL_S = 0010,
64 	FL_V = 0020,
65 	FL_P = 0040,
66 	FL_A = 0777
67 } flags;
68 
69 static unsigned	errcnt;			/* count of errors */
70 static char	*progname;		/* argv[0] to main() */
71 static char	*system_name;		/* new system name */
72 
73 static void
usage(void)74 usage(void)
75 {
76 	fprintf(stderr, "\
77 usage:  %s [-snrvmap]\n\
78         %s [-S system name]\n",
79        		progname, progname);
80 	exit(2);
81 }
82 
83 #define	FIELD(x)	printf("%s%s", hadfield++ ? " " : "", x)
84 
85 static int
go(void)86 go(void)
87 {
88 	struct utsname	u;
89 	char	*sysv3, *token;
90 	int	hadfield = 0;
91 
92 	if (uname(&u) < 0) {
93 		fprintf(stderr, "%s: uname: %s\n", progname, strerror(errno));
94 		return 1;
95 	}
96 	if ((sysv3 = getenv("SYSV3")) != NULL && *sysv3 != '\0') {
97 		token = strtok(sysv3, ",");
98 		if (token = strtok(0, ","))
99 			snprintf(u.sysname, sizeof u.sysname, token);
100 		if (token = strtok(0, ","))
101 			snprintf(u.nodename, sizeof u.nodename, token);
102 		if (token = strtok(0, ","))
103 			snprintf(u.release, sizeof u.release, token);
104 		if (token = strtok(0, ","))
105 			snprintf(u.version, sizeof u.version, token);
106 		if (token = strtok(0, ","))
107 			snprintf(u.machine, sizeof u.machine, token);
108 	} else if (sysv3) {
109 		snprintf(u.sysname, sizeof u.sysname, u.nodename);
110 		snprintf(u.release, sizeof u.release, "3.2");
111 		snprintf(u.version, sizeof u.version, "2");
112 		snprintf(u.machine, sizeof u.machine, "i386");
113 	}
114 	if (flags & FL_S)
115 		FIELD(u.sysname);
116 	if (flags & FL_N)
117 		FIELD(u.nodename);
118 	if (flags & FL_R)
119 		FIELD(u.release);
120 	if (flags & FL_V)
121 		FIELD(u.version);
122 	if (flags & FL_M)
123 		FIELD(u.machine);
124 	if (flags & FL_P && (sysv3 == 0 || flags != FL_A))
125 		FIELD(PROCESSOR);
126 	if (hadfield)
127 		putchar('\n');
128 	return 0;
129 }
130 
131 int
main(int argc,char ** argv)132 main(int argc, char **argv)
133 {
134 	const char	optstring[] = "amnprsvS:";
135 	int	i;
136 
137 #ifdef	__GLIBC__
138 	putenv("POSIXLY_CORRECT=1");
139 #endif
140 	progname = basename(argv[0]);
141 	while ((i = getopt(argc, argv, optstring)) != EOF) {
142 		switch (i) {
143 		case 'a':
144 			flags |= FL_A;
145 			break;
146 		case 'm':
147 			flags |= FL_M;
148 			break;
149 		case 'n':
150 			flags |= FL_N;
151 			break;
152 		case 'p':
153 			flags |= FL_P;
154 			break;
155 		case 'r':
156 			flags |= FL_R;
157 			break;
158 		case 's':
159 			flags |= FL_S;
160 			break;
161 		case 'v':
162 			flags |= FL_V;
163 			break;
164 		case 'S':
165 			system_name = optarg;
166 			break;
167 		default:
168 			usage();
169 		}
170 	}
171 	if (optind < argc)
172 		usage();
173 	if (system_name) {
174 		if (flags)
175 			usage();
176 		if (sethostname(system_name, strlen(system_name)) < 0) {
177 			fprintf(stderr, "%s: sethostname: %s\n",
178 					progname, strerror(errno));
179 			errcnt |= 010;
180 		}
181 	} else {
182 		if (flags == 0)
183 			flags = FL_S;
184 		errcnt |= go();
185 	}
186 	return errcnt;
187 }
188