Beispiel #1
0
/*
 * The trap builtin.
 */
int
trapcmd(int argc __unused, char **argv)
{
	char *action;
	int signo;
	int errors = 0;
	int i;

	while ((i = nextopt("l")) != '\0') {
		switch (i) {
		case 'l':
			printsignals();
			return (0);
		}
	}
	argv = argptr;

	if (*argv == NULL) {
		for (signo = 0 ; signo < sys_nsig ; signo++) {
			if (signo < NSIG && trap[signo] != NULL) {
				out1str("trap -- ");
				out1qstr(trap[signo]);
				if (signo == 0) {
					out1str(" EXIT\n");
				} else if (sys_signame[signo]) {
					out1fmt(" %s\n", sys_signame[signo]);
				} else {
					out1fmt(" %d\n", signo);
				}
			}
		}
		return 0;
	}
	action = NULL;
	if (*argv && sigstring_to_signum(*argv) == -1) {
		if (strcmp(*argv, "-") == 0)
			argv++;
		else {
			action = *argv;
			argv++;
		}
	}
	for (; *argv; argv++) {
		if ((signo = sigstring_to_signum(*argv)) == -1) {
			warning("bad signal %s", *argv);
			errors = 1;
			continue;
		}
		INTOFF;
		if (action)
			action = savestr(action);
		if (trap[signo])
			ckfree(trap[signo]);
		trap[signo] = action;
		if (signo != 0)
			setsignal(signo);
		INTON;
	}
	return errors;
}
Beispiel #2
0
/*
 * The trap builtin.
 */
int
trapcmd(int argc, char **argv)
{
	char *action;
	int signo;

	if (argc <= 1) {
		for (signo = 0 ; signo < _NSIG ; signo++) {
			if (trap[signo] != NULL) {
				if (signo == 0) {
					out1fmt("trap -- '%s' %s\n",
					    trap[signo], "exit");
				} else if (strsigname(signo)) {
					out1fmt("trap -- '%s' %s\n",
					    trap[signo], strsigname(signo));
				} else {
					out1fmt("trap -- '%s' %d\n",
					    trap[signo], signo);
				}
			}
		}
		return 0;
	}
	action = NULL;
	if (*++argv && strcmp(*argv, "--") == 0)
		argv++;
	if (*argv && sigstring_to_signum(*argv) == -1) {
		if ((*argv)[0] != '-') {
			action = *argv;
			argv++;
		} else if ((*argv)[1] == '\0') {
			argv++;
		} else if ((*argv)[1] == 'l' && (*argv)[2] == '\0') {
			printsignals();
			return 0;
		} else {
			error("bad option %s", *argv);
		}
	}
	while (*argv) {
		if ((signo = sigstring_to_signum(*argv)) == -1)
			error("bad signal %s", *argv);
		INTOFF;
		if (action)
			action = savestr(action);
		if (trap[signo])
			ckfree(trap[signo]);
		trap[signo] = action;
		if (signo != 0)
			setsignal(signo);
		INTON;
		argv++;
	}
	return 0;
}