-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_proexec.c
134 lines (127 loc) · 2.98 KB
/
_proexec.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "shell.h"
/**
* temp_function - function that execute the nodes
* @temp: node temporal
* @status: status of program
* @argv: arguments to execute
* @path: path of directory
* Return: status of program
*/
int temp_function(node_t *temp, int *status, char **argv, char *path)
{
int status2 = *status;
DIR *dir = NULL;
struct dirent *fil = NULL;
int len, i;
char *buff = NULL, *slash = "/";
while (temp)
{
dir = opendir(temp->s);
if (dir == NULL)
return (-1);
while ((fil = readdir(dir)))
{
i = _strlen(fil->d_name);
fil->d_name[i] = '\0';
if ((_strcmp(fil->d_name, argv[0])) == 0)
{
len = _strlen(fil->d_name) + _strlen(temp->s) + 3;
buff = malloc(len);
if (buff == NULL)
return (-1);
for (i = 0; i < len; i++)
buff[i] = 0;
buff = _strcat(buff, temp->s), buff = _strcat(buff, slash);
buff = _strcat(buff, fil->d_name);
status2 = execve(buff, argv, environ);
closedir(dir), free(buff), free(path);
return (status2);
}
}
temp = temp->next;
free(dir);
}
return (0);
}
/**
* free_all - Function that frees the malloc
* @argv: Multidimensional string of arguments
* @path: Environment path
* @to: Tokens of path
* @l: input line
* @p: path linked list
* @e: env linked list
* Return: 0 on succes or -1 otherwise
*/
int free_all(char **argv, char *path, char **to, char *l, node_t *e, node_t *p)
{
struct stat st;
int i;
if (stat(argv[0], &st) != 0)
{ free(path);
for (i = 0; to[i]; i++)
{
if (to[i])
free(to[i]);
}
for (i = 0; argv[i]; i++)
{
if (argv[i])
free(argv[i]);
}
free(to);
free(l);
free(argv);
_free_list(p);
_free_list(e);
return (-1);
}
return (0);
}
/**
* _proexec - function that execute the arguments
* @argv: arguments parameters
* @li: input line
* @path_s: path linked list
* @env_s: path linked list
* Return: status or -1 if not succes
*/
int _proexec(char **argv, char *li, node_t **env_s, node_t **path_s)
{
pid_t pid;
int status, i = _strcmp(argv[0], "env"), j, y = 0, k, l, m;
node_t *temp = NULL;
char *path, **tok, *tmp;
j = _strcmp(argv[0], "setenv"), k = _strcmp(argv[0], "getenv");
l = _strcmp(argv[0], "cd"), m = _strcmp(argv[0], "unsetenv");
if (argv[0][0] == '/' || argv[0][1] == '/' || argv[0][2] == '/')
{
pid = fork();
if (pid == 0)
status = execve(argv[0], argv, environ);
if (pid < 0)
perror("./hsh: 1");
else
waitpid(pid, &status, WUNTRACED);
free_all2(argv, li);
return (status);
}
path = _getenv("PATH", env_s), tok = _strtok(path, &y);
if (i == 0 || j == 0 || m == 0 || k == 0 || l == 0)
{
for (i = 0; tok[i]; i++)
free(tok[i]);
free(tok), free(path);
return (0);
}
for (i = 0; tok[i]; i++)
{
if (path[0] == ':' && i == 0)
_add_node_end(path_s, _getenv("PWD", env_s));
_add_node_end(path_s, tok[i]);
}
_add_node_end(path_s, (tmp = _getenv("PWD", env_s)));
temp = *path_s, temp_function(temp, &status, argv, path);
free_all(argv, path, tok, li, *env_s, *path_s), free(tmp);
return (-1);
}