blob: a3e52e39fdfb7b7cdff3a9836e4b6c5bdb9eccff [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc8fdb561999-10-26 05:21:02 +00002/* 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
Manuel Novoa III cad53642003-03-19 09:13:01 +000019/* BB_AUDIT SUSv3 compliant */
20/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
21
Eric Andersenc8fdb561999-10-26 05:21:02 +000022/* 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
Manuel Novoa III 6509f922001-12-05 04:21:30 +000037/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
38
Manuel Novoa III cad53642003-03-19 09:13:01 +000039/* 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
Eric Andersenc8fdb561999-10-26 05:21:02 +000044#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000045#include <stdlib.h>
Manuel Novoa III 6509f922001-12-05 04:21:30 +000046#include <stddef.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000047#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000048#include <unistd.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000049#include <sys/types.h>
50#include <sys/utsname.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000051#include "busybox.h"
Eric Andersenc8fdb561999-10-26 05:21:02 +000052
Manuel Novoa III 6509f922001-12-05 04:21:30 +000053typedef struct {
54 struct utsname name;
55 char processor[8]; /* for "unknown" */
56} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000057
Manuel Novoa III 6509f922001-12-05 04:21:30 +000058static const char options[] = "snrvmpa";
Manuel Novoa III 6509f922001-12-05 04:21:30 +000059static 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};
Eric Andersenc8fdb561999-10-26 05:21:02 +000067
68int uname_main(int argc, char **argv)
69{
Manuel Novoa III 6509f922001-12-05 04:21:30 +000070 uname_info_t uname_info;
Eric Andersenc8fdb561999-10-26 05:21:02 +000071#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000073#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +000074 const unsigned short int *delta;
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 char toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +000076
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 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;
Manuel Novoa III 6509f922001-12-05 04:21:30 +000085 }
86
87 if (toprint == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 toprint = 1; /* sysname */
Manuel Novoa III 6509f922001-12-05 04:21:30 +000089 }
90
91 if (uname(&uname_info.name) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_error_msg_and_die("cannot get system name");
Manuel Novoa III 6509f922001-12-05 04:21:30 +000093 }
94
95#if defined(__sparc__) && defined(__linux__)
Manuel Novoa III fa15f702002-03-25 02:37:20 +000096 if ((fake_sparc != NULL)
Manuel Novoa III 6509f922001-12-05 04:21:30 +000097 && ((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
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000105 delta=utsname_offset;
106 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000107 if (toprint & 1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 bb_printf(((char *)(&uname_info)) + *delta);
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000109 if (toprint > 1) {
110 putchar(' ');
111 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000112 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000113 ++delta;
114 } while (toprint >>= 1);
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000115 putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000116
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000118}