main.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <fcntl.h>
  10. #include <errno.h>
  11. int
  12. main(int argc, char **argv)
  13. {
  14. int n, m;
  15. char buf[BUFSIZ];
  16. if (argc != 3) {
  17. fprintf(stderr, "usage: %s <from> <to>\n", argv[0]);
  18. exit(1);
  19. }
  20. printf("##open %s\n", argv[1]);
  21. int in = open(argv[1], O_RDONLY);
  22. if (in < 0) {
  23. fprintf(stderr, "error opening input %s: %s\n", argv[1],
  24. strerror(errno));
  25. exit(1);
  26. }
  27. printf("##open %s\n", argv[2]);
  28. int out = open(argv[2], O_WRONLY | O_CREAT, 0660);
  29. if (out < 0) {
  30. fprintf(stderr, "error opening output %s: %s\n", argv[2],
  31. strerror(errno));
  32. exit(1);
  33. }
  34. printf("##read content of %s, and write it to %s\n", argv[1], argv[2]);
  35. while ((n = read(in, buf, BUFSIZ)) > 0) {
  36. while (n > 0) {
  37. m = write(out, buf, n);
  38. if (m < 0) {
  39. fprintf(stderr, "write error: %s\n", strerror(errno));
  40. exit(1);
  41. }
  42. n -= m;
  43. }
  44. }
  45. if (n < 0) {
  46. fprintf(stderr, "read error: %s\n", strerror(errno));
  47. exit(1);
  48. }
  49. printf("##success.\n");
  50. return EXIT_SUCCESS;
  51. }