xref: /csrg-svn/lib/libc/stdlib/system.c (revision 42635)
135701Sbostic /*
235701Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335701Sbostic  * All rights reserved.
435701Sbostic  *
5*42635Sbostic  * %sccs.include.redist.c%
635701Sbostic  */
735701Sbostic 
826603Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*42635Sbostic static char sccsid[] = "@(#)system.c	5.9 (Berkeley) 06/01/90";
1035701Sbostic #endif /* LIBC_SCCS and not lint */
1122120Smckusick 
1236680Sbostic #include <sys/types.h>
1342138Sbostic #include <sys/signal.h>
1435701Sbostic #include <sys/wait.h>
1542138Sbostic #include <stdlib.h>
1642123Sbostic #include <stddef.h>
1739192Sbostic #include <paths.h>
182035Swnj 
1935701Sbostic system(command)
2035701Sbostic 	char *command;
212035Swnj {
2236680Sbostic 	union wait pstat;
2336680Sbostic 	pid_t pid, waitpid();
2439192Sbostic 	int omask;
2539192Sbostic 	sig_t intsave, quitsave;
262035Swnj 
2742123Sbostic 	if (!command)		/* just checking... */
2842123Sbostic 		return(1);
2942123Sbostic 
3036688Skarels 	omask = sigblock(sigmask(SIGCHLD));
3135701Sbostic 	switch(pid = vfork()) {
3235701Sbostic 	case -1:			/* error */
3336688Skarels 		(void)sigsetmask(omask);
3436680Sbostic 		pstat.w_status = 0;
3536680Sbostic 		pstat.w_retcode = 127;
3636680Sbostic 		return(pstat.w_status);
3735701Sbostic 	case 0:				/* child */
3836688Skarels 		(void)sigsetmask(omask);
3939192Sbostic 		execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
402035Swnj 		_exit(127);
412035Swnj 	}
4239192Sbostic 	intsave = signal(SIGINT, SIG_IGN);
4339192Sbostic 	quitsave = signal(SIGQUIT, SIG_IGN);
4436680Sbostic 	pid = waitpid(pid, &pstat, 0);
4536680Sbostic 	(void)sigsetmask(omask);
4639192Sbostic 	(void)signal(SIGINT, intsave);
4739192Sbostic 	(void)signal(SIGQUIT, quitsave);
4836680Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
492035Swnj }
50