uname.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * uname - Print kernel version information
  7. *
  8. * Supports all the usual options (a,s,n,r,v,m,o)
  9. *
  10. * Note that o is hardcoded, which is also the situation in
  11. * the coreutils implementation, so I don't see that being
  12. * a problem. If you want to build this uname for Linux or
  13. * something... you'll have to change that.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <sys/utsname.h>
  19. #define FLAG_SYSNAME 0x01
  20. #define FLAG_NODENAME 0x02
  21. #define FLAG_RELEASE 0x04
  22. #define FLAG_VERSION 0x08
  23. #define FLAG_MACHINE 0x10
  24. #define FLAG_OSNAME 0x20
  25. #define FLAG_ALL (FLAG_SYSNAME|FLAG_NODENAME|FLAG_RELEASE|FLAG_VERSION|FLAG_MACHINE|FLAG_OSNAME)
  26. #define _ITALIC "\033[3m"
  27. #define _END "\033[0m\n"
  28. void show_usage(int argc, char * argv[]) {
  29. fprintf(stderr,
  30. "uname - Print system version information.\n"
  31. "\n"
  32. "usage: %s [-asnrvmp]\n"
  33. "\n"
  34. " -a " _ITALIC "Print the standard uname string we all love" _END
  35. " -s " _ITALIC "Print kernel name" _END
  36. " -n " _ITALIC "Print system name" _END
  37. " -r " _ITALIC "Print kernel version number" _END
  38. " -v " _ITALIC "Print the extra kernel version information" _END
  39. " -m " _ITALIC "Print the architecture name" _END
  40. " -o " _ITALIC "Print operating system name" _END
  41. " -p " _ITALIC "Alias to -m" _END
  42. "\n", argv[0]);
  43. exit(1);
  44. }
  45. int main(int argc, char * argv[]) {
  46. struct utsname u;
  47. int flags = 0;
  48. int space = 0;
  49. for (int i = 1; i < argc; ++i) {
  50. if (argv[i][0] == '-') {
  51. char *c = &argv[i][1];
  52. while (*c) {
  53. switch (*c) {
  54. case 'a':
  55. flags |= FLAG_ALL;
  56. break;
  57. case 's':
  58. flags |= FLAG_SYSNAME;
  59. break;
  60. case 'n':
  61. flags |= FLAG_NODENAME;
  62. break;
  63. case 'r':
  64. flags |= FLAG_RELEASE;
  65. break;
  66. case 'v':
  67. flags |= FLAG_VERSION;
  68. break;
  69. case 'm':
  70. case 'p':
  71. flags |= FLAG_MACHINE;
  72. break;
  73. case 'o':
  74. flags |= FLAG_OSNAME;
  75. break;
  76. case 'h':
  77. default:
  78. show_usage(argc, argv);
  79. break;
  80. }
  81. c++;
  82. }
  83. }
  84. }
  85. uname(&u);
  86. if (!flags) {
  87. /* By default, we just print the kernel name */
  88. flags = FLAG_SYSNAME;
  89. }
  90. if (flags & FLAG_SYSNAME) {
  91. if (space++) printf(" ");
  92. printf("%s", u.sysname);
  93. }
  94. if (flags & FLAG_NODENAME) {
  95. if (space++) printf(" ");
  96. printf("%s", u.nodename);
  97. }
  98. if (flags & FLAG_RELEASE) {
  99. if (space++) printf(" ");
  100. printf("%s", u.release);
  101. }
  102. if (flags & FLAG_VERSION) {
  103. if (space++) printf(" ");
  104. printf("%s", u.version);
  105. }
  106. if (flags & FLAG_MACHINE) {
  107. if (space++) printf(" ");
  108. printf("%s", u.machine);
  109. }
  110. if (flags & FLAG_OSNAME) {
  111. if (space++) printf(" ");
  112. printf("%s", "ToaruOS");
  113. }
  114. printf("\n");
  115. return 0;
  116. }