blob: 9a1cb808aa56d62b28ee30d49d1fcf5d69a4c3a5 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc8fdb561999-10-26 05:21:02 +00002/* uname -- print system information
3 Copyright (C) 1989-1999 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19/* Option Example
20
21 -s, --sysname SunOS
22 -n, --nodename rocky8
23 -r, --release 4.0
24 -v, --version
25 -m, --machine sun
26 -a, --all SunOS rocky8 4.0 sun
27
28 The default behavior is equivalent to `-s'.
29
30 David MacKenzie <djm@gnu.ai.mit.edu> */
31
32/* Busyboxed by Erik Andersen */
33
34#include "internal.h"
35#include <stdio.h>
36#include <sys/types.h>
37#include <sys/utsname.h>
38
39#if defined (HAVE_SYSINFO) && defined (HAVE_SYS_SYSTEMINFO_H)
40# include <sys/systeminfo.h>
41#endif
42
43
44static const char uname_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 "uname [OPTION]...\n\n"
46 "Print certain system information. With no OPTION, same as -s.\n\n"
47 "Options:\n"
48 "\t-a\tprint all information\n"
49 "\t-m\tthe machine (hardware) type\n"
50 "\t-n\tprint the machine's network node hostname\n"
51 "\t-r\tprint the operating system release\n"
52 "\t-s\tprint the operating system name\n"
53
54 "\t-p\tprint the host processor type\n"
55 "\t-v\tprint the operating system version\n";
Eric Andersenc8fdb561999-10-26 05:21:02 +000056
57
58static void print_element(unsigned int mask, char *element);
59
60/* Values that are bitwise or'd into `toprint'. */
61/* Operating system name. */
62#define PRINT_SYSNAME 1
63
64/* Node name on a communications network. */
65#define PRINT_NODENAME 2
66
67/* Operating system release. */
68#define PRINT_RELEASE 4
69
70/* Operating system version. */
71#define PRINT_VERSION 8
72
73/* Machine hardware name. */
74#define PRINT_MACHINE 16
75
76 /* Host processor type. */
77#define PRINT_PROCESSOR 32
78
79/* Mask indicating which elements of the name to print. */
80static unsigned char toprint;
81
82
83int uname_main(int argc, char **argv)
84{
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 struct utsname name;
86 char processor[256];
87
Eric Andersenc8fdb561999-10-26 05:21:02 +000088#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000090#endif
91
Erik Andersene49d5ec2000-02-08 19:58:47 +000092 toprint = 0;
Eric Andersenc8fdb561999-10-26 05:21:02 +000093
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 /* Parse any options */
95 //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv);
96 while (--argc > 0 && **(++argv) == '-') {
97 while (*(++(*argv))) {
98 switch (**argv) {
99 case 's':
100 toprint |= PRINT_SYSNAME;
101 break;
102 case 'n':
103 toprint |= PRINT_NODENAME;
104 break;
105 case 'r':
106 toprint |= PRINT_RELEASE;
107 break;
108 case 'v':
109 toprint |= PRINT_VERSION;
110 break;
111 case 'm':
112 toprint |= PRINT_MACHINE;
113 break;
114 case 'p':
115 toprint |= PRINT_PROCESSOR;
116 break;
117 case 'a':
118 toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
119 PRINT_PROCESSOR | PRINT_VERSION |
120 PRINT_MACHINE);
121 break;
122 default:
123 usage(uname_usage);
124 }
125 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000126 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000127
Erik Andersene49d5ec2000-02-08 19:58:47 +0000128 if (toprint == 0)
129 toprint = PRINT_SYSNAME;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000130
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (uname(&name) == -1)
132 perror("cannot get system name");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000133
134#if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000135 if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1)
136 perror("cannot get processor type");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000137}
138
139#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000140 strcpy(processor, "unknown");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000141#endif
142
143#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000144 if (fake_sparc != NULL
145 && (fake_sparc[0] == 'y'
146 || fake_sparc[0] == 'Y')) strcpy(name.machine, "sparc");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000147#endif
148
Erik Andersene49d5ec2000-02-08 19:58:47 +0000149 print_element(PRINT_SYSNAME, name.sysname);
150 print_element(PRINT_NODENAME, name.nodename);
151 print_element(PRINT_RELEASE, name.release);
152 print_element(PRINT_VERSION, name.version);
153 print_element(PRINT_MACHINE, name.machine);
154 print_element(PRINT_PROCESSOR, processor);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000155
Erik Andersene49d5ec2000-02-08 19:58:47 +0000156 exit(TRUE);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000157}
158
159/* If the name element set in MASK is selected for printing in `toprint',
160 print ELEMENT; then print a space unless it is the last element to
161 be printed, in which case print a newline. */
162
163static void print_element(unsigned int mask, char *element)
164{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000165 if (toprint & mask) {
166 toprint &= ~mask;
167 printf("%s%c", element, toprint ? ' ' : '\n');
168 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000169}