uname.c 5.3 KB

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