-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_path.c
78 lines (71 loc) · 2.03 KB
/
parsing_path.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kmatjuhi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/31 08:23:22 by kmatjuhi #+# #+# */
/* Updated: 2024/03/07 15:44:01 by kmatjuhi ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
static char *ft_sstrjoin(char *s1, char *s2)
{
char *dest;
int i;
i = 0;
dest = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 4) * sizeof(char));
if (!dest)
return (NULL);
while (s1[i])
{
dest[i] = s1[i];
i++;
}
dest[i++] = '/';
while (*s2)
dest[i++] = *s2++;
dest[i] = '\0';
free(s1);
return (dest);
}
static char **parsing_to_env(char *path, char *str, int *fd)
{
char **path_to_env;
int i;
i = 0;
path_to_env = ft_split(path, ':');
free(path);
if (!path_to_env)
custom_msg("", "malloc failed\n", fd, NULL);
while (path_to_env[i])
{
path_to_env[i] = ft_sstrjoin(path_to_env[i], str);
if (!path_to_env[i])
{
ft_free_arr(path_to_env);
custom_msg("", "malloc failed\n", fd, NULL);
}
i++;
}
return (path_to_env);
}
char **parsing_path(char **envp, char *str, int *fd)
{
char **path_to_env;
char *path;
int i;
i = 0;
path = NULL;
while (envp[i] != NULL)
{
if (ft_strncmp(envp[i], "PATH=", 5) == 0)
path = ft_substr(envp[i], 5, ft_strlen(envp[i]));
i++;
}
if (!path)
custom_msg(str, ": No such file or directory\n", fd, NULL);
path_to_env = parsing_to_env(path, str, fd);
return (path_to_env);
}