system() 函数源码

 1 int system(const char * cmdstring)
2 {
3 pid_t pid;
4 int status;
5
6 if(cmdstring == NULL)
7 {
8 return (1);
9 }
10
11 if ((pid = fork())<0)
12 {
13 status = -1;
14 }
15 else if (pid == 0)
16 {
17 execl("/bin/sh", "sh", "-c", cmdstring, (char *)0);
18 _exit(127);
19 }
20 else
21 {
22 while(waitpid(pid, &status, 0) < 0)
23 {
24 if(errno != EINTR)
25 {
26 status = -1;
27 break;
28 }
29 }
30 }
31 return status;
32 }

posted @ 2011-07-26 09:56  LittleAnt  阅读(853)  评论(0编辑  收藏  举报