blob: f7e2291a8f8cc159201568da24bc96a8895d96c2 [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
Eric Andersenc8fdb561999-10-26 05:21:02 +000034#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000035#include <stdlib.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000036#include <string.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000037#include <sys/types.h>
38#include <sys/utsname.h>
39
40#if defined (HAVE_SYSINFO) && defined (HAVE_SYS_SYSTEMINFO_H)
41# include <sys/systeminfo.h>
42#endif
Eric Andersencbe31da2001-02-20 06:14:08 +000043#include "busybox.h"
Eric Andersenc8fdb561999-10-26 05:21:02 +000044
Eric Andersenc8fdb561999-10-26 05:21:02 +000045static void print_element(unsigned int mask, char *element);
46
47/* Values that are bitwise or'd into `toprint'. */
48/* Operating system name. */
Mark Whitley59ab0252001-01-23 22:30:04 +000049static const int PRINT_SYSNAME = 1;
Eric Andersenc8fdb561999-10-26 05:21:02 +000050
51/* Node name on a communications network. */
Mark Whitley59ab0252001-01-23 22:30:04 +000052static const int PRINT_NODENAME = 2;
Eric Andersenc8fdb561999-10-26 05:21:02 +000053
54/* Operating system release. */
Mark Whitley59ab0252001-01-23 22:30:04 +000055static const int PRINT_RELEASE = 4;
Eric Andersenc8fdb561999-10-26 05:21:02 +000056
57/* Operating system version. */
Mark Whitley59ab0252001-01-23 22:30:04 +000058static const int PRINT_VERSION = 8;
Eric Andersenc8fdb561999-10-26 05:21:02 +000059
60/* Machine hardware name. */
Mark Whitley59ab0252001-01-23 22:30:04 +000061static const int PRINT_MACHINE = 16;
Eric Andersenc8fdb561999-10-26 05:21:02 +000062
63 /* Host processor type. */
Mark Whitley59ab0252001-01-23 22:30:04 +000064static const int PRINT_PROCESSOR = 32;
Eric Andersenc8fdb561999-10-26 05:21:02 +000065
66/* Mask indicating which elements of the name to print. */
67static unsigned char toprint;
68
69
70int uname_main(int argc, char **argv)
71{
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 struct utsname name;
73 char processor[256];
74
Eric Andersenc8fdb561999-10-26 05:21:02 +000075#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000077#endif
78
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 toprint = 0;
Eric Andersenc8fdb561999-10-26 05:21:02 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 /* Parse any options */
82 //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv);
83 while (--argc > 0 && **(++argv) == '-') {
84 while (*(++(*argv))) {
85 switch (**argv) {
86 case 's':
87 toprint |= PRINT_SYSNAME;
88 break;
89 case 'n':
90 toprint |= PRINT_NODENAME;
91 break;
92 case 'r':
93 toprint |= PRINT_RELEASE;
94 break;
95 case 'v':
96 toprint |= PRINT_VERSION;
97 break;
98 case 'm':
99 toprint |= PRINT_MACHINE;
100 break;
101 case 'p':
102 toprint |= PRINT_PROCESSOR;
103 break;
104 case 'a':
105 toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
106 PRINT_PROCESSOR | PRINT_VERSION |
107 PRINT_MACHINE);
108 break;
109 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000110 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000111 }
112 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000113 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000114
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 if (toprint == 0)
116 toprint = PRINT_SYSNAME;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000117
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 if (uname(&name) == -1)
Matt Kraaia9819b22000-12-22 01:48:07 +0000119 perror_msg("cannot get system name");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000120
121#if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000122 if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1)
Matt Kraaia9819b22000-12-22 01:48:07 +0000123 perror_msg("cannot get processor type");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000124}
125
126#else
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127 strcpy(processor, "unknown");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000128#endif
129
130#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131 if (fake_sparc != NULL
132 && (fake_sparc[0] == 'y'
133 || fake_sparc[0] == 'Y')) strcpy(name.machine, "sparc");
Eric Andersenc8fdb561999-10-26 05:21:02 +0000134#endif
135
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 print_element(PRINT_SYSNAME, name.sysname);
137 print_element(PRINT_NODENAME, name.nodename);
138 print_element(PRINT_RELEASE, name.release);
139 print_element(PRINT_VERSION, name.version);
140 print_element(PRINT_MACHINE, name.machine);
141 print_element(PRINT_PROCESSOR, processor);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000142
Matt Kraai3e856ce2000-12-01 02:55:13 +0000143 return EXIT_SUCCESS;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000144}
145
146/* If the name element set in MASK is selected for printing in `toprint',
147 print ELEMENT; then print a space unless it is the last element to
148 be printed, in which case print a newline. */
149
150static void print_element(unsigned int mask, char *element)
151{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 if (toprint & mask) {
153 toprint &= ~mask;
154 printf("%s%c", element, toprint ? ' ' : '\n');
155 }
Eric Andersenc8fdb561999-10-26 05:21:02 +0000156}