xref: /csrg-svn/lib/libc/stdlib/system.c (revision 42123)
135701Sbostic /*
235701Sbostic  * Copyright (c) 1988 The Regents of the University of California.
335701Sbostic  * All rights reserved.
435701Sbostic  *
535701Sbostic  * Redistribution and use in source and binary forms are permitted
635701Sbostic  * provided that the above copyright notice and this paragraph are
735701Sbostic  * duplicated in all such forms and that any documentation,
835701Sbostic  * advertising materials, and other materials related to such
935701Sbostic  * distribution and use acknowledge that the software was developed
1035701Sbostic  * by the University of California, Berkeley.  The name of the
1135701Sbostic  * University may not be used to endorse or promote products derived
1235701Sbostic  * from this software without specific prior written permission.
1335701Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435701Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535701Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1635701Sbostic  */
1735701Sbostic 
1826603Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*42123Sbostic static char sccsid[] = "@(#)system.c	5.7 (Berkeley) 05/16/90";
2035701Sbostic #endif /* LIBC_SCCS and not lint */
2122120Smckusick 
2236680Sbostic #include <sys/types.h>
2335701Sbostic #include <sys/wait.h>
2435701Sbostic #include <sys/signal.h>
25*42123Sbostic #include <stddef.h>
2639192Sbostic #include <paths.h>
272035Swnj 
2835701Sbostic system(command)
2935701Sbostic 	char *command;
302035Swnj {
3136680Sbostic 	union wait pstat;
3236680Sbostic 	pid_t pid, waitpid();
3339192Sbostic 	int omask;
3439192Sbostic 	sig_t intsave, quitsave;
352035Swnj 
36*42123Sbostic 	if (!command)		/* just checking... */
37*42123Sbostic 		return(1);
38*42123Sbostic 
3936688Skarels 	omask = sigblock(sigmask(SIGCHLD));
4035701Sbostic 	switch(pid = vfork()) {
4135701Sbostic 	case -1:			/* error */
4236688Skarels 		(void)sigsetmask(omask);
4336680Sbostic 		pstat.w_status = 0;
4436680Sbostic 		pstat.w_retcode = 127;
4536680Sbostic 		return(pstat.w_status);
4635701Sbostic 	case 0:				/* child */
4736688Skarels 		(void)sigsetmask(omask);
4839192Sbostic 		execl(_PATH_BSHELL, "sh", "-c", command, (char *)NULL);
492035Swnj 		_exit(127);
502035Swnj 	}
5139192Sbostic 	intsave = signal(SIGINT, SIG_IGN);
5239192Sbostic 	quitsave = signal(SIGQUIT, SIG_IGN);
5336680Sbostic 	pid = waitpid(pid, &pstat, 0);
5436680Sbostic 	(void)sigsetmask(omask);
5539192Sbostic 	(void)signal(SIGINT, intsave);
5639192Sbostic 	(void)signal(SIGQUIT, quitsave);
5736680Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
582035Swnj }
59