xref: /csrg-svn/lib/libc/stdlib/system.c (revision 36680)
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*36680Sbostic static char sccsid[] = "@(#)system.c	5.4 (Berkeley) 02/05/89";
2035701Sbostic #endif /* LIBC_SCCS and not lint */
2122120Smckusick 
22*36680Sbostic #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 {
30*36680Sbostic 	union wait pstat;
31*36680Sbostic 	pid_t pid, waitpid();
32*36680Sbostic 	int omask;
332035Swnj 
3435701Sbostic 	switch(pid = vfork()) {
3535701Sbostic 	case -1:			/* error */
36*36680Sbostic 		pstat.w_status = 0;
37*36680Sbostic 		pstat.w_retcode = 127;
38*36680Sbostic 		return(pstat.w_status);
3935701Sbostic 	case 0:				/* child */
4035701Sbostic 		execl("/bin/sh", "sh", "-c", command, (char *)NULL);
412035Swnj 		_exit(127);
422035Swnj 	}
43*36680Sbostic 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
44*36680Sbostic 	pid = waitpid(pid, &pstat, 0);
45*36680Sbostic 	(void)sigsetmask(omask);
46*36680Sbostic 	return(pid == -1 ? -1 : pstat.w_status);
472035Swnj }
48