uname.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 source tree.
  6. */
  7. /* Option Example
  8. * -s, --sysname SunOS
  9. * -n, --nodename rocky8
  10. * -r, --release 4.0
  11. * -v, --version
  12. * -m, --machine sun
  13. * -a, --all SunOS rocky8 4.0 sun
  14. *
  15. * The default behavior is equivalent to '-s'.
  16. *
  17. * David MacKenzie <djm@gnu.ai.mit.edu>
  18. *
  19. * GNU coreutils 6.10:
  20. * Option: struct Example(s):
  21. * utsname
  22. * field:
  23. * -s, --kernel-name sysname Linux
  24. * -n, --nodename nodename localhost.localdomain
  25. * -r, --kernel-release release 2.6.29
  26. * -v, --kernel-version version #1 SMP Sun Jan 11 20:52:37 EST 2009
  27. * -m, --machine machine x86_64 i686
  28. * -p, --processor (none) x86_64 i686
  29. * -i, --hardware-platform (none) x86_64 i386
  30. * NB: vanilla coreutils reports "unknown" -p and -i,
  31. * x86_64 and i686/i386 shown above are Fedora's inventions.
  32. * -o, --operating-system (none) GNU/Linux
  33. * -a, --all: all of the above, in the order shown.
  34. * If -p or -i is not known, don't show them
  35. */
  36. /* Busyboxed by Erik Andersen
  37. *
  38. * Before 2003: Glenn McGrath and Manuel Novoa III
  39. * Further size reductions.
  40. * Mar 16, 2003: Manuel Novoa III (mjn3@codepoet.org)
  41. * Now does proper error checking on i/o. Plus some further space savings.
  42. * Jan 2009:
  43. * Fix handling of -a to not print "unknown", add -o and -i support.
  44. */
  45. //config:config UNAME
  46. //config: bool "uname"
  47. //config: default y
  48. //config: help
  49. //config: uname is used to print system information.
  50. //config:
  51. //config:config UNAME_OSNAME
  52. //config: string "Operating system name"
  53. //config: default "GNU/Linux"
  54. //config: depends on UNAME
  55. //config: help
  56. //config: Sets the operating system name reported by uname -o. The
  57. //config: default is "GNU/Linux".
  58. //applet:IF_UNAME(APPLET(uname, BB_DIR_BIN, BB_SUID_DROP))
  59. //kbuild:lib-$(CONFIG_UNAME) += uname.o
  60. /* BB_AUDIT SUSv3 compliant */
  61. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
  62. //usage:#define uname_trivial_usage
  63. //usage: "[-amnrspvio]"
  64. //usage:#define uname_full_usage "\n\n"
  65. //usage: "Print system information\n"
  66. //usage: "\n -a Print all"
  67. //usage: "\n -m The machine (hardware) type"
  68. //usage: "\n -n Hostname"
  69. //usage: "\n -r Kernel release"
  70. //usage: "\n -s Kernel name (default)"
  71. //usage: "\n -p Processor type"
  72. //usage: "\n -v Kernel version"
  73. //usage: "\n -i The hardware platform"
  74. //usage: "\n -o OS name"
  75. //usage:
  76. //usage:#define uname_example_usage
  77. //usage: "$ uname -a\n"
  78. //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
  79. #include "libbb.h"
  80. /* After libbb.h, since it needs sys/types.h on some systems */
  81. #include <sys/utsname.h>
  82. typedef struct {
  83. struct utsname name;
  84. char processor[sizeof(((struct utsname*)NULL)->machine)];
  85. char platform[sizeof(((struct utsname*)NULL)->machine)];
  86. char os[sizeof(CONFIG_UNAME_OSNAME)];
  87. } uname_info_t;
  88. static const char options[] ALIGN1 = "snrvmpioa";
  89. static const unsigned short utsname_offset[] = {
  90. offsetof(uname_info_t, name.sysname), /* -s */
  91. offsetof(uname_info_t, name.nodename), /* -n */
  92. offsetof(uname_info_t, name.release), /* -r */
  93. offsetof(uname_info_t, name.version), /* -v */
  94. offsetof(uname_info_t, name.machine), /* -m */
  95. offsetof(uname_info_t, processor), /* -p */
  96. offsetof(uname_info_t, platform), /* -i */
  97. offsetof(uname_info_t, os), /* -o */
  98. };
  99. int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  100. int uname_main(int argc UNUSED_PARAM, char **argv)
  101. {
  102. #if ENABLE_LONG_OPTS
  103. static const char uname_longopts[] ALIGN1 =
  104. /* name, has_arg, val */
  105. "all\0" No_argument "a"
  106. "kernel-name\0" No_argument "s"
  107. "nodename\0" No_argument "n"
  108. "kernel-release\0" No_argument "r"
  109. "release\0" No_argument "r"
  110. "kernel-version\0" No_argument "v"
  111. "machine\0" No_argument "m"
  112. "processor\0" No_argument "p"
  113. "hardware-platform\0" No_argument "i"
  114. "operating-system\0" No_argument "o"
  115. ;
  116. #endif
  117. uname_info_t uname_info;
  118. #if defined(__sparc__) && defined(__linux__)
  119. char *fake_sparc = getenv("FAKE_SPARC");
  120. #endif
  121. const char *unknown_str = "unknown";
  122. const char *fmt;
  123. const unsigned short *delta;
  124. unsigned toprint;
  125. IF_LONG_OPTS(applet_long_options = uname_longopts);
  126. toprint = getopt32(argv, options);
  127. if (argv[optind]) { /* coreutils-6.9 compat */
  128. bb_show_usage();
  129. }
  130. if (toprint & (1 << 8)) { /* -a => all opts on */
  131. toprint = (1 << 8) - 1;
  132. unknown_str = ""; /* -a does not print unknown fields */
  133. }
  134. if (toprint == 0) { /* no opts => -s (sysname) */
  135. toprint = 1;
  136. }
  137. uname(&uname_info.name); /* never fails */
  138. #if defined(__sparc__) && defined(__linux__)
  139. if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
  140. strcpy(uname_info.name.machine, "sparc");
  141. }
  142. #endif
  143. strcpy(uname_info.processor, unknown_str);
  144. strcpy(uname_info.platform, unknown_str);
  145. strcpy(uname_info.os, CONFIG_UNAME_OSNAME);
  146. #if 0
  147. /* Fedora does something like this */
  148. strcpy(uname_info.processor, uname_info.name.machine);
  149. strcpy(uname_info.platform, uname_info.name.machine);
  150. if (uname_info.platform[0] == 'i'
  151. && uname_info.platform[1]
  152. && uname_info.platform[2] == '8'
  153. && uname_info.platform[3] == '6'
  154. ) {
  155. uname_info.platform[1] = '3';
  156. }
  157. #endif
  158. delta = utsname_offset;
  159. fmt = " %s" + 1;
  160. do {
  161. if (toprint & 1) {
  162. const char *p = (char *)(&uname_info) + *delta;
  163. if (p[0]) {
  164. printf(fmt, p);
  165. fmt = " %s";
  166. }
  167. }
  168. ++delta;
  169. } while (toprint >>= 1);
  170. bb_putchar('\n');
  171. fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
  172. }