for (fd = PR_MIN(65536,getdtablesize()); --fd > 2; close(fd)) ;
{ int ndesc = getdtablesize(); for (fd = PR_MIN(65536, ndesc); --fd > 2; close(fd)); }
safe_popen(char *cmd){    int p[2], fd, argc;    pid_t pid;    char *argv[SAFE_POPEN_MAXARGS + 1];    FILE *fp;    static char blank[] = " \t";    static struct sigaction newact;    if (pipe(p) < 0)	return 0;    /* Setup signals so that SIGCHLD is ignored as we want to do waitpid */    newact.sa_handler = SIG_DFL;    newact.sa_flags = 0;    sigfillset(&newact.sa_mask);    sigaction (SIGCHLD, &newact, &oldact);    pid = fork();    switch (pid) {      case -1:	close(p[0]);	close(p[1]);	sigaction (SIGCHLD, &oldact, NULL);	return 0;      case 0:	/* dup write-side of pipe to stderr and stdout */	if (p[1] != 1) dup2(p[1], 1);	if (p[1] != 2) dup2(p[1], 2);	close(0);	for (fd = PR_MIN(65536,getdtablesize()); --fd > 2; close(fd))	    ;	/* clean up environment in the child process */	putenv("PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc:/usr/etc");	putenv("SHELL=/bin/sh");	putenv("IFS= \t");	/*	 * The caller may have passed us a string that is in text	 * space. It may be illegal to modify the string	 */	cmd = strdup(cmd);	/* format argv */	argv[0] = strtok(cmd, blank);	argc = 1;	while ((argv[argc] = strtok(0, blank)) != 0) {	    if (++argc == SAFE_POPEN_MAXARGS) {		argv[argc] = 0;		break;	    }	}	/* and away we go */	execvp(argv[0], argv);	exit(127);	break;      default:	close(p[1]);	fp = fdopen(p[0], "r");	if (fp == 0) {	    close(p[0]);	    sigaction (SIGCHLD, &oldact, NULL);	    return 0;	}	break;    }    /* non-zero means there's a cmd running */    safe_popen_pid = pid;    return fp;}