get_machine.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. void file_print(char* s, FILE* f);
  22. int match(char* a, char* b);
  23. #define TRUE 1
  24. //CONSTANT TRUE 1
  25. #define FALSE 0
  26. //CONSTANT FALSE 0
  27. /* Standard C main program */
  28. int main(int argc, char **argv)
  29. {
  30. int exact = FALSE;
  31. int override = FALSE;
  32. char* override_string;
  33. int option_index = 1;
  34. struct utsname* unameData = calloc(1, sizeof(struct utsname));
  35. uname(unameData);
  36. while(option_index <= argc)
  37. {
  38. if(NULL == argv[option_index])
  39. {
  40. option_index = option_index + 1;
  41. }
  42. else if(match(argv[option_index], "--exact"))
  43. {
  44. exact = TRUE;
  45. option_index = option_index + 1;
  46. }
  47. else if(match(argv[option_index], "--override"))
  48. {
  49. override = TRUE;
  50. override_string = argv[option_index + 1];
  51. option_index = option_index + 2;
  52. }
  53. else if(match(argv[option_index], "--OS"))
  54. {
  55. file_print(unameData->sysname, stdout);
  56. fputc('\n', stdout);
  57. exit(EXIT_SUCCESS);
  58. }
  59. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  60. {
  61. file_print("get_machine 0.6.0\n", stdout);
  62. exit(EXIT_SUCCESS);
  63. }
  64. else if(match(argv[option_index], "-h") || match(argv[option_index], "--help"))
  65. {
  66. file_print("If you want exact architecture use --exact\n", stderr);
  67. file_print("If you want to know the Operating system use --OS\n", stderr);
  68. file_print("If you wish to override the output to anything you want use --override\n", stderr);
  69. exit(EXIT_SUCCESS);
  70. }
  71. else
  72. {
  73. file_print("Unknown option\n", stderr);
  74. exit(EXIT_FAILURE);
  75. }
  76. }
  77. if(override) file_print(override_string, stdout);
  78. else if(!exact)
  79. {
  80. if(match("i386", unameData->machine) ||
  81. match("i486", unameData->machine) ||
  82. match("i586", unameData->machine) ||
  83. match("i686", unameData->machine) ||
  84. match("i686-pae", unameData->machine)) file_print("x86", stdout);
  85. else if(match("x86_64", unameData->machine)) file_print("amd64", stdout);
  86. else file_print(unameData->machine, stdout);
  87. }
  88. else file_print(unameData->machine, stdout);
  89. file_print("\n", stdout);
  90. return EXIT_SUCCESS;
  91. }