2
0

dup.c 963 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include "test_helpers.h"
  5. int main(void) {
  6. int fd0 = creat("dup.out", 0777);
  7. ERROR_IF(creat, fd0, == -1);
  8. UNEXP_IF(creat, fd0, < 0);
  9. int fd1 = open("dup.out", 0);
  10. ERROR_IF(open, fd1, == -1);
  11. UNEXP_IF(open, fd1, < 0);
  12. int fd2 = dup(fd1);
  13. ERROR_IF(dup, fd2, == -1);
  14. UNEXP_IF(dup, fd2, < 0);
  15. printf("duped fd is %d greater than the original fd\n", fd2 - fd1);
  16. int c1 = close(fd1);
  17. ERROR_IF(close, c1, == -1);
  18. UNEXP_IF(close, c1, != 0);
  19. int c2 = close(fd2);
  20. ERROR_IF(close, c2, == -1);
  21. UNEXP_IF(close, c2, != 0);
  22. int fd3 = open("dup.out", 0x0002, 0x1000);
  23. ERROR_IF(open, fd3, == -1);
  24. UNEXP_IF(open, fd3, < 0);
  25. int fd4 = dup2(fd3, 1);
  26. ERROR_IF(dup2, fd4, == -1);
  27. UNEXP_IF(dup2, fd4, < 0);
  28. printf("hello fd %d", fd3);
  29. int c3 = close(fd3);
  30. ERROR_IF(close, c3, == -1);
  31. UNEXP_IF(close, c3, != 0);
  32. }