-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem.c
More file actions
50 lines (48 loc) · 973 Bytes
/
system.c
File metadata and controls
50 lines (48 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*************************************************************************
> File Name: system.c
> Author: jianghechao
> Mail: 591378033@qq.com
> Created Time: Sun 04 May 2014 08:28:36 PM CST
************************************************************************/
#include<stdio.h>
#include<errno.h>
#include"apue.h"
int system(const char *cmdstring)
{
pid_t pid;
int status;
if(cmdstring==NULL)
return 1;
if((pid=fork())<0)
{
perror("fork error");
status = -1;
}else if(pid==0)
{
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;
}
int main()
{
int status;
if((status=system("date"))<0)
perror("system error");
pr_exit(status);
if((status=system("nosuchcommand"))<0)
perror("system error");
pr_exit(status);
if((status=system("who;exit 44"))<0)
perror("system error");
pr_exit(status);
return 0;
}