get_machine.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. while(option_index <= argc)
  35. {
  36. if(NULL == argv[option_index])
  37. {
  38. option_index = option_index + 1;
  39. }
  40. else if(match(argv[option_index], "--exact"))
  41. {
  42. exact = TRUE;
  43. option_index = option_index + 1;
  44. }
  45. else if(match(argv[option_index], "--override"))
  46. {
  47. override = TRUE;
  48. override_string = argv[option_index + 1];
  49. option_index = option_index + 2;
  50. }
  51. else if(match(argv[option_index], "-V") || match(argv[option_index], "--version"))
  52. {
  53. file_print("get_machine 0.1\n", stdout);
  54. exit(EXIT_SUCCESS);
  55. }
  56. else
  57. {
  58. file_print("Unknown option\n", stderr);
  59. exit(EXIT_FAILURE);
  60. }
  61. }
  62. struct utsname* unameData = calloc(1, sizeof(struct utsname));
  63. uname(unameData);
  64. if(override) file_print(override_string, stdout);
  65. else if(!exact)
  66. {
  67. if(match("i386", unameData->machine) ||
  68. match("i486", unameData->machine) ||
  69. match("i586", unameData->machine) ||
  70. match("i686", unameData->machine) ||
  71. match("i686-pae", unameData->machine)) file_print("x86", stdout);
  72. else if(match("x86_64", unameData->machine)) file_print("amd64", stdout);
  73. else file_print(unameData->machine, stdout);
  74. }
  75. else file_print(unameData->machine, stdout);
  76. file_print("\n", stdout);
  77. return EXIT_SUCCESS;
  78. }