xref: /csrg-svn/lib/libc/stdlib/system.c (revision 36688)
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*36688Skarels static char sccsid[] = "@(#)system.c	5.5 (Berkeley) 02/06/89";
2035701Sbostic #endif /* LIBC_SCCS and not lint */
2122120Smckusick 
2236680Sbostic #include <sys/types.h>
2335701Sbostic #include <sys/wait.h>
2435701Sbostic #include <sys/signal.h>
2535701Sbostic #include <stdio.h>
262035Swnj 
2735701Sbostic system(command)
2835701Sbostic 	char *command;
292035Swnj {
3036680Sbostic 	union wait pstat;
3136680Sbostic 	pid_t pid, waitpid();
32*36688Skarels 	int omask, (*i)(), (*q)();
332035Swnj 
34*36688Skarels 	omask = sigblock(sigmask(SIGCHLD));
3535701Sbostic 	switch(pid = vfork()) {
3635701Sbostic 	case -1:			/* error */
37*36688Skarels 		(void)sigsetmask(omask);
3836680Sbostic 		pstat.w_status = 0;
3936680Sbostic 		pstat.w_retcode = 127;
4036680Sbostic 		return(pstat.w_status);
4135701Sbostic 	case 0:				/* child */
42*36688Skarels 		(void)sigsetmask(omask);
4335701Sbostic 		execl("/bin/sh", "sh", "-c", command, (char *)NULL);
442035Swnj 		_exit(127);
452035Swnj 	}
46*36688Skarels 	i = signal(SIGINT, SIG_IGN);
47*36688Skarels 	q = signal(SIGQUIT, SIG_IGN);
4836680Sbostic 	pid = waitpid(pid, &pstat, 0);
4936680Sbostic 	(void)sigsetmask(omask);
50*36688Skarels 	(void)signal(SIGINT, i);
51*36688Skarels 	(void)signal(SIGQUIT, q);
5236680Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
532035Swnj }
54