blob: 5a3eafe9289fbfd8124fe4c7a52bdf884fc3b349 [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
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00003 * Copyright (C) 1989-1999 Free Software Foundation, Inc.
4 *
5 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
6 */
Eric Andersenc8fdb561999-10-26 05:21:02 +00007
Manuel Novoa III cad53642003-03-19 09:13:01 +00008/* BB_AUDIT SUSv3 compliant */
9/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
10
Eric Andersenc8fdb561999-10-26 05:21:02 +000011/* 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
Manuel Novoa III 6509f922001-12-05 04:21:30 +000026/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
27
Manuel Novoa III cad53642003-03-19 09:13:01 +000028/* 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
Eric Andersenc8fdb561999-10-26 05:21:02 +000033#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000034#include <stdlib.h>
Manuel Novoa III 6509f922001-12-05 04:21:30 +000035#include <stddef.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000036#include <string.h>
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#include <unistd.h>
Eric Andersenc8fdb561999-10-26 05:21:02 +000038#include <sys/types.h>
39#include <sys/utsname.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000040#include "busybox.h"
Eric Andersenc8fdb561999-10-26 05:21:02 +000041
Manuel Novoa III 6509f922001-12-05 04:21:30 +000042typedef struct {
43 struct utsname name;
44 char processor[8]; /* for "unknown" */
45} uname_info_t;
Eric Andersenc8fdb561999-10-26 05:21:02 +000046
Manuel Novoa III 6509f922001-12-05 04:21:30 +000047static const char options[] = "snrvmpa";
Manuel Novoa III 6509f922001-12-05 04:21:30 +000048static 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};
Eric Andersenc8fdb561999-10-26 05:21:02 +000056
57int uname_main(int argc, char **argv)
58{
Manuel Novoa III 6509f922001-12-05 04:21:30 +000059 uname_info_t uname_info;
Eric Andersenc8fdb561999-10-26 05:21:02 +000060#if defined(__sparc__) && defined(__linux__)
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 char *fake_sparc = getenv("FAKE_SPARC");
Eric Andersenc8fdb561999-10-26 05:21:02 +000062#endif
Manuel Novoa III 6509f922001-12-05 04:21:30 +000063 const unsigned short int *delta;
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 char toprint;
Eric Andersenc8fdb561999-10-26 05:21:02 +000065
Denis Vlasenko67b23e62006-10-03 21:00:06 +000066 toprint = getopt32(argc, argv, options);
Manuel Novoa III cad53642003-03-19 09:13:01 +000067
68 if (argc != optind) {
69 bb_show_usage();
70 }
71
72 if (toprint & (1 << 6)) {
73 toprint = 0x3f;
Manuel Novoa III 6509f922001-12-05 04:21:30 +000074 }
75
76 if (toprint == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 toprint = 1; /* sysname */
Manuel Novoa III 6509f922001-12-05 04:21:30 +000078 }
79
80 if (uname(&uname_info.name) == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 bb_error_msg_and_die("cannot get system name");
Manuel Novoa III 6509f922001-12-05 04:21:30 +000082 }
83
84#if defined(__sparc__) && defined(__linux__)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000085 if ((fake_sparc != NULL)
Manuel Novoa III 6509f922001-12-05 04:21:30 +000086 && ((fake_sparc[0] == 'y')
87 || (fake_sparc[0] == 'Y'))) {
88 strcpy(uname_info.name.machine, "sparc");
89 }
90#endif
91
92 strcpy(uname_info.processor, "unknown");
93
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000094 delta = utsname_offset;
Manuel Novoa III fa15f702002-03-25 02:37:20 +000095 do {
Manuel Novoa III 6509f922001-12-05 04:21:30 +000096 if (toprint & 1) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000097 printf(((char *)(&uname_info)) + *delta);
Manuel Novoa III fa15f702002-03-25 02:37:20 +000098 if (toprint > 1) {
99 putchar(' ');
100 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000101 }
Manuel Novoa III fa15f702002-03-25 02:37:20 +0000102 ++delta;
103 } while (toprint >>= 1);
Manuel Novoa III 6509f922001-12-05 04:21:30 +0000104 putchar('\n');
Eric Andersenc8fdb561999-10-26 05:21:02 +0000105
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000106 fflush_stdout_and_exit(EXIT_SUCCESS);
Eric Andersenc8fdb561999-10-26 05:21:02 +0000107}