+
+int piped_child(const char **command) {
+ int pid;
+ int to_child_pipe[2];
+ int from_child_pipe[2];
+
+ if (pipe (to_child_pipe) < 0) {
+ msg ("cannot create pipe: %s\n", strerror(errno));
+ return -1;
+ }
+ if (pipe (from_child_pipe) < 0) {
+ msg ("cannot create pipe: %s\n", strerror(errno));
+ return -1;
+ }
+ pid = fork ();
+ if (pid < 0) {
+ msg ("cannot fork: %s\n", strerror(errno));
+ return -1;
+ }
+ if (pid == 0) {
+ if (dup2 (to_child_pipe[0], STDIN_FILENO) < 0) {
+ msg ("cannot dup2 pipe: %s\n", strerror(errno));
+ exit(1);
+ }
+ if (close (to_child_pipe[1]) < 0) {
+ msg ("cannot close pipe: %s\n", strerror(errno));
+ exit(1);
+ }
+ if (close (from_child_pipe[0]) < 0) {
+ msg ("cannot close pipe: %s\n", strerror(errno));
+ exit(1);
+ }
+ if (dup2 (from_child_pipe[1], STDOUT_FILENO) < 0) {
+ msg ("cannot dup2 pipe: %s\n", strerror(errno));
+ exit(1);
+ }
+ setpgid(0, getpid());
+ execvp (command[0], (char *const *) command);
+ msg("cannot exec %s: %s\n", command[0], strerror(errno));
+ exit(1);
+ }
+ if (close (to_child_pipe[0]) < 0) {
+ msg ("cannot close pipe: %s\n", strerror(errno));
+ return -1;
+ }
+ if (close (from_child_pipe[1]) < 0) {
+ msg ("cannot close pipe: %s\n", strerror(errno));
+ return -1;
+ }
+ tormtape = to_child_pipe[1];
+ fromrmtape = from_child_pipe[0];
+ return pid;
+}