uname.c 4.6 KB

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