catm.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 2019 Jeremiah Orians
  2. * This file is part of mescc-tools
  3. *
  4. * mescc-tools 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. * mescc-tools 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 mescc-tools. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. void file_print(char* s, FILE* f);
  22. // CONSTANT BUFFER_SIZE 4096
  23. #define BUFFER_SIZE 4096
  24. int main(int argc, char** argv)
  25. {
  26. if(2 > argc)
  27. {
  28. file_print("catm requires 2 or more arguments\n", stderr);
  29. exit(EXIT_FAILURE);
  30. }
  31. int output = open(argv[1], 577 , 384);
  32. if(-1 == output)
  33. {
  34. file_print("The file: ", stderr);
  35. file_print(argv[1], stderr);
  36. file_print(" is not a valid output file name\n", stderr);
  37. exit(EXIT_FAILURE);
  38. }
  39. int i;
  40. int bytes;
  41. char* buffer = calloc(BUFFER_SIZE + 1, sizeof(char));
  42. int input;
  43. for(i = 2; i <= argc ; i = i + 1)
  44. {
  45. input = open(argv[i], 0, 0);
  46. if(-1 == input)
  47. {
  48. file_print("The file: ", stderr);
  49. file_print(argv[i], stderr);
  50. file_print(" is not a valid input file name\n", stderr);
  51. exit(EXIT_FAILURE);
  52. }
  53. keep:
  54. bytes = read(input, buffer, BUFFER_SIZE);
  55. write(output, buffer, bytes);
  56. if(BUFFER_SIZE == bytes) goto keep;
  57. }
  58. free(buffer);
  59. return EXIT_SUCCESS;
  60. }