uname.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* vi: set sw=4 ts=4: */
  2. /* uname -- print system information
  3. Copyright (C) 1989-1999 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* BB_AUDIT SUSv3 compliant */
  16. /* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
  17. /* Option Example
  18. -s, --sysname SunOS
  19. -n, --nodename rocky8
  20. -r, --release 4.0
  21. -v, --version
  22. -m, --machine sun
  23. -a, --all SunOS rocky8 4.0 sun
  24. The default behavior is equivalent to `-s'.
  25. David MacKenzie <djm@gnu.ai.mit.edu> */
  26. /* Busyboxed by Erik Andersen */
  27. /* Further size reductions by Glenn McGrath and Manuel Novoa III. */
  28. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  29. *
  30. * Now does proper error checking on i/o. Plus some further space savings.
  31. */
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <stddef.h>
  35. #include <string.h>
  36. #include <unistd.h>
  37. #include <sys/types.h>
  38. #include <sys/utsname.h>
  39. #include "busybox.h"
  40. typedef struct {
  41. struct utsname name;
  42. char processor[8]; /* for "unknown" */
  43. } uname_info_t;
  44. static const char options[] = "snrvmpa";
  45. static const unsigned short int utsname_offset[] = {
  46. offsetof(uname_info_t,name.sysname),
  47. offsetof(uname_info_t,name.nodename),
  48. offsetof(uname_info_t,name.release),
  49. offsetof(uname_info_t,name.version),
  50. offsetof(uname_info_t,name.machine),
  51. offsetof(uname_info_t,processor)
  52. };
  53. int uname_main(int argc, char **argv)
  54. {
  55. uname_info_t uname_info;
  56. #if defined(__sparc__) && defined(__linux__)
  57. char *fake_sparc = getenv("FAKE_SPARC");
  58. #endif
  59. const unsigned short int *delta;
  60. char toprint;
  61. toprint = bb_getopt_ulflags(argc, argv, options);
  62. if (argc != optind) {
  63. bb_show_usage();
  64. }
  65. if (toprint & (1 << 6)) {
  66. toprint = 0x3f;
  67. }
  68. if (toprint == 0) {
  69. toprint = 1; /* sysname */
  70. }
  71. if (uname(&uname_info.name) == -1) {
  72. bb_error_msg_and_die("cannot get system name");
  73. }
  74. #if defined(__sparc__) && defined(__linux__)
  75. if ((fake_sparc != NULL)
  76. && ((fake_sparc[0] == 'y')
  77. || (fake_sparc[0] == 'Y'))) {
  78. strcpy(uname_info.name.machine, "sparc");
  79. }
  80. #endif
  81. strcpy(uname_info.processor, "unknown");
  82. delta=utsname_offset;
  83. do {
  84. if (toprint & 1) {
  85. bb_printf(((char *)(&uname_info)) + *delta);
  86. if (toprint > 1) {
  87. putchar(' ');
  88. }
  89. }
  90. ++delta;
  91. } while (toprint >>= 1);
  92. putchar('\n');
  93. bb_fflush_stdout_and_exit(EXIT_SUCCESS);
  94. }