Changeset 203 for trunk/base
- Timestamp:
- Aug 20, 2002, 4:15:08 AM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/base/Tcl/port1.0/Pextlib.c
r202 r203 6 6 #include <sys/file.h> 7 7 #include <sys/types.h> 8 #include <sys/wait.h> 8 9 #include <tcl.h> 10 #include <unistd.h> 9 11 10 12 #define BUFSIZ 1024 … … 32 34 { 33 35 char buf[BUFSIZ]; 36 char *args[4]; 34 37 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; 37 42 cmdlen = cmdlenavail = BUFSIZ; 38 43 p = cmdstring = NULL; … … 95 100 } 96 101 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"); 98 125 if (p != NULL) 99 126 free(cmdstring); 100 127 101 while (fgets(buf, BUFSIZ, pipe) != NULL) { 128 /* read from simulated popen() pipe */ 129 while (fgets(buf, BUFSIZ, pdes) != NULL) { 102 130 int ret = ui_info(interp, buf); 103 131 if (ret != TCL_OK) … … 105 133 Tcl_AppendResult(interp, buf, NULL); 106 134 } 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; 114 141 } 115 142
Note: See TracChangeset
for help on using the changeset viewer.