exercise_5_5.c 780 B

1234567891011121314151617181920212223242526272829303132
  1. /*
  2. Write a program to verify that duplicated file descriptors share a file offset value
  3. and open file status flags.
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <assert.h>
  9. int
  10. main (int argc, char *argv[])
  11. {
  12. if (argc != 2) return EXIT_FAILURE;
  13. int fd;
  14. if ((fd = open (argv[1],
  15. O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU|S_IRWXG|S_IRWXO)) == -1)
  16. return EXIT_FAILURE;
  17. int dup_fd;
  18. if ((dup_fd = dup (fd)) == -1) return EXIT_FAILURE;
  19. assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));
  20. write (fd, "from fd\n", 8);
  21. assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));
  22. write (dup_fd, "from dup_fd\n", 12);
  23. assert (lseek (fd, 0, SEEK_CUR) == lseek (dup_fd, 0, SEEK_CUR));
  24. return EXIT_SUCCESS;
  25. }