-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpath_utils.c
83 lines (76 loc) · 2.15 KB
/
path_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
/* ************************************************************************** */
/* */
/* :::::::: */
/* path_utils.c :+: :+: */
/* +:+ */
/* By: sde-rijk <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/12/13 10:15:58 by sde-rijk #+# #+# */
/* Updated: 2022/01/24 14:45:33 by dnoom ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "Libft/libft.h"
#include <stdio.h>
static void ft_write_error_msg(char *str, t_env *s_env);
void ft_try_paths(char **paths, char **args, t_env *s_env)
{
char *cmd;
int i;
i = 0;
if (!args || !args[i])
return ;
while (paths[i])
{
if (!ft_strchr(args[0], '/'))
cmd = ft_strjoin(paths[i], args[0]);
else
cmd = args[0];
if (!access(cmd, F_OK) && !access(cmd, X_OK))
{
close(s_env->term_in);
close(s_env->term_out);
execve(cmd, args, s_env->env);
}
if (!ft_strchr(args[0], '/'))
free(cmd);
i++;
}
ft_write_error_msg(args[0], s_env);
exit(127);
}
char **ft_get_paths(char **env)
{
char **paths;
char *path;
char *line;
int i;
i = 0;
line = NULL;
while (!line)
{
line = ft_strnstr(env[i], "PATH=", 5);
i++;
if (!env[i])
paths = ft_calloc(sizeof(*paths), 1);
if (!env[i])
return (paths);
}
path = ft_substr(line, 5, ft_strlen(line) - 5);
paths = ft_split(path, ':');
free(path);
i = 0;
while (paths[i])
ft_strjoin_free(&paths[i++], "/");
return (paths);
}
static void ft_write_error_msg(char *str, t_env *s_env)
{
ft_putstr_fd(SHELL_NAME, 2);
if (isatty(s_env->term_in))
ft_putstr_fd(": ", 2);
else
ft_print_line_nr(s_env->line_nr);
ft_putstr_fd(str, 2);
ft_putstr_fd(": command not found\n", 2);
}