aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/uname.c144
1 files changed, 48 insertions, 96 deletions
diff --git a/coreutils/uname.c b/coreutils/uname.c
index f7e2291a8..0f3a7d5c0 100644
--- a/coreutils/uname.c
+++ b/coreutils/uname.c
@@ -31,126 +31,78 @@
31 31
32/* Busyboxed by Erik Andersen */ 32/* Busyboxed by Erik Andersen */
33 33
34/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
35
34#include <stdio.h> 36#include <stdio.h>
35#include <stdlib.h> 37#include <stdlib.h>
38#include <stddef.h>
36#include <string.h> 39#include <string.h>
37#include <sys/types.h> 40#include <sys/types.h>
38#include <sys/utsname.h> 41#include <sys/utsname.h>
39 42#include <getopt.h>
40#if defined (HAVE_SYSINFO) && defined (HAVE_SYS_SYSTEMINFO_H)
41# include <sys/systeminfo.h>
42#endif
43#include "busybox.h" 43#include "busybox.h"
44 44
45static void print_element(unsigned int mask, char *element); 45typedef struct {
46 46 struct utsname name;
47/* Values that are bitwise or'd into `toprint'. */ 47 char processor[8]; /* for "unknown" */
48/* Operating system name. */ 48} uname_info_t;
49static const int PRINT_SYSNAME = 1; 49
50 50static const char options[] = "snrvmpa";
51/* Node name on a communications network. */ 51static const char flags[] = "\x01\x02\x04\x08\x10\x20\x3f";
52static const int PRINT_NODENAME = 2; 52static const unsigned short int utsname_offset[] = {
53 53 offsetof(uname_info_t,name.sysname),
54/* Operating system release. */ 54 offsetof(uname_info_t,name.nodename),
55static const int PRINT_RELEASE = 4; 55 offsetof(uname_info_t,name.release),
56 56 offsetof(uname_info_t,name.version),
57/* Operating system version. */ 57 offsetof(uname_info_t,name.machine),
58static const int PRINT_VERSION = 8; 58 offsetof(uname_info_t,processor)
59 59};
60/* Machine hardware name. */
61static const int PRINT_MACHINE = 16;
62
63 /* Host processor type. */
64static const int PRINT_PROCESSOR = 32;
65
66/* Mask indicating which elements of the name to print. */
67static unsigned char toprint;
68
69 60
70int uname_main(int argc, char **argv) 61int uname_main(int argc, char **argv)
71{ 62{
72 struct utsname name; 63 uname_info_t uname_info;
73 char processor[256];
74 64
75#if defined(__sparc__) && defined(__linux__) 65#if defined(__sparc__) && defined(__linux__)
76 char *fake_sparc = getenv("FAKE_SPARC"); 66 char *fake_sparc = getenv("FAKE_SPARC");
77#endif 67#endif
78 68
79 toprint = 0; 69 const unsigned short int *delta;
80 70 int opt;
81 /* Parse any options */ 71 char toprint = 0;
82 //fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv); 72
83 while (--argc > 0 && **(++argv) == '-') { 73 while ((opt = getopt(argc, argv, options)) != -1) {
84 while (*(++(*argv))) { 74 const char *p = strchr(options,opt);
85 switch (**argv) { 75 if (p == NULL) {
86 case 's': 76 show_usage();
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:
110 show_usage();
111 }
112 } 77 }
78 toprint |= flags[(int)(p-options)];
113 } 79 }
114 80
115 if (toprint == 0) 81 if (toprint == 0) {
116 toprint = PRINT_SYSNAME; 82 toprint = flags[0]; /* sysname */
117 83 }
118 if (uname(&name) == -1)
119 perror_msg("cannot get system name");
120
121#if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE)
122 if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1)
123 perror_msg("cannot get processor type");
124}
125 84
126#else 85 if (uname(&uname_info.name) == -1) {
127 strcpy(processor, "unknown"); 86 error_msg_and_die("cannot get system name");
128#endif 87 }
129 88
130#if defined(__sparc__) && defined(__linux__) 89#if defined(__sparc__) && defined(__linux__)
131 if (fake_sparc != NULL 90 if ((fake_sparc != NULL)
132 && (fake_sparc[0] == 'y' 91 && ((fake_sparc[0] == 'y')
133 || fake_sparc[0] == 'Y')) strcpy(name.machine, "sparc"); 92 || (fake_sparc[0] == 'Y'))) {
93 strcpy(uname_info.name.machine, "sparc");
94 }
134#endif 95#endif
135 96
136 print_element(PRINT_SYSNAME, name.sysname); 97 strcpy(uname_info.processor, "unknown");
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);
142 98
143 return EXIT_SUCCESS; 99 for (delta=utsname_offset ; toprint ; delta++, toprint >>= 1) {
144} 100 if (toprint & 1) {
101 printf("%s ", ((char *)(&uname_info)) + *delta );
102 }
103 }
145 104
146/* If the name element set in MASK is selected for printing in `toprint', 105 putchar('\n');
147 print ELEMENT; then print a space unless it is the last element to
148 be printed, in which case print a newline. */
149 106
150static void print_element(unsigned int mask, char *element) 107 return EXIT_SUCCESS;
151{
152 if (toprint & mask) {
153 toprint &= ~mask;
154 printf("%s%c", element, toprint ? ' ' : '\n');
155 }
156} 108}