popen.c (18471) | popen.c (19902) |
---|---|
1/* 2 * Copyright (c) 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software written by Ken Arnold and 6 * published in UNIX Review, Vol. 6, No. 8. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 19 unchanged lines hidden (view full) --- 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * | 1/* 2 * Copyright (c) 1988, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software written by Ken Arnold and 6 * published in UNIX Review, Vol. 6, No. 8. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 19 unchanged lines hidden (view full) --- 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * |
36 * $Id$ | 36 * $Id: popen.c,v 1.4 1996/09/22 21:53:32 wosch Exp $ |
37 */ 38 39#if 0 40#ifndef lint 41static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; 42#endif /* not lint */ 43#endif 44 --- 5 unchanged lines hidden (view full) --- 50#include <signal.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> 55 56#include "extern.h" 57 | 37 */ 38 39#if 0 40#ifndef lint 41static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 4/6/94"; 42#endif /* not lint */ 43#endif 44 --- 5 unchanged lines hidden (view full) --- 50#include <signal.h> 51#include <stdio.h> 52#include <stdlib.h> 53#include <string.h> 54#include <unistd.h> 55 56#include "extern.h" 57 |
58#define MAXUSRARGS 100 59#define MAXGLOBARGS 1000 60 |
|
58/* 59 * Special version of popen which avoids call to shell. This ensures noone 60 * may create a pipe to a hidden program as a side effect of a list or dir 61 * command. 62 */ 63static int *pids; 64static int fds; 65 66FILE * 67ftpd_popen(program, type) 68 char *program, *type; 69{ 70 char *cp; 71 FILE *iop; 72 int argc, gargc, pdes[2], pid; | 61/* 62 * Special version of popen which avoids call to shell. This ensures noone 63 * may create a pipe to a hidden program as a side effect of a list or dir 64 * command. 65 */ 66static int *pids; 67static int fds; 68 69FILE * 70ftpd_popen(program, type) 71 char *program, *type; 72{ 73 char *cp; 74 FILE *iop; 75 int argc, gargc, pdes[2], pid; |
73 char **pop, *argv[100], *gargv[1000]; | 76 char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS]; |
74 75 if (((*type != 'r') && (*type != 'w')) || type[1]) 76 return (NULL); 77 78 if (!pids) { 79 if ((fds = getdtablesize()) <= 0) 80 return (NULL); 81 if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL) 82 return (NULL); 83 memset(pids, 0, fds * sizeof(int)); 84 } 85 if (pipe(pdes) < 0) 86 return (NULL); 87 88 /* break up string into pieces */ | 77 78 if (((*type != 'r') && (*type != 'w')) || type[1]) 79 return (NULL); 80 81 if (!pids) { 82 if ((fds = getdtablesize()) <= 0) 83 return (NULL); 84 if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL) 85 return (NULL); 86 memset(pids, 0, fds * sizeof(int)); 87 } 88 if (pipe(pdes) < 0) 89 return (NULL); 90 91 /* break up string into pieces */ |
89 for (argc = 0, cp = program;; cp = NULL) | 92 for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) |
90 if (!(argv[argc++] = strtok(cp, " \t\n"))) 91 break; 92 93 /* glob each piece */ 94 gargv[0] = argv[0]; | 93 if (!(argv[argc++] = strtok(cp, " \t\n"))) 94 break; 95 96 /* glob each piece */ 97 gargv[0] = argv[0]; |
95 for (gargc = argc = 1; argv[argc]; argc++) { | 98 for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) { |
96 glob_t gl; 97 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 98 99 memset(&gl, 0, sizeof(gl)); 100 if (glob(argv[argc], flags, NULL, &gl)) 101 gargv[gargc++] = strdup(argv[argc]); 102 else | 99 glob_t gl; 100 int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE; 101 102 memset(&gl, 0, sizeof(gl)); 103 if (glob(argv[argc], flags, NULL, &gl)) 104 gargv[gargc++] = strdup(argv[argc]); 105 else |
103 for (pop = gl.gl_pathv; *pop; pop++) | 106 for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1); 107 pop++) |
104 gargv[gargc++] = strdup(*pop); 105 globfree(&gl); 106 } 107 gargv[gargc] = NULL; 108 109 iop = NULL; 110 switch(pid = vfork()) { 111 case -1: /* error */ --- 63 unchanged lines hidden --- | 108 gargv[gargc++] = strdup(*pop); 109 globfree(&gl); 110 } 111 gargv[gargc] = NULL; 112 113 iop = NULL; 114 switch(pid = vfork()) { 115 case -1: /* error */ --- 63 unchanged lines hidden --- |