catm.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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(0 == 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. keep:
  47. bytes = read(input, buffer, BUFFER_SIZE);
  48. write(output, buffer, bytes);
  49. if(BUFFER_SIZE == bytes) goto keep;
  50. }
  51. free(buffer);
  52. return EXIT_SUCCESS;
  53. }