From: Chris Allegretta Date: Thu, 21 Mar 2002 23:39:56 +0000 (+0000) Subject: Redo open_pipe with just pipes and no FIFOs X-Git-Tag: v1.1.8~20 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=b23d14460e09447278b165958ca9ad03caf8ab9b;p=nano.git Redo open_pipe with just pipes and no FIFOs git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1138 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/files.c b/files.c index 5d8f30a5..4913a0d8 100644 --- a/files.c +++ b/files.c @@ -291,37 +291,34 @@ int read_file(int fd, char *filename, int quiet) #ifndef NANO_SMALL int open_pipe(char *command) { - int forkpid, fd; - char *pipefile, *execute; - - execute = charalloc(strlen(command) + 24); - if ((pipefile = safe_tempnam(0, "nano.")) == NULL) { - statusbar(_("Could not create a temporary filename: %s"), - strerror(errno)); - free(execute); - free(pipefile); - return 1; - } + int fd[2], pid; + + /* Make our pipes. */ - sprintf(execute,"%s 2>&1 > %s",command,pipefile); - umask(0); - mkfifo(pipefile,0700); - forkpid = fork(); - if (forkpid == -1) { - statusbar(_("Could not fork")); - free(execute); - free(pipefile); + if (pipe(fd) == -1) { + statusbar("Could not pipe"); return 1; } - else if (forkpid == 0) { - execl("/bin/sh","/bin/sh","-c",execute,0); + + /* Fork a child */ + + if ((pid = fork()) == 0) { + close(fd[0]); + dup2(fd[1], fileno(stdout)); + dup2(fd[1], fileno(stderr)); + /* If execl() returns at all, there was an error. */ + + execl("/bin/sh","/bin/sh","-c",command,0); exit(0); } - fd = open(pipefile,O_RDONLY); - read_file(fd,"stdin",0); - unlink(pipefile); - free(execute); - free(pipefile); + else if (pid == -1) { + statusbar(_("Could not fork")); + return 1; + } + + /* Else continue as parent */ + close(fd[1]); + read_file(fd[0],"stdin",0); set_modified(); return 0; }