CS计算机代考程序代写 /* execute.c – code used by small shell to execute commands */

/* execute.c – code used by small shell to execute commands */

#include
#include
#include
#include
#include

int execute(char *argv[])
/*
* purpose: run a program passing it arguments
* returns: status returned via wait, or -1 on error
* errors: -1 on fork() or wait() errors

*/
{
int pid ;
int child_info = -1;

if ( argv[0] == NULL ) /* nothing succeeds */
return 0;

if ( (pid = fork()) == -1 )
perror(“fork”);
else if ( pid == 0 ){
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
execvp(argv[0], argv);
perror(“cannot execute command”);
exit(1);
}
else {
if ( wait(&child_info) == -1 )
perror(“wait”);
}
return child_info;
}