get_machine.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* -*- c-file-style: "linux";indent-tabs-mode:t -*- */
  2. /* Copyright (C) 2017 Jeremiah Orians
  3. * This file is part of mescc-tools.
  4. *
  5. * mescc-tools is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * mescc-tools is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sys/utsname.h>
  21. int match(char* a, char* b);
  22. #define TRUE 1
  23. //CONSTANT TRUE 1
  24. #define FALSE 0
  25. //CONSTANT FALSE 0
  26. /* Standard C main program */
  27. int main(int argc, char **argv)
  28. {
  29. int exact = FALSE;
  30. int override = FALSE;
  31. char* override_string;
  32. int option_index = 1;
  33. struct utsname* unameData = calloc(1, sizeof(struct utsname));
  34. uname(unameData);
  35. while(option_index <= argc)
  36. {
  37. if(NULL == argv[option_index])
  38. {
  39. option_index = option_index + 1;
  40. }
  41. else if(match(argv[option_index], "--exact"))
  42. {
  43. exact = TRUE;
  44. option_index = option_index + 1;
  45. }
  46. else if(match(argv[option_index], "--override"))
  47. {
  48. override = TRUE;
  49. if((option_index + 1) < argc)
  50. {
  51. override_string = argv[option_index + 1];
  52. option_index = option_index + 2;
  53. }
  54. else
  55. {
  56. fputs("--override requires an actual override string\n", stderr);
  57. exit(EXIT_FAILURE);
  58. }
  59. }
  60. else if(match(argv[option_index], "--os") || match(argv[option_index], "--OS"))
  61. {
  62. if(override) fputs(override_string, stdout);
  63. else fputs(unameData->sysname, stdout);
  64. fputc('\n', stdout);
  65. exit(EXIT_SUCCESS);
  66. }
  67. else if(match(argv[option_index], "--blood"))
  68. {
  69. if(override) fputs(override_string, stdout);
  70. else if(match("aarch64", unameData->machine)
  71. || match("amd64", unameData->machine)
  72. || match("ppc64le", unameData->machine)
  73. || match("riscv64", unameData->machine)
  74. || match("x86_64", unameData->machine)) fputs("--64", stdout);
  75. fputc('\n', stdout);
  76. exit(EXIT_SUCCESS);
  77. }
  78. else if(match(argv[option_index], "--endian"))
  79. {
  80. if(override) fputs(override_string, stdout);
  81. else if(match("aarch64", unameData->machine)
  82. || match("amd64", unameData->machine)
  83. || match("ppc64le", unameData->machine)
  84. || match("riscv64", unameData->machine)
  85. || match("x86_64", unameData->machine)
  86. || match("i386", unameData->machine)
  87. || match("i486", unameData->machine)
  88. || match("i586", unameData->machine)
  89. || match("i686", unameData->machine)
  90. || match("i686-pae", unameData->machine))fputs("--little-endian", stdout);
  91. else fputs("--big-endian", stdout);
  92. fputc('\n', stdout);
  93. exit(EXIT_SUCCESS);
  94. }
  95. else if(match(argv[option_index], "--hex2"))
  96. {
  97. if(override) fputs(override_string, stdout);
  98. else if(match("aarch64", unameData->machine)) fputs("0x400000", stdout);
  99. else if(match("armv7l", unameData->machine)) fputs("0x10000", stdout);
  100. else if(match("amd64", unameData->machine)
  101. || match("x86_64", unameData->machine)) fputs("0x600000", stdout);
  102. else if(match("ppc64le", unameData->machine)) fputs("0x10000", stdout);
  103. else if(match("riscv64", unameData->machine)) fputs("0x600000", stdout);
  104. else if(match("i386", unameData->machine)
  105. || match("i486", unameData->machine)
  106. || match("i586", unameData->machine)
  107. || match("i686", unameData->machine)
  108. || match("i686-pae", unameData->machine)) fputs("0x08048000", stdout);
  109. else fputs("0x0", stdout);
  110. fputc('\n', stdout);
  111. exit(EXIT_SUCCESS);
  112. }
  113. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  114. {
  115. fputs("get_machine 1.5.0\n", stdout);
  116. exit(EXIT_SUCCESS);
  117. }
  118. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  119. {
  120. fputs("If you want exact architecture use --exact\n", stderr);
  121. fputs("If you want to know the Operating system use --os\n", stderr);
  122. fputs("If you wish to override the output to anything you want use --override\n", stderr);
  123. exit(EXIT_SUCCESS);
  124. }
  125. else
  126. {
  127. fputs("Unknown option\n", stderr);
  128. exit(EXIT_FAILURE);
  129. }
  130. }
  131. if(override) fputs(override_string, stdout);
  132. else if(!exact)
  133. {
  134. if(match("i386", unameData->machine) ||
  135. match("i486", unameData->machine) ||
  136. match("i586", unameData->machine) ||
  137. match("i686", unameData->machine) ||
  138. match("i686-pae", unameData->machine)) fputs("x86", stdout);
  139. else if(match("x86_64", unameData->machine)) fputs("amd64", stdout);
  140. else fputs(unameData->machine, stdout);
  141. }
  142. else fputs(unameData->machine, stdout);
  143. fputs("\n", stdout);
  144. return EXIT_SUCCESS;
  145. }