uname.c 4.6 KB

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