xref: /third_party/toybox/toys/posix/uname.c (revision 0f66f451)
1/* uname.c - return system name
2 *
3 * Copyright 2008 Rob Landley <rob@landley.net>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/uname.html
6
7USE_UNAME(NEWTOY(uname, "oamvrns[+os]", TOYFLAG_BIN))
8USE_ARCH(NEWTOY(arch, 0, TOYFLAG_USR|TOYFLAG_BIN))
9
10config ARCH
11  bool "arch"
12  default y
13  help
14    usage: arch
15
16    Print machine (hardware) name, same as uname -m.
17
18config UNAME
19  bool "uname"
20  default y
21  help
22    usage: uname [-asnrvm]
23
24    Print system information.
25
26    -s	System name
27    -n	Network (domain) name
28    -r	Kernel Release number
29    -v	Kernel Version
30    -m	Machine (hardware) name
31    -a	All of the above
32*/
33
34#define FOR_uname
35#define FORCE_FLAGS
36#include "toys.h"
37
38// If a 32 bit x86 build environment working in a chroot under an x86-64
39// kernel returns x86_64 for -m it confuses ./configure.  Special case it.
40
41#if defined(__i686__)
42#define GROSS "i686"
43#elif defined(__i586__)
44#define GROSS "i586"
45#elif defined(__i486__)
46#define GROSS "i486"
47#elif defined(__i386__)
48#define GROSS "i386"
49#endif
50
51void uname_main(void)
52{
53  int i, flags = toys.optflags, needspace=0;
54  struct utsname u;
55
56  uname(&u);
57
58  if (!flags) flags = FLAG_s;
59  for (i=0; i<5; i++) {
60    char *c = ((char *) &u)+(sizeof(u.sysname)*i);
61
62    if (flags & ((1<<i)|FLAG_a)) {
63      int len = strlen(c);
64
65      // This problem originates in autoconf, so of course the solution
66      // is horribly ugly.
67#ifdef GROSS
68      if (i==4 && !strcmp(c,"x86_64")) {
69        printf(GROSS);
70        continue;
71      }
72#endif
73
74      if (needspace++) {
75        // We can't decrement on the first entry, because
76        // needspace would be 0
77        *(--c)=' ';
78        len++;
79      }
80      xwrite(1, c, len);
81    }
82  }
83  putchar('\n');
84}
85
86void arch_main(void)
87{
88  toys.optflags = FLAG_m;
89  uname_main();
90}
91