-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
94 lines (84 loc) · 1.98 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rrangwan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 13:22:26 by rrangwan #+# #+# */
/* Updated: 2022/03/14 13:09:32 by rrangwan ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void ft_error(char *s, int e)
{
perror(s);
exit (e);
}
int ft_path_sub(char **envp)
{
int i;
int stop;
i = 0;
stop = 0;
while (envp[i] && !stop)
{
if (envp[i][0] == 'P' && envp[i][1] == 'A' && envp[i][2] == 'T')
stop = 1;
i++;
}
return (i - 1);
}
char *ft_path(char *cmd, char **envp)
{
int i;
char **path;
char *ret_a;
char *ret;
i = ft_path_sub(envp);
path = ft_split(envp[i] + 5, ':');
i = 0;
while (path[i])
{
ret_a = ft_strjoin(path[i], "/");
ret = ft_strjoin(ret_a, cmd);
free (ret_a);
if (access(ret, F_OK) == 0)
return (ret);
free(ret);
i++;
}
i = 0;
while (path[i])
free(path[i++]);
free(path);
return (cmd);
}
int ft_strlen(const char *str)
{
size_t i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_strjoin(char const *s1, char const *s2)
{
char *str;
size_t i;
size_t j;
if (!s1)
return (NULL);
str = (char *)malloc(sizeof(*s1) * (ft_strlen(s1) + ft_strlen(s2) + 1));
if (!str)
return (NULL);
i = 0;
j = 0;
while (s1[i])
str[j++] = s1[i++];
i = 0;
while (s2[i])
str[j++] = s2[i++];
str[j] = '\0';
return (str);
}