Skip to content
kohkubo edited this page Apr 11, 2022 · 2 revisions
#include <unistd.h>
#include <iostream>

int main() {
  int fd[2];
  if (pipe(fd) == -1) {
    std::cout << "pipe() failed." << std::endl;
    return -1;
  }
  pid_t pid = fork();
  switch (pid) {
  case -1:
    std::cout << "fork() failed." << std::endl;
    break;
  case 0:
    close(fd[0]);
    dup2(fd[1], STDOUT_FILENO);
    const char *argv[] = {
        "php",
        "index.php",
        NULL,
    };
    if (execve("/usr/local/bin/php", (char **)argv, NULL) == -1)
      std::cout << "execve() failed." << std::endl;
    break;
  }
  close(fd[1]);
  char buf[1024];
  read(fd[0], buf, 1024);
  std::cout << buf << std::endl;
  close(fd[0]);

  int status; // statusによって、動きを変える必要があるらしい。
  if (waitpid(pid, &status, 0) == -1) {
    std::cout << "waitpid() failed." << std::endl;
    return -1;
  }
  return 0;
}