Ejemplo n.º 1
0
char *
expand(char *name)
{
	char xname[BUFSIZ];
	char foldbuf[BUFSIZ];
	register char *cp;
	register int l;
	wordexp_t wrdexp_buf;

	if (debug) fprintf(stderr, "expand(%s)=", name);
	cp = strchr(name, '\0') - 1;	/* pointer to last char of name */
	if (isspace(*cp)) {
		/* strip off trailing blanks */
		while (cp > name && isspace(*cp))
			cp--;
		l = *++cp;	/* save char */
		*cp = '\0';
		name = savestr(name);
		*cp = (char)l;	/* restore char */
	}
	if (name[0] == '+' && getfold(foldbuf) >= 0) {
		snprintf(xname, sizeof (xname), "%s/%s", foldbuf, name + 1);
		cp = safeexpand(savestr(xname));
		if (debug) fprintf(stderr, "%s\n", cp);
		return (cp);
	}
	if (!anyof(name, "~{[*?$`'\"\\")) {
		if (debug) fprintf(stderr, "%s\n", name);
		return (name);
	}
	if (wordexp(name, &wrdexp_buf, WRDE_NOCMD) != 0) {
		fprintf(stderr, gettext("Syntax error in \"%s\"\n"), name);
		fflush(stderr);
		return (NOSTR);
	}
	if (wrdexp_buf.we_wordc > 1) {
		fprintf(stderr, gettext("\"%s\": Ambiguous\n"), name);
		fflush(stderr);
		return (NOSTR);
	}
	if (debug) fprintf(stderr, "%s\n", wrdexp_buf.we_wordv[0]);
	return (savestr(wrdexp_buf.we_wordv[0]));
}
Ejemplo n.º 2
0
char *
expand(char *name)
{
	static char xname[BUFSIZ];
	char cmdbuf[BUFSIZ];
	register int pid, l;
	register char *cp, *Shell;
	int s, pivec[2] /*, (*sigint)()*/;

	if (!anyof(name, "~{[*?$`'\"\\"))
		return(name);
	/* sigint = signal(SIGINT, SIG_IGN); */
	if (pipe(pivec) < 0) {
		warn("pipe");
		/* signal(SIGINT, sigint) */
		return(name);
	}
	snprintf(cmdbuf, sizeof(cmdbuf), "echo %s", name);
	if ((pid = vfork()) == 0) {
		Shell = value(SHELL);
		if (Shell == NULL)
			Shell = _PATH_BSHELL;
		close(pivec[0]);
		close(1);
		dup(pivec[1]);
		close(pivec[1]);
		close(2);
		shell_uid();
		execl(Shell, Shell, "-c", cmdbuf, NULL);
		_exit(1);
	}
	if (pid == -1) {
		warn("fork");
		close(pivec[0]);
		close(pivec[1]);
		return(NULL);
	}
	close(pivec[1]);
	l = read(pivec[0], xname, BUFSIZ);
	close(pivec[0]);
	while (wait(&s) != pid);
		;
	s &= 0377;
	if (s != 0 && s != SIGPIPE) {
		fprintf(stderr, "\"Echo\" failed\n");
		return(NULL);
	}
	if (l < 0) {
		warn("read");
		return(NULL);
	}
	if (l == 0) {
		fprintf(stderr, "\"%s\": No match\n", name);
		return(NULL);
	}
	if (l == BUFSIZ) {
		fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
		return(NULL);
	}
	xname[l] = 0;
	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
		;
	*++cp = '\0';
	return(xname);
}
Ejemplo n.º 3
0
char *
expand(char name[])
{
	static char xname[BUFSIZ];
	char cmdbuf[BUFSIZ];
	int pid, l;
	char *cp, *Shell;
	int s, pivec[2];

	if (!anyof(name, "~{[*?$`'\"\\"))
		return (name);
	if (pipe(pivec) < 0) {
		perror("pipe");
		return (name);
	}
	(void) snprintf(cmdbuf, sizeof (cmdbuf), "echo %s", name);
	if ((pid = vfork()) == 0) {
		userperm();
		Shell = value(SHELL);
		if (Shell == NOSTR)
			Shell = "/bin/sh";
		(void) close(pivec[0]);
		(void) close(1);
		(void) dup(pivec[1]);
		(void) close(pivec[1]);
		(void) close(2);
		(void) execl(Shell, Shell, "-c", cmdbuf, 0);
		_exit(1);
	}
	if (pid == -1) {
		perror("fork");
		(void) close(pivec[0]);
		(void) close(pivec[1]);
		return (NOSTR);
	}
	(void) close(pivec[1]);
	l = read(pivec[0], xname, BUFSIZ);
	(void) close(pivec[0]);
	while (wait(&s) != pid)
		;
	s &= 0377;
	if (s != 0 && s != SIGPIPE) {
		(void) fprintf(stderr, "\"Echo\" failed\n");
		return (NOSTR);
	}
	if (l < 0) {
		perror("read");
		return (NOSTR);
	}
	if (l == 0) {
		(void) fprintf(stderr, "\"%s\": No match\n", name);
		return (NOSTR);
	}
	if (l == BUFSIZ) {
		(void) fprintf(stderr, "Buffer overflow expanding \"%s\"\n",
		    name);
		return (NOSTR);
	}
	xname[l] = 0;
	for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
		;
	*++cp = '\0';
	return (xname);
}