getopt.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 2016 Jeremiah Orians
  2. * This file is part of M2-Planet.
  3. *
  4. * M2-Planet is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * M2-Planet is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with M2-Planet. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include<stdlib.h>
  18. #include<stdio.h>
  19. char* int2str(int x, int base, int signed_p);
  20. int char2int(char c)
  21. {
  22. if((48 <= c) && (57>= c))
  23. {
  24. return (c - 48);
  25. }
  26. return 0;
  27. }
  28. void write_string(char* c, FILE* f)
  29. {
  30. while(0 != c[0])
  31. {
  32. fputc(c[0], f);
  33. c = c + 1;
  34. }
  35. }
  36. void sum_file(FILE* input, FILE* output)
  37. {
  38. int c = fgetc(input);
  39. int sum = 0;
  40. while(0 <= c)
  41. {
  42. sum = sum + char2int(c);
  43. c = fgetc(input);
  44. }
  45. write_string(int2str(sum, 10, 0), output);
  46. fputc(10, output);
  47. }
  48. int match(char* a, char* b);
  49. int main(int argc, char** argv)
  50. {
  51. FILE* in = stdin;
  52. FILE* out = stdout;
  53. int i = 1;
  54. while(i <= argc)
  55. {
  56. if(NULL == argv[i])
  57. {
  58. i = i + 1;
  59. }
  60. else if(match(argv[i], "-f"))
  61. {
  62. in = fopen(argv[i + 1], "r");
  63. if(NULL == in)
  64. {
  65. write_string("Unable to open for reading file: ", stderr);
  66. write_string(argv[i + 1], stderr);
  67. write_string("\x0A Aborting to avoid problems\x0A", stderr);
  68. exit(EXIT_FAILURE);
  69. }
  70. i = i + 2;
  71. }
  72. else if(match(argv[i], "-o"))
  73. {
  74. out = fopen(argv[i + 1], "w");
  75. if(NULL == out)
  76. {
  77. write_string("Unable to open for writing file: ", stderr);
  78. write_string(argv[i + 1], stderr);
  79. write_string("\x0A Aborting to avoid problems\x0A", stderr);
  80. exit(EXIT_FAILURE);
  81. }
  82. i = i + 2;
  83. }
  84. else if(match(argv[i], "--help"))
  85. {
  86. write_string(" -f input file\x0A -o output file\x0A --help for this message\x0A --version for file version\x0A", stdout);
  87. exit(EXIT_SUCCESS);
  88. }
  89. else if(match(argv[i], "--version"))
  90. {
  91. write_string("Basic test version 0.0.0.1a\x0A", stderr);
  92. exit(EXIT_SUCCESS);
  93. }
  94. else
  95. {
  96. write_string("UNKNOWN ARGUMENT\x0A", stdout);
  97. exit(EXIT_FAILURE);
  98. }
  99. }
  100. sum_file(in, out);
  101. if (in != stdin)
  102. {
  103. fclose(in);
  104. }
  105. if (out != stdout)
  106. {
  107. fclose(out);
  108. }
  109. return 0;
  110. }