-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_cmd.c
99 lines (90 loc) · 2.15 KB
/
ft_cmd.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yajallal < yajallal@student.1337.ma > +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/24 02:20:22 by yajallal #+# #+# */
/* Updated: 2023/01/13 12:24:51 by yajallal ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pipex.h"
char **ft_envp(char **envp)
{
char **paths;
char *dpath;
int i;
dpath = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:.";
i = 0;
if (!envp[i])
{
paths = ft_split(dpath, ':');
return (paths);
}
while (!ft_strnstr(envp[i], "PATH", 4))
i++;
paths = ft_split(envp[i] + 5, ':');
return (paths);
}
char *ft_cmd(char **paths, char *cmd)
{
char *fpath;
char *addslash;
int i;
i = 0;
while (paths[i] && paths)
{
addslash = ft_strjoin(paths[i], "/");
fpath = ft_strjoin(addslash, cmd);
if (access(fpath, F_OK) == 0)
{
ft_free2d(paths, addslash);
return (fpath);
}
ft_free(fpath, addslash);
i++;
}
ft_free2d(paths, NULL);
return (0);
}
char *ft_cmdpath(char *cmd, char **envp)
{
char **paths;
char *fpath;
int i;
i = 0;
if (ft_strchr(cmd, '/'))
{
ft_checkf(cmd, "pipex: no such file or directory:", 0);
return (cmd);
}
paths = ft_envp(envp);
fpath = ft_cmd(paths, cmd);
return (fpath);
}
char **ft_quotes(char *cmd)
{
char c;
char **quotes;
int i;
i = 0;
c = 0;
while (cmd[i])
{
if (cmd[i] == 39 || cmd[i] == 34)
{
c = cmd[i];
break ;
}
i++;
}
if (c != 0)
{
quotes = ft_split(cmd, c);
quotes[0] = ft_strtrim(quotes[0], " ");
}
else
quotes = ft_split(cmd, ' ');
return (quotes);
}