get_machine.c 2.4 KB

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