uname.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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: "[-amnrspv]"
  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 OS release"
  55. //usage: "\n -s OS name (default)"
  56. //usage: "\n -p Processor type"
  57. //usage: "\n -v OS version"
  58. //usage:
  59. //usage:#define uname_example_usage
  60. //usage: "$ uname -a\n"
  61. //usage: "Linux debian 2.4.23 #2 Tue Dec 23 17:09:10 MST 2003 i686 GNU/Linux\n"
  62. #include "libbb.h"
  63. /* After libbb.h, since it needs sys/types.h on some systems */
  64. #include <sys/utsname.h>
  65. typedef struct {
  66. struct utsname name;
  67. char processor[sizeof(((struct utsname*)NULL)->machine)];
  68. char platform[sizeof(((struct utsname*)NULL)->machine)];
  69. char os[sizeof("GNU/Linux")];
  70. } uname_info_t;
  71. static const char options[] ALIGN1 = "snrvmpioa";
  72. static const unsigned short utsname_offset[] = {
  73. offsetof(uname_info_t, name.sysname), /* -s */
  74. offsetof(uname_info_t, name.nodename), /* -n */
  75. offsetof(uname_info_t, name.release), /* -r */
  76. offsetof(uname_info_t, name.version), /* -v */
  77. offsetof(uname_info_t, name.machine), /* -m */
  78. offsetof(uname_info_t, processor), /* -p */
  79. offsetof(uname_info_t, platform), /* -i */
  80. offsetof(uname_info_t, os), /* -o */
  81. };
  82. int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  83. int uname_main(int argc UNUSED_PARAM, char **argv)
  84. {
  85. #if ENABLE_LONG_OPTS
  86. static const char uname_longopts[] ALIGN1 =
  87. /* name, has_arg, val */
  88. "all\0" No_argument "a"
  89. "kernel-name\0" No_argument "s"
  90. "nodename\0" No_argument "n"
  91. "kernel-release\0" No_argument "r"
  92. "release\0" No_argument "r"
  93. "kernel-version\0" No_argument "v"
  94. "machine\0" No_argument "m"
  95. "processor\0" No_argument "p"
  96. "hardware-platform\0" No_argument "i"
  97. "operating-system\0" No_argument "o"
  98. ;
  99. #endif
  100. uname_info_t uname_info;
  101. #if defined(__sparc__) && defined(__linux__)
  102. char *fake_sparc = getenv("FAKE_SPARC");
  103. #endif
  104. const char *unknown_str = "unknown";
  105. const char *fmt;
  106. const unsigned short *delta;
  107. unsigned toprint;
  108. IF_LONG_OPTS(applet_long_options = uname_longopts);
  109. toprint = getopt32(argv, options);
  110. if (argv[optind]) { /* coreutils-6.9 compat */
  111. bb_show_usage();
  112. }
  113. if (toprint & (1 << 8)) { /* -a => all opts on */
  114. toprint = (1 << 8) - 1;
  115. unknown_str = ""; /* -a does not print unknown fields */
  116. }
  117. if (toprint == 0) { /* no opts => -s (sysname) */
  118. toprint = 1;
  119. }
  120. uname(&uname_info.name); /* never fails */
  121. #if defined(__sparc__) && defined(__linux__)
  122. if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
  123. strcpy(uname_info.name.machine, "sparc");
  124. }
  125. #endif
  126. strcpy(uname_info.processor, unknown_str);
  127. strcpy(uname_info.platform, unknown_str);
  128. strcpy(uname_info.os, "GNU/Linux");
  129. #if 0
  130. /* Fedora does something like this */
  131. strcpy(uname_info.processor, uname_info.name.machine);
  132. strcpy(uname_info.platform, uname_info.name.machine);
  133. if (uname_info.platform[0] == 'i'
  134. && uname_info.platform[1]
  135. && uname_info.platform[2] == '8'
  136. && uname_info.platform[3] == '6'
  137. ) {
  138. uname_info.platform[1] = '3';
  139. }
  140. #endif
  141. delta = utsname_offset;
  142. fmt = " %s" + 1;
  143. do {
  144. if (toprint & 1) {
  145. const char *p = (char *)(&uname_info) + *delta;
  146. if (p[0]) {
  147. printf(fmt, p);
  148. fmt = " %s";
  149. }
  150. }
  151. ++delta;
  152. } while (toprint >>= 1);
  153. bb_putchar('\n');
  154. fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
  155. }