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