blob: df4f14ea0577f41b929c539e798e57af05d92518 [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
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
Manuel Novoa III 6509f922001-12-05 04:21:30 +000034/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
35
Eric Andersenc8fdb561999-10-26 05:21:02 +000036#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000037#include <stdlib.h>
Manuel Novoa III 6509f922001-12-05 04:21:30 +000038#include <stddef.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000039#include <string.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000040#include <sys/types.h>
41#include <sys/utsname.h>
Manuel Novoa III 6509f922001-12-05 04:21:30 +000042#include <getopt.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000043#include "busybox.h"
Eric Andersenc8fdb561999-10-26 05:21:02 +000044
Manuel Novoa III 6509f922001-12-05 04:21:30 +000045typedef struct {
46 struct utsname name;
47 char processor[8]; /* for "unknown" */
48} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000049
Manuel Novoa III 6509f922001-12-05 04:21:30 +000050static const char options[] = "snrvmpa";
51static const char flags[] = "\x01\x02\x04\x08\x10\x20\x3f";
52static 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};
Eric Andersenc8fdb561999-10-26 05:21:02 +000060
61int uname_main(int argc, char **argv)
62{
Manuel Novoa III 6509f922001-12-05 04:21:30 +000063 uname_info_t uname_info;
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Eric Andersenc8fdb561999-10-26 05:21:02 +000065#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000067#endif
68
Manuel Novoa III 6509f922001-12-05 04:21:30 +000069 const unsigned short int *delta;
70 int opt;
71 char toprint = 0;
Eric Andersenc8fdb561999-10-26 05:21:02 +000072
Manuel Novoa III 6509f922001-12-05 04:21:30 +000073 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__)
Manuel Novoa III fa15f702002-03-25 02:37:20 +000090 if ((fake_sparc != NULL)
Manuel Novoa III 6509f922001-12-05 04:21:30 +000091 && ((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
Manuel Novoa III fa15f702002-03-25 02:37:20 +000099 delta=utsname_offset;
100 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000101 if (toprint & 1) {
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000102 printf(((char *)(&uname_info)) + *delta);
103 if (toprint > 1) {
104 putchar(' ');
105 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000106 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000107 ++delta;
108 } while (toprint >>= 1);
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000109 putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000110
Matt Kraai3e856ce2000-12-01 02:55:13 +0000111 return EXIT_SUCCESS;
Eric Andersenc8fdb561999-10-26 05:21:02 +0000112}