maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
utils_1.c File Reference
#include "libft.h"
#include "env.h"
#include "execute.h"
Include dependency graph for utils_1.c:

Go to the source code of this file.

Functions

char * ft_find_path (char *cmd, t_env *envp)
 function find the full path of the executed command
 
static void relative_path_handle (char *cmd, char *path, char **tmp_full_path)
 

Function Documentation

◆ ft_find_path()

char * ft_find_path ( char *  cmd,
t_env envp 
)

function find the full path of the executed command

--

  • Parameters
    cmdstring from the first argument
  • Parameters
    envpstructure with environment variables
  • Returns
    char* returns the full path of the command TODO: rewrite ft_find_path for working full path when $PATH deleted

Definition at line 32 of file utils_1.c.

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}
char * get_env(t_env *envp, const char *key)
Definition env.c:59
void ft_free_2d_arr(char **arr)
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
static void relative_path_handle(char *cmd, char *path, char **tmp_full_path)
Definition utils_1.c:57

References ft_free_2d_arr(), ft_split(), ft_strdup(), ft_strncmp(), get_env(), and relative_path_handle().

Referenced by child_process().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ relative_path_handle()

static void relative_path_handle ( char *  cmd,
char *  path,
char **  tmp_full_path 
)
static

Definition at line 57 of file utils_1.c.

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 * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23

References ft_strjoin().

Referenced by ft_find_path().

Here is the call graph for this function:
Here is the caller graph for this function: