just whitespace
[platform/upstream/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 /* BB_AUDIT SUSv3 compliant */
20 /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
21
22 /* Option               Example
23
24    -s, --sysname        SunOS
25    -n, --nodename       rocky8
26    -r, --release        4.0
27    -v, --version
28    -m, --machine        sun
29    -a, --all            SunOS rocky8 4.0  sun
30
31    The default behavior is equivalent to `-s'.
32
33    David MacKenzie <djm@gnu.ai.mit.edu> */
34
35 /* Busyboxed by Erik Andersen */
36
37 /* Further size reductions by Glenn McGrath and Manuel Novoa III. */
38
39 /* Mar 16, 2003      Manuel Novoa III   (mjn3@codepoet.org)
40  *
41  * Now does proper error checking on i/o.  Plus some further space savings.
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <stddef.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/types.h>
50 #include <sys/utsname.h>
51 #include "busybox.h"
52
53 typedef struct {
54         struct utsname name;
55         char processor[8];                      /* for "unknown" */
56 } uname_info_t;
57
58 static const char options[] = "snrvmpa";
59 static const unsigned short int utsname_offset[] = {
60         offsetof(uname_info_t,name.sysname),
61         offsetof(uname_info_t,name.nodename),
62         offsetof(uname_info_t,name.release),
63         offsetof(uname_info_t,name.version),
64         offsetof(uname_info_t,name.machine),
65         offsetof(uname_info_t,processor)
66 };
67
68 int uname_main(int argc, char **argv)
69 {
70         uname_info_t uname_info;
71 #if defined(__sparc__) && defined(__linux__)
72         char *fake_sparc = getenv("FAKE_SPARC");
73 #endif
74         const unsigned short int *delta;
75         char toprint;
76
77         toprint = bb_getopt_ulflags(argc, argv, options);
78
79         if (argc != optind) {
80                 bb_show_usage();
81         }
82
83         if (toprint & (1 << 6)) {
84                 toprint = 0x3f;
85         }
86
87         if (toprint == 0) {
88                 toprint = 1;                    /* sysname */
89         }
90
91         if (uname(&uname_info.name) == -1) {
92                 bb_error_msg_and_die("cannot get system name");
93         }
94
95 #if defined(__sparc__) && defined(__linux__)
96         if ((fake_sparc != NULL)
97                 && ((fake_sparc[0] == 'y')
98                         || (fake_sparc[0] == 'Y'))) {
99                 strcpy(uname_info.name.machine, "sparc");
100         }
101 #endif
102
103         strcpy(uname_info.processor, "unknown");
104
105         delta=utsname_offset;
106         do {
107                 if (toprint & 1) {
108                         bb_printf(((char *)(&uname_info)) + *delta);
109                         if (toprint > 1) {
110                                 putchar(' ');
111                         }
112                 }
113                 ++delta;
114         } while (toprint >>= 1);
115         putchar('\n');
116
117         bb_fflush_stdout_and_exit(EXIT_SUCCESS);
118 }