-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_utils.c
More file actions
55 lines (50 loc) · 1.52 KB
/
exec_utils.c
File metadata and controls
55 lines (50 loc) · 1.52 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eduaserr < eduaserr@student.42malaga.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/18 17:09:25 by aamoros- #+# #+# */
/* Updated: 2025/07/01 16:36:23 by eduaserr ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../inc/minishell.h"
static char *find_cmd_path(char **paths, char *cmd)
{
int i;
char *path;
char *temp;
i = 0;
while (paths[i])
{
temp = ft_strjoin(paths[i], "/");
path = ft_strjoin(temp, cmd);
free(temp);
if (access(path, X_OK) == 0)
break ;
free(path);
path = NULL;
i++;
}
return (path);
}
char *get_cmd_paths(char *cmd, char **env)
{
char **paths;
char *path;
int i;
i = 0;
while (env && env[i])
{
if (ft_strncmp(env[i], "PATH=", 5) == 0)
break ;
i++;
}
if (!env[i])
return (NULL);
paths = ft_split(env[i] + 5, ':');
path = find_cmd_path(paths, cmd);
ft_free_array((void **)paths);
return (path);
}