maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils_1.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* utils_1.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/07/11 18:11:06 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 13:00:02 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14#include "env.h"
15#include "execute.h"
16
17char *ft_find_path(char *cmd, t_env *envp);
18static void relative_path_handle(char *cmd, char *path, char **tmp_full_path);
19
32char *ft_find_path(char *cmd, t_env *envp)
33{
34 char **path;
35 char *tmp_full_path;
36 int i;
37
38 path = ft_split(get_env(envp, "PATH"), ':');
39 i = -1;
40 while (path[++i] != NULL)
41 {
42 if (ft_strncmp(cmd, "/", 1) != 0)
43 relative_path_handle(cmd, path[i], &tmp_full_path);
44 else
45 tmp_full_path = ft_strdup(cmd);
46 if (access(tmp_full_path, F_OK) == 0)
47 {
48 ft_free_2d_arr(path);
49 return (tmp_full_path);
50 }
51 free(tmp_full_path);
52 }
53 ft_free_2d_arr(path);
54 return (NULL);
55}
56
57static void relative_path_handle(char *cmd, char *path, char **tmp_full_path)
58{
59 char *tmp_slash;
60
61 tmp_slash = ft_strjoin(path, "/");
62 *tmp_full_path = ft_strjoin(tmp_slash, cmd);
63 free(tmp_slash);
64}
char * get_env(t_env *envp, const char *key)
Definition env.c:59
void ft_free_2d_arr(char **arr)
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23
int ft_strncmp(const char *s1, const char *s2, size_t n)
Definition ft_strncmp.c:24
char ** ft_split(char const *s, char c)
Definition ft_split.c:95
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
Definition env.h:17
char * ft_find_path(char *cmd, t_env *envp)
function find the full path of the executed command
Definition utils_1.c:32
static void relative_path_handle(char *cmd, char *path, char **tmp_full_path)
Definition utils_1.c:57