《Unix/Linux编程实践教程》之Shell编程二(2)

execute.c:

1: /* execute.c - code used by small shell to execute commands */ 2: 3: #include <stdio.h> 4: #include <stdlib.h> 5: #include <unistd.h> 6: #include <signal.h> 7: #include 8: 9: int execute(char *argv[]) 10: /* 11: * purpose: run a program passing it arguments 12: * returns: status returned via wait, or -1 on error 13: * errors: -1 on fork() or wait() errors 14: 15: */ 16: { 17: int pid ; 18: int child_info = -1; 19: 20: if ( argv[0] == NULL ) /* nothing succeeds */ 21: return 0; 22: 23: if ( (pid = fork()) == -1 ) 24: perror("fork"); 25: else if ( pid == 0 ){ 26: signal(SIGINT, SIG_DFL); 27: signal(SIGQUIT, SIG_DFL); 28: execvp(argv[0], argv); 29: perror("cannot execute command"); 30: exit(1); 31: } 32: else { 33: if ( wait(&child_info) == -1 ) 34: perror("wait"); 35: } 36: return child_info; 37: } 38:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/9eef0418e6794d0e3eca78c0c8543688.html