Changeset 203 for trunk/base


Ignore:
Timestamp:
Aug 20, 2002, 4:15:08 AM (22 years ago)
Author:
eric
Message:

Fork and use pipes in place of popen(), so stderr can be read as well.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/base/Tcl/port1.0/Pextlib.c

    r202 r203  
    66#include <sys/file.h>
    77#include <sys/types.h>
     8#include <sys/wait.h>
    89#include <tcl.h>
     10#include <unistd.h>
    911
    1012#define BUFSIZ 1024
     
    3234{
    3335        char buf[BUFSIZ];
     36        char *args[4];
    3437        char *cmdstring, *p;
    35         FILE *pipe;
    36         int i, cmdlen, cmdlenavail;
     38        FILE *pdes;
     39        int fdset[2];
     40        int i, cmdlen, cmdlenavail, ret;
     41        pid_t pid;
    3742        cmdlen = cmdlenavail = BUFSIZ;
    3843        p = cmdstring = NULL;
     
    95100        }
    96101
    97         pipe = popen(cmdstring, "r");
     102        if (pipe(fdset) == -1)
     103                return TCL_ERROR;
     104
     105        /*
     106         * Fork a child to run the command, in a popen() like fashion -
     107         * popen() itself is not used because stderr is also desired.
     108         */
     109        pid = fork();
     110        if (pid == -1)
     111                return TCL_ERROR;
     112        if (pid == 0) {
     113                close(fdset[0]);
     114                dup2(fdset[1], STDOUT_FILENO);
     115                dup2(fdset[1], STDERR_FILENO);
     116                /* XXX ugly string constants */
     117                args[0] = "sh";
     118                args[1] = "-c";
     119                args[2] = cmdstring;
     120                args[3] = NULL;
     121                execve("/bin/sh", args, NULL);
     122        }
     123        close(fdset[1]);
     124        pdes = fdopen(fdset[0], "r");
    98125        if (p != NULL)
    99126                free(cmdstring);
    100127
    101         while (fgets(buf, BUFSIZ, pipe) != NULL) {
     128        /* read from simulated popen() pipe */
     129        while (fgets(buf, BUFSIZ, pdes) != NULL) {
    102130                int ret = ui_info(interp, buf);
    103131                if (ret != TCL_OK)
     
    105133                Tcl_AppendResult(interp, buf, NULL);
    106134        }
    107 
    108         switch (pclose(pipe)) {
    109                 case 0:
    110                         return TCL_OK;
    111                 default:
    112                         return TCL_ERROR;
    113         }
     135        fclose(pdes);
     136        wait(&ret);
     137        if (ret == 0)
     138                return TCL_OK;
     139        else
     140                return TCL_ERROR;
    114141}
    115142
Note: See TracChangeset for help on using the changeset viewer.