popen.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Modified 3/03/2001 Manuel Novoa III
  3. *
  4. * Added check for legal mode arg.
  5. * Call fdopen and check return value before forking.
  6. * Reduced code size by using variables pr and pnr instead of array refs.
  7. */
  8. #include <stdio.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/wait.h>
  12. #include <errno.h>
  13. FILE *popen (const char *command, const char *mode)
  14. {
  15. FILE *fp;
  16. int pipe_fd[2];
  17. int pid, reading;
  18. int pr, pnr;
  19. reading = (mode[0] == 'r');
  20. if ((!reading && (mode[0] != 'w')) || mode[1]) {
  21. __set_errno(EINVAL); /* Invalid mode arg. */
  22. } else if (pipe(pipe_fd) == 0) {
  23. pr = pipe_fd[reading];
  24. pnr = pipe_fd[1-reading];
  25. if ((fp = fdopen(pnr, mode)) != NULL) {
  26. if ((pid = vfork()) == 0) { /* vfork -- child */
  27. close(pnr);
  28. close(reading);
  29. if (pr != reading) {
  30. dup2(pr, reading);
  31. close(pr);
  32. }
  33. execl("/bin/sh", "sh", "-c", command, (char *) 0);
  34. _exit(255); /* execl failed! */
  35. } else { /* vfork -- parent or failed */
  36. close(pr);
  37. if (pid > 0) { /* vfork -- parent */
  38. return fp;
  39. } else { /* vfork -- failed! */
  40. fclose(fp);
  41. }
  42. }
  43. } else { /* fdopen failed */
  44. close(pr);
  45. close(pnr);
  46. }
  47. }
  48. return NULL;
  49. }
  50. int pclose(FILE *fd)
  51. {
  52. int waitstat;
  53. if (fclose(fd) != 0) {
  54. return EOF;
  55. }
  56. wait(&waitstat);
  57. return waitstat;
  58. }