myopen.c 904 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "unp.h"
  2. int
  3. my_open(const char *pathname, int mode)
  4. {
  5. int fd, sockfd[2], status;
  6. pid_t childpid;
  7. char c, argsockfd[10], argmode[10];
  8. Socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);
  9. if ( (childpid = Fork()) == 0) { /* child process */
  10. Close(sockfd[0]);
  11. snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[1]);
  12. snprintf(argmode, sizeof(argmode), "%d", mode);
  13. execl("./openfile", "openfile", argsockfd, pathname, argmode,
  14. (char *) NULL);
  15. err_sys("execl error");
  16. }
  17. /* parent process - wait for the child to terminate */
  18. Close(sockfd[1]); /* close the end we don't use */
  19. Waitpid(childpid, &status, 0);
  20. if (WIFEXITED(status) == 0)
  21. err_quit("child did not terminate");
  22. if ( (status = WEXITSTATUS(status)) == 0)
  23. Read_fd(sockfd[0], &c, 1, &fd);
  24. else {
  25. errno = status; /* set errno value from child's status */
  26. fd = -1;
  27. }
  28. Close(sockfd[0]);
  29. return(fd);
  30. }