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