uname-obsd.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* $OpenBSD: uname.c,v 1.19 2016/10/28 07:22:59 schwarze Exp $ */
  2. /*
  3. * Copyright (c) 1994 Winning Strategies, Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by Winning Strategies, Inc.
  17. * 4. The name of Winning Strategies, Inc. may not be used to endorse or
  18. * promote products derived from this software without specific prior
  19. * written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  22. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  30. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <sys/param.h> /* MACHINE_ARCH */
  33. #include <sys/utsname.h>
  34. #include <err.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <unistd.h>
  38. static void __dead usage(void);
  39. #define PRINT_SYSNAME 0x01
  40. #define PRINT_NODENAME 0x02
  41. #define PRINT_RELEASE 0x04
  42. #define PRINT_VERSION 0x08
  43. #define PRINT_MACHINE 0x10
  44. #define PRINT_ALL 0x1f
  45. #define PRINT_MACHINE_ARCH 0x20
  46. int
  47. main(int argc, char *argv[])
  48. {
  49. struct utsname u;
  50. int c;
  51. int space = 0;
  52. int print_mask = 0;
  53. if (pledge("stdio", NULL) == -1)
  54. err(1, "pledge");
  55. while ((c = getopt(argc, argv, "amnrsvp")) != -1 ) {
  56. switch (c) {
  57. case 'a':
  58. print_mask |= PRINT_ALL;
  59. break;
  60. case 'm':
  61. print_mask |= PRINT_MACHINE;
  62. break;
  63. case 'n':
  64. print_mask |= PRINT_NODENAME;
  65. break;
  66. case 'p':
  67. print_mask |= PRINT_MACHINE_ARCH;
  68. break;
  69. case 'r':
  70. print_mask |= PRINT_RELEASE;
  71. break;
  72. case 's':
  73. print_mask |= PRINT_SYSNAME;
  74. break;
  75. case 'v':
  76. print_mask |= PRINT_VERSION;
  77. break;
  78. default:
  79. usage();
  80. }
  81. }
  82. if (optind != argc)
  83. usage();
  84. if (!print_mask)
  85. print_mask = PRINT_SYSNAME;
  86. if (uname(&u) == -1)
  87. err(1, NULL);
  88. if (print_mask & PRINT_SYSNAME) {
  89. space++;
  90. fputs("OpenBSD", stdout);
  91. }
  92. if (print_mask & PRINT_NODENAME) {
  93. if (space++)
  94. putchar(' ');
  95. fputs(u.nodename, stdout);
  96. }
  97. if (print_mask & PRINT_RELEASE) {
  98. if (space++)
  99. putchar(' ');
  100. fputs(u.release, stdout);
  101. }
  102. if (print_mask & PRINT_VERSION) {
  103. if (space++)
  104. putchar(' ');
  105. fputs(u.version, stdout);
  106. }
  107. if (print_mask & PRINT_MACHINE) {
  108. if (space++)
  109. putchar(' ');
  110. fputs(u.machine, stdout);
  111. }
  112. if (print_mask & PRINT_MACHINE_ARCH) {
  113. if (space++)
  114. putchar(' ');
  115. fputs(MACHINE_ARCH, stdout);
  116. }
  117. putchar('\n');
  118. return 0;
  119. }
  120. static void __dead
  121. usage(void)
  122. {
  123. fprintf(stderr, "usage: uname [-amnprsv]\n");
  124. exit(1);
  125. }