Combined size reduction changes by Glenn and myself. Added back in "unknown"
[oweals/busybox.git] / coreutils / uname.c
1 /* vi: set sw=4 ts=4: */
2 /* 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 /* Further size reductions by Glenn McGrath and Manuel Novoa III. */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stddef.h>
39 #include <string.h>
40 #include <sys/types.h>
41 #include <sys/utsname.h>
42 #include <getopt.h>
43 #include "busybox.h"
44
45 typedef struct {
46         struct utsname name;
47         char processor[8];                      /* for "unknown" */
48 } uname_info_t;
49
50 static const char options[] = "snrvmpa";
51 static const char flags[] = "\x01\x02\x04\x08\x10\x20\x3f";
52 static const unsigned short int utsname_offset[] = {
53         offsetof(uname_info_t,name.sysname),
54         offsetof(uname_info_t,name.nodename),
55         offsetof(uname_info_t,name.release),
56         offsetof(uname_info_t,name.version),
57         offsetof(uname_info_t,name.machine),
58         offsetof(uname_info_t,processor)
59 };
60
61 int uname_main(int argc, char **argv)
62 {
63         uname_info_t uname_info;
64
65 #if defined(__sparc__) && defined(__linux__)
66         char *fake_sparc = getenv("FAKE_SPARC");
67 #endif
68
69         const unsigned short int *delta;
70         int opt;
71         char toprint = 0;
72
73         while ((opt = getopt(argc, argv, options)) != -1) {
74                 const char *p = strchr(options,opt);
75                 if (p == NULL) {
76                         show_usage();
77                 }
78                 toprint |= flags[(int)(p-options)];
79         }
80
81         if (toprint == 0) {
82                 toprint = flags[0];             /* sysname */
83         }
84
85         if (uname(&uname_info.name) == -1) {
86                 error_msg_and_die("cannot get system name");
87         }
88
89 #if defined(__sparc__) && defined(__linux__)
90         if ((fake_sparc != NULL)
91                 && ((fake_sparc[0] == 'y')
92                         || (fake_sparc[0] == 'Y'))) {
93                 strcpy(uname_info.name.machine, "sparc");
94         }
95 #endif
96
97         strcpy(uname_info.processor, "unknown");
98
99         for (delta=utsname_offset ; toprint ; delta++, toprint >>= 1) {
100                 if (toprint & 1) {
101                         printf("%s ", ((char *)(&uname_info)) + *delta );
102                 }
103         }
104
105         putchar('\n');
106
107         return EXIT_SUCCESS;
108 }