-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute.c
More file actions
75 lines (75 loc) · 1.84 KB
/
Copy pathexecute.c
File metadata and controls
75 lines (75 loc) · 1.84 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/utsname.h>
#include<unistd.h>
#include<dirent.h>
#include <sys/types.h>
#include<sys/wait.h>
#include<signal.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
void execute(char ** split){
int temp=0;
for(int i=0;i<10;i++){
if(strlen(split[i])!=0){
temp++;
}
}
char ** process = (char **)malloc(temp*sizeof(char *));
for(int i=0;i<=temp;i++){
process[i]=(char *)malloc(100*sizeof(char));
}
for(int i=0;i<temp;i++){
strcpy(process[i],split[i]);
int t = strlen(process[i]);
if(process[i][t-1]=='\n')
process[i][t-1]='\0';
}
int is_background=0;
if(strcmp(process[temp-1],"&")==0){
is_background=1;
process[temp-1]=NULL;
}
process[temp]=NULL;
int status = 0;
pid_t child = fork();
if(!is_background){
if(child == 0){
if(execvp(process[0],process)<0){
perror("");
}
}
else{
waitpid(child, &status, 0);
}
}
else{
if(child==0){
int sub;
pid_t grandchild = fork();
if(grandchild==0){
if(execvp(process[0],process)<0){
perror("");
}
}
else{
char * path = process[0];
waitpid(grandchild,&sub,0);
printf("\n%s with pid %d exited ",path,grandchild-1);
int local = waitpid(grandchild,&sub,1);
if(WIFEXITED(sub)){
printf("%s\n",strerror(WEXITSTATUS(sub)));
}
else{
printf("abrupt\n");
}
}
}
else{
printf("%d\n",child);
}
}
return;
}