-
Notifications
You must be signed in to change notification settings - Fork 0
/
Black.c
58 lines (54 loc) · 1.47 KB
/
Black.c
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
#include "Black.h"
int execute_bin(char * cmd, char* path){
int id, i;
char *flags[5], *ptr, *command;
int argc = 0;
if((command = (char*)malloc(sizeof(char) * BUFF))==NULL){
printf("Allocation failure\n");
return -1;
}
cmd[strlen(cmd)-1] = '\0';
ptr = strtok(cmd, " ");
while(ptr != NULL){
if((flags[argc++] = strIncrease(ptr)) == NULL){
for(i=0; i<argc; i++)
free(flags[i]);
free(command);
printf("Allocation failure\n");
return -1;
}
ptr = strtok(NULL, " ");
} //remember to free flags[i]
flags[argc] = NULL;
if((id = fork()) == -1){
perror("Failed on creating new process\n");
for(i=0; i<argc; i++)
free(flags[i]);
free(command);
}
else if(id == 0){ //this is child process
strcpy(command, path); //build command path
strcat(command, flags[0]);
execv(command, flags);
if(errno == ENOENT)
printf("Not Supported\n");
else
perror("execv: ");
for(i=0; i<argc; i++)
free(flags[i]);
free(command);
return -1;
}
else
wait();
for(i=0; i<argc; i++)
free(flags[i]);
free(command);
return 1;
}
char *strIncrease(char *str){
char *other = (char*)malloc(sizeof(char) *(strlen(str)+1));
if(other != NULL)
strcpy(other, str);
return other;
}