uname.c 6.6 KB

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