-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec_command.c
More file actions
58 lines (53 loc) · 1.28 KB
/
Copy pathexec_command.c
File metadata and controls
58 lines (53 loc) · 1.28 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
#include "main.h"
int exec_command(char *original)
{
char *path, *tok, *dup, *duplicate, *token, **p, **arr;
char initial_path[20] = "/bin/";
int i, n,k;
pid_t child;
int status, exit_status;
char sep[] = "\n ";
duplicate = strdup(original);
token = _strtok(duplicate, sep);
if (token == NULL)
{
free(duplicate);
return(-1);
}
path = malloc(strlen(token) + strlen(initial_path) + 1);
if (strchr(token, '/') == NULL)
{
k = find_function(token, original);
if (k == 0)
return(0);
strcat(initial_path, token);
path = initial_path;
} else
path = (token);
p = malloc(sizeof(char *) * (strlen(original) + 1));
if (p == NULL)
{
perror("Memory allocation failed\n");
free(duplicate);
free(path);
return(-1);
}
p[0] = path;
for (i = 1; (token = _strtok(NULL, sep)) != NULL; i++)
{
p[i] = token;
}
p[i] = NULL;
// printf("Path: **%s**\n", p[0]);
// if (p[0] != NULL)
// {
// for (i = 0; p[i]; i++)
// {
// printf("p[%d]: **%s**\n", i, p[i]);
// }
// } else
// {
// printf("No arguments\n");
// }
return (_bin(path, p, duplicate));
}