uname.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 [-asnrvm]\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. "\n", argv[0]);
  42. exit(1);
  43. }
  44. int main(int argc, char * argv[]) {
  45. struct utsname u;
  46. int flags = 0;
  47. int space = 0;
  48. for (int i = 1; i < argc; ++i) {
  49. if (argv[i][0] == '-') {
  50. char *c = &argv[i][1];
  51. while (*c) {
  52. switch (*c) {
  53. case 'a':
  54. flags |= FLAG_ALL;
  55. break;
  56. case 's':
  57. flags |= FLAG_SYSNAME;
  58. break;
  59. case 'n':
  60. flags |= FLAG_NODENAME;
  61. break;
  62. case 'r':
  63. flags |= FLAG_RELEASE;
  64. break;
  65. case 'v':
  66. flags |= FLAG_VERSION;
  67. break;
  68. case 'm':
  69. flags |= FLAG_MACHINE;
  70. break;
  71. case 'o':
  72. flags |= FLAG_OSNAME;
  73. break;
  74. case 'h':
  75. default:
  76. show_usage(argc, argv);
  77. break;
  78. }
  79. c++;
  80. }
  81. }
  82. }
  83. uname(&u);
  84. if (!flags) {
  85. /* By default, we just print the kernel name */
  86. flags = FLAG_SYSNAME;
  87. }
  88. if (flags & FLAG_SYSNAME) {
  89. if (space++) printf(" ");
  90. printf("%s", u.sysname);
  91. }
  92. if (flags & FLAG_NODENAME) {
  93. if (space++) printf(" ");
  94. printf("%s", u.nodename);
  95. }
  96. if (flags & FLAG_RELEASE) {
  97. if (space++) printf(" ");
  98. printf("%s", u.release);
  99. }
  100. if (flags & FLAG_VERSION) {
  101. if (space++) printf(" ");
  102. printf("%s", u.version);
  103. }
  104. if (flags & FLAG_MACHINE) {
  105. if (space++) printf(" ");
  106. printf("%s", u.machine);
  107. }
  108. if (flags & FLAG_OSNAME) {
  109. if (space++) printf(" ");
  110. printf("%s", "ToaruOS");
  111. }
  112. printf("\n");
  113. return 0;
  114. }