uname.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. The default behavior is equivalent to '-s'.
  17. David MacKenzie <djm@gnu.ai.mit.edu> */
  18. /* Busyboxed by Erik Andersen */
  19. /* Further size reductions by Glenn McGrath and Manuel Novoa III. */
  20. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  21. *
  22. * Now does proper error checking on i/o. Plus some further space savings.
  23. */
  24. #include <sys/utsname.h>
  25. #include "libbb.h"
  26. typedef struct {
  27. struct utsname name;
  28. char processor[8]; /* for "unknown" */
  29. } uname_info_t;
  30. static const char options[] ALIGN1 = "snrvmpa";
  31. static const unsigned short utsname_offset[] = {
  32. offsetof(uname_info_t, name.sysname),
  33. offsetof(uname_info_t, name.nodename),
  34. offsetof(uname_info_t, name.release),
  35. offsetof(uname_info_t, name.version),
  36. offsetof(uname_info_t, name.machine),
  37. offsetof(uname_info_t, processor)
  38. };
  39. int uname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  40. int uname_main(int argc UNUSED_PARAM, char **argv)
  41. {
  42. uname_info_t uname_info;
  43. #if defined(__sparc__) && defined(__linux__)
  44. char *fake_sparc = getenv("FAKE_SPARC");
  45. #endif
  46. const unsigned short *delta;
  47. char toprint;
  48. toprint = getopt32(argv, options);
  49. if (argv[optind]) { /* coreutils-6.9 compat */
  50. bb_show_usage();
  51. }
  52. if (toprint & (1 << 6)) { /* -a => all opts on */
  53. toprint = 0x3f;
  54. }
  55. if (toprint == 0) { /* no opts => -s (sysname) */
  56. toprint = 1;
  57. }
  58. uname(&uname_info.name); /* never fails */
  59. #if defined(__sparc__) && defined(__linux__)
  60. if (fake_sparc && (fake_sparc[0] | 0x20) == 'y') {
  61. strcpy(uname_info.name.machine, "sparc");
  62. }
  63. #endif
  64. strcpy(uname_info.processor, "unknown");
  65. delta = utsname_offset;
  66. do {
  67. if (toprint & 1) {
  68. /* printf would not be safe here */
  69. fputs((char *)(&uname_info) + *delta, stdout);
  70. if (toprint > 1) {
  71. bb_putchar(' ');
  72. }
  73. }
  74. ++delta;
  75. } while (toprint >>= 1);
  76. bb_putchar('\n');
  77. fflush_stdout_and_exit(EXIT_SUCCESS); /* coreutils-6.9 compat */
  78. }