-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.c
96 lines (85 loc) · 2.33 KB
/
function.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* function.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahaifoul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 12:54:23 by ahaifoul #+# #+# */
/* Updated: 2021/12/20 10:40:02 by ahaifoul ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
int str_len(char *str)
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
char *get_path(char **envp)
{
char *path_from_envp;
int i;
i = 0;
while (envp[i])
{
path_from_envp = ft_strnstr(envp[i], "PATH=", 6);
if (!path_from_envp)
i++;
else
{
path_from_envp
= ft_substr(path_from_envp, 5, ft_strlen(path_from_envp) - 5);
break ;
}
}
return (path_from_envp);
}
void pars_exec_cmd2(char **av, char **envp)
{
char *path_from_envp;
char **spltd_paths;
char **cmd_options;
char *execve_par;
int i;
path_from_envp = get_path(envp);
cmd_options = ft_split(av[3], ' ');
spltd_paths = ft_split(path_from_envp, ':');
i = 0;
while (spltd_paths[i++])
{
execve_par = ft_strjoin(spltd_paths[i], cmd_options[0]);
execve(execve_par, cmd_options, envp);
free (execve_par);
}
return (perror ("execve error: "));
}
void pars_exec_cmd1(char **av, char **envp)
{
char *path_from_envp;
char **spltd_paths;
char **cmd_options;
char *execve_par;
int i;
path_from_envp = get_path(envp);
cmd_options = ft_split(av[2], ' ');
spltd_paths = ft_split(path_from_envp, ':');
i = 0;
while (spltd_paths[i++])
{
execve_par = ft_strjoin(spltd_paths[i], cmd_options[0]);
execve(execve_par, cmd_options, envp);
free (execve_par);
}
return (perror ("execve error: "));
}
char **spltd(char **envp)
{
char *path_from_envp;
char **spltd;
path_from_envp = get_path(envp);
spltd = ft_split(path_from_envp, ':');
return (spltd);
}