Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE /
- #include <errno.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #define READ 0
- #define WRITE 1
- pid_t spawn(char *cmdstring);
- int main(int argc, char *argv[])
- {
- pid_t childpid = spawn("man 2 open");
- return 0;
- }
- pid_t spawn(char *cmdstring)
- {
- int pipefd[2];
- char buf[32], *argv[] = {"/bin/sh", "-c", cmdstring, NULL};
- pid_t pid = fork();
- if (pipe(pipefd) == -1)
- {
- perror("pipe");
- return 1;
- }
- if (pid == -1) // Parent
- perror("fork()");
- if (pid == 0) // child
- {
- if (write(pipefd[WRITE], "child wrote here", sizeof("child wrote here")) < 0)
- perror("write");
- // execv("/bin/sh", argv);
- // perror("execve failed!");
- _exit(123);
- }
- int wstatus;
- if (pid != wait(&wstatus) || !WIFEXITED(wstatus))
- perror("Divorsed");
- if (read(pipefd[WRITE], buf, sizeof(buf)) < 0)
- perror("read");
- printf("%s\n", buf);
- return pid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement