uname.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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[] ALIGN2 = {
  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);
  40. int uname_main(int argc, 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 int *delta;
  47. char toprint;
  48. toprint = getopt32(argv, options);
  49. if (argc != optind) {
  50. bb_show_usage();
  51. }
  52. if (toprint & (1 << 6)) {
  53. toprint = 0x3f;
  54. }
  55. if (toprint == 0) {
  56. toprint = 1; /* sysname */
  57. }
  58. if (uname(&uname_info.name) == -1) {
  59. bb_error_msg_and_die("cannot get system name");
  60. }
  61. #if defined(__sparc__) && defined(__linux__)
  62. if ((fake_sparc != NULL)
  63. && ((fake_sparc[0] == 'y')
  64. || (fake_sparc[0] == 'Y'))) {
  65. strcpy(uname_info.name.machine, "sparc");
  66. }
  67. #endif
  68. strcpy(uname_info.processor, "unknown");
  69. delta = utsname_offset;
  70. do {
  71. if (toprint & 1) {
  72. printf(((char *)(&uname_info)) + *delta);
  73. if (toprint > 1) {
  74. putchar(' ');
  75. }
  76. }
  77. ++delta;
  78. } while (toprint >>= 1);
  79. putchar('\n');
  80. fflush_stdout_and_exit(EXIT_SUCCESS);
  81. }