uname.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <stdio.h>
  25. #include <stdlib.h>
  26. #include <stddef.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <sys/types.h>
  30. #include <sys/utsname.h>
  31. #include "busybox.h"
  32. typedef struct {
  33. struct utsname name;
  34. char processor[8]; /* for "unknown" */
  35. } uname_info_t;
  36. static const char options[] = "snrvmpa";
  37. static const unsigned short int utsname_offset[] = {
  38. offsetof(uname_info_t,name.sysname),
  39. offsetof(uname_info_t,name.nodename),
  40. offsetof(uname_info_t,name.release),
  41. offsetof(uname_info_t,name.version),
  42. offsetof(uname_info_t,name.machine),
  43. offsetof(uname_info_t,processor)
  44. };
  45. int uname_main(int argc, char **argv)
  46. {
  47. uname_info_t uname_info;
  48. #if defined(__sparc__) && defined(__linux__)
  49. char *fake_sparc = getenv("FAKE_SPARC");
  50. #endif
  51. const unsigned short int *delta;
  52. char toprint;
  53. toprint = getopt32(argc, argv, options);
  54. if (argc != optind) {
  55. bb_show_usage();
  56. }
  57. if (toprint & (1 << 6)) {
  58. toprint = 0x3f;
  59. }
  60. if (toprint == 0) {
  61. toprint = 1; /* sysname */
  62. }
  63. if (uname(&uname_info.name) == -1) {
  64. bb_error_msg_and_die("cannot get system name");
  65. }
  66. #if defined(__sparc__) && defined(__linux__)
  67. if ((fake_sparc != NULL)
  68. && ((fake_sparc[0] == 'y')
  69. || (fake_sparc[0] == 'Y'))) {
  70. strcpy(uname_info.name.machine, "sparc");
  71. }
  72. #endif
  73. strcpy(uname_info.processor, "unknown");
  74. delta = utsname_offset;
  75. do {
  76. if (toprint & 1) {
  77. printf(((char *)(&uname_info)) + *delta);
  78. if (toprint > 1) {
  79. putchar(' ');
  80. }
  81. }
  82. ++delta;
  83. } while (toprint >>= 1);
  84. putchar('\n');
  85. fflush_stdout_and_exit(EXIT_SUCCESS);
  86. }