示例#1
0
文件: main.c 项目: ryo/netbsd-src
int
main(int argc, char *argv[])
{

    return ls_main(argc, argv);
    /* NOTREACHED */
}
示例#2
0
int main(int argc,char *argv[])
{
    ls_main(argc, argv);


    //已经把问题1 3 4 解决 1.3版
}
示例#3
0
文件: main.c 项目: radixo/openbsd-src
int
main(int argc, char *argv[])
{
	return ls_main(argc, argv);
}
示例#4
0
FILE *
ftpd_popen(char *program, char *type)
{
	char *cp;
	FILE *iop;
	int argc, gargc, pdes[2], pid;
	char **pop, *argv[MAXUSRARGS], *gargv[MAXGLOBARGS];

	if (((*type != 'r') && (*type != 'w')) || type[1])
		return (NULL);

	if (!pids) {
		if ((fds = getdtablesize()) <= 0)
			return (NULL);
		if ((pids = malloc(fds * sizeof(int))) == NULL)
			return (NULL);
		memset(pids, 0, fds * sizeof(int));
	}
	if (pipe(pdes) < 0)
		return (NULL);

	/* break up string into pieces */
	for (argc = 0, cp = program; argc < MAXUSRARGS; cp = NULL) {
		if (!(argv[argc++] = strtok(cp, " \t\n")))
			break;
	}
	argv[argc - 1] = NULL;

	/* glob each piece */
	gargv[0] = argv[0];
	for (gargc = argc = 1; argv[argc] && gargc < (MAXGLOBARGS-1); argc++) {
		glob_t gl;
		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;

		memset(&gl, 0, sizeof(gl));
		gl.gl_matchc = MAXGLOBARGS;
		flags |= GLOB_LIMIT;
		if (glob(argv[argc], flags, NULL, &gl))
			gargv[gargc++] = strdup(argv[argc]);
		else if (gl.gl_pathc > 0) {
			for (pop = gl.gl_pathv; *pop && gargc < (MAXGLOBARGS-1);
			     pop++)
				gargv[gargc++] = strdup(*pop);
		}
		globfree(&gl);
	}
	gargv[gargc] = NULL;

	iop = NULL;
	fflush(NULL);
	pid = (strcmp(gargv[0], _PATH_LS) == 0) ? fork() : vfork();
	switch(pid) {
	case -1:			/* error */
		(void)close(pdes[0]);
		(void)close(pdes[1]);
		goto pfree;
		/* NOTREACHED */
	case 0:				/* child */
		if (*type == 'r') {
			if (pdes[1] != STDOUT_FILENO) {
				dup2(pdes[1], STDOUT_FILENO);
				(void)close(pdes[1]);
			}
			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
			(void)close(pdes[0]);
		} else {
			if (pdes[0] != STDIN_FILENO) {
				dup2(pdes[0], STDIN_FILENO);
				(void)close(pdes[0]);
			}
			(void)close(pdes[1]);
		}
		/* Drop privileges before proceeding */
		if (getuid() != geteuid() && setuid(geteuid()) < 0)
			_exit(1);
		if (strcmp(gargv[0], _PATH_LS) == 0) {
			/* Reset getopt for ls_main() */
			optreset = optind = optopt = 1;
			/* Close syslogging to remove pwd.db missing msgs */
			closelog();
			/* Trigger to sense new /etc/localtime after chroot */
			if (getenv("TZ") == NULL) {
				setenv("TZ", "", 0);
				tzset();
				unsetenv("TZ");
				tzset();
			}
			exit(ls_main(gargc, gargv));
		}
		execv(gargv[0], gargv);
		_exit(1);
	}
	/* parent; assume fdopen can't fail...  */
	if (*type == 'r') {
		iop = fdopen(pdes[0], type);
		(void)close(pdes[1]);
	} else {
		iop = fdopen(pdes[1], type);
		(void)close(pdes[0]);
	}
	pids[fileno(iop)] = pid;

pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
		free(gargv[argc]);

	return (iop);
}
示例#5
0
FILE *
ftpd_popen(char *argv[], const char *ptype, int stderrfd)
{
	FILE *iop;
	int argc, pdes[2], pid, isls;
	char **pop;
	StringList *sl;

	iop = NULL;
	isls = 0;
	if ((*ptype != 'r' && *ptype != 'w') || ptype[1])
		return (NULL);

	if (!pids) {
		if ((fds = getdtablesize()) <= 0)
			return (NULL);
		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
			return (NULL);
		memset(pids, 0, fds * sizeof(int));
	}
	if (pipe(pdes) < 0)
		return (NULL);

	if ((sl = sl_init()) == NULL)
		goto pfree;

					/* glob each piece */
	if (sl_add(sl, ftpd_strdup(argv[0])) == -1)
		goto pfree;
	for (argc = 1; argv[argc]; argc++) {
		glob_t gl;
		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE|GLOB_LIMIT;

		memset(&gl, 0, sizeof(gl));
		if (glob(argv[argc], flags, NULL, &gl)) {
			if (sl_add(sl, ftpd_strdup(argv[argc])) == -1) {
				globfree(&gl);
				goto pfree;
			}
		} else {
			for (pop = gl.gl_pathv; *pop; pop++) {
				if (sl_add(sl, ftpd_strdup(*pop)) == -1) {
					globfree(&gl);
					goto pfree;
				}
			}
		}
		globfree(&gl);
	}
	if (sl_add(sl, NULL) == -1)
		goto pfree;

#ifndef NO_INTERNAL_LS
	isls = (strcmp(sl->sl_str[0], INTERNAL_LS) == 0);
#endif

	pid = isls ? fork() : vfork();
	switch (pid) {
	case -1:			/* error */
		(void)close(pdes[0]);
		(void)close(pdes[1]);
		goto pfree;
		/* NOTREACHED */
	case 0:				/* child */
		if (*ptype == 'r') {
			if (pdes[1] != STDOUT_FILENO) {
				dup2(pdes[1], STDOUT_FILENO);
				(void)close(pdes[1]);
			}
			if (stderrfd == -1)
				(void)close(STDERR_FILENO);
			else
				dup2(stderrfd, STDERR_FILENO);
			(void)close(pdes[0]);
		} else {
			if (pdes[0] != STDIN_FILENO) {
				dup2(pdes[0], STDIN_FILENO);
				(void)close(pdes[0]);
			}
			(void)close(pdes[1]);
		}
#ifndef NO_INTERNAL_LS
		if (isls) {	/* use internal ls */
			optreset = optind = optopt = 1;
			closelog();
			exit(ls_main(sl->sl_cur - 1, sl->sl_str));
		}
#endif

		execv(sl->sl_str[0], sl->sl_str);
		_exit(1);
	}
	/* parent; assume fdopen can't fail...  */
	if (*ptype == 'r') {
		iop = fdopen(pdes[0], ptype);
		(void)close(pdes[1]);
	} else {
		iop = fdopen(pdes[1], ptype);
		(void)close(pdes[0]);
	}
	pids[fileno(iop)] = pid;

 pfree:
	if (sl)
		sl_free(sl, 1);
	return (iop);
}
示例#6
0
FILE *
ftpd_popen (char *program, const char *type)
{
  char *cp;
  FILE *iop;
  struct file_pid *fpid;
  int argc, gargc, pdes[2], pid;
  char **pop, *argv[MAX_ARGC], *gargv[MAX_GARGC];

  if (((*type != 'r') && (*type != 'w')) || type[1])
    return (NULL);

  if (pipe (pdes) < 0)
    return (NULL);

  /* break up string into pieces */
  for (argc = 0, cp = program; argc < MAX_ARGC - 1; cp = NULL, argc++)
    if (!(argv[argc] = strtok (cp, " \t\n")))
      break;
  argv[MAX_ARGC - 1] = NULL;

  /* glob each piece */
  gargv[0] = argv[0];
  for (gargc = argc = 1; argv[argc]; argc++)
    {
      glob_t gl;
      int flags = GLOB_NOCHECK;

#ifdef GLOB_BRACE
      flags |= GLOB_BRACE;
#endif
#ifdef GLOB_QUOTE
      flags |= GLOB_QUOTE;
#endif
#ifdef GLOB_TILDE
      flags |= GLOB_TILDE;
#endif

      memset (&gl, 0, sizeof (gl));
      if (glob (argv[argc], flags, NULL, &gl))
	gargv[gargc++] = strdup (argv[argc]);
      else
	for (pop = gl.gl_pathv; *pop; pop++)
	  gargv[gargc++] = strdup (*pop);
      globfree (&gl);
    }
  gargv[gargc] = NULL;

  iop = NULL;

#ifdef WITH_LIBLS
  /* Do not use vfork() for internal ls.  */
  pid = (strcmp (gargv[0], "/bin/ls") == 0) ? fork () : vfork ();
  switch (pid)
#else
  switch (pid = vfork ())
#endif
    {
    case -1:			/* error */
      close (pdes[0]);
      close (pdes[1]);
      goto pfree;
    case 0:			/* child */
      if (*type == 'r')
	{
	  if (pdes[1] != STDOUT_FILENO)
	    {
	      dup2 (pdes[1], STDOUT_FILENO);
	      close (pdes[1]);
	    }
	  dup2 (STDOUT_FILENO, STDERR_FILENO);	/* stderr too! */
	  close (pdes[0]);
	}
      else
	{
	  if (pdes[0] != STDIN_FILENO)
	    {
	      dup2 (pdes[0], STDIN_FILENO);
	      close (pdes[0]);
	    }
	  close (pdes[1]);
	}

#ifdef WITH_LIBLS
      /* mvo: should this be a config-option? */
      if (strcmp (gargv[0], "/bin/ls") == 0)
	{
	  optind = 0;
	  exit (ls_main (gargc, gargv));
	}
#endif

      execv (gargv[0], gargv);
      _exit (1);
    }
  /* parent; assume fdopen can't fail...  */
  if (*type == 'r')
    {
      iop = fdopen (pdes[0], type);
      close (pdes[1]);
    }
  else
    {
      iop = fdopen (pdes[1], type);
      close (pdes[0]);
    }

  fpid = (struct file_pid *) malloc (sizeof (struct file_pid));
  if (fpid)
    {
      fpid->file = iop;
      fpid->pid = pid;
      fpid->next = file_pids;
      file_pids = fpid;
    }

pfree:for (argc = 1; gargv[argc] != NULL; argc++)
    free (gargv[argc]);

  return (iop);
}