system函数
system 函数能够执行函数中参数的命令,实现如下:
- /* system 函数 */
- #include <stdlib.h>
- int system(const char *cmdstring);
- /* system 函数的实现 */
- #include "apue.h"
- #include <sys/wait.h>
- int system(const char *cmdstring)
- {
- pid_t pid;
- int status;
- if(NULL == cmdstring)
- return -1;
- if((pid = fork()) < 0)
- status = -1;
- else if(0 == pid)
- {
- execl("/bin/sh", "sh", "-c", "cmdstring", (char *)0);
- _exit(127);
- }
- else
- {
- while(waitpid(pid,&status,0) < 0)
- {
- if(errno != EINTR)
- {
- status = -1;
- break;
- }
- }
- }
- return status;
- }
- 如果 fork 失败或者 waitpid 返回值除 EINTR 之外的出错,则 system 返回-1,而且 errno 中设置了错误类型值;
- 如果 exec 失败(表示不能执行 shell),其返回值如同 shell 执行 exit(127)一样;
- 否则所有三个函数(fork、exec、waitpid)都执行成功,并且system 的返回值是 shell 的终止状态;
- #include <sys/wait.h>
- #include "apue.h"
- void pr_exit(int status)
- {
- if(WIFEXITED(status))
- printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
- else if(WIFSIGNALED(status))
- printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status),
- #ifdef WCOREDUMP
- WCOREDUMP(status) ? "(core file generated)" : " ");
- #else
- " ");
- #endif
- else if(WIFSTOPPED(status))
- printf("child stoped, signal number = %d\n", WSTOPSIG(status));
- }
- int main(void)
- {
- int status;
- if((status = system("date")) < 0)
- err_sys("system error");
- pr_exit(status);
- if((status = system("nosuchcommand")) < 0)
- err_sys("system error");
- pr_exit(status);
- if((status = system("stat;who; exit 44")) < 0)
- err_sys("system error");
- pr_exit(status);
- exit(0);
- }
- Fri Nov 7 19:19:17 CST 2014
- normal termination, exit status = 0
- sh: nosuchcommand: command not found
- normal termination, exit status = 127
- stat: missing operand
- Try `stat --help' for more information.
- nifengweijifen tty7 2014-11-07 08:44
- nifengweijifen pts/1 2014-11-07 08:57 (:0.0)
- nifengweijifen pts/4 2014-11-07 14:44 (:0.0)
- normal termination, exit status = 44
- #include <sys/wait.h>
- #include "apue.h"
- void pr_exit(int status)
- {
- if(WIFEXITED(status))
- printf("normal termination, exit status = %d\n", WEXITSTATUS(status));
- else if(WIFSIGNALED(status))
- printf("abnormal termination, signal number = %d%s\n", WTERMSIG(status),
- #ifdef WCOREDUMP
- WCOREDUMP(status) ? "(core file generated)" : " ");
- #else
- " ");
- #endif
- else if(WIFSTOPPED(status))
- printf("child stoped, signal number = %d\n", WSTOPSIG(status));
- }
- int main(int argc, char *argv[])
- {
- int status;
- if(argc != 2)
- {
- printf("usage: a.out <cmdstring>\n");
- exit(1);
- }
- status = system(argv[1]);
- pr_exit(status);
- exit(0);
- }
- Fri Nov 7 19:28:43 CST 2014
- normal termination, exit status = 0