catm.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // CONSTANT BUFFER_SIZE 4096
  22. #define BUFFER_SIZE 4096
  23. int main(int argc, char** argv)
  24. {
  25. if(2 > argc)
  26. {
  27. fputs("catm requires 2 or more arguments\n", stderr);
  28. exit(EXIT_FAILURE);
  29. }
  30. int output = open(argv[1], 577 , 384);
  31. if(-1 == output)
  32. {
  33. fputs("The file: ", stderr);
  34. fputs(argv[1], stderr);
  35. fputs(" is not a valid output file name\n", stderr);
  36. exit(EXIT_FAILURE);
  37. }
  38. int i;
  39. int bytes;
  40. char* buffer = calloc(BUFFER_SIZE + 1, sizeof(char));
  41. int input;
  42. for(i = 2; i < argc ; i = i + 1)
  43. {
  44. input = open(argv[i], 0, 0);
  45. if(-1 == input)
  46. {
  47. fputs("The file: ", stderr);
  48. fputs(argv[i], stderr);
  49. fputs(" is not a valid input file name\n", stderr);
  50. exit(EXIT_FAILURE);
  51. }
  52. keep:
  53. bytes = read(input, buffer, BUFFER_SIZE);
  54. write(output, buffer, bytes);
  55. if(BUFFER_SIZE == bytes) goto keep;
  56. }
  57. free(buffer);
  58. return EXIT_SUCCESS;
  59. }