MiniDevil As beautiful as a shell
path.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* path.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2025/12/13 00:32:43 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 08:50:41 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "executor.h"
14 #include "env.h"
15 #include "libft.h"
16 
23 static int is_direct_path(char *cmd)
24 {
25  if (!cmd || !*cmd)
26  return (0);
27  if (ft_strchr(cmd, '/'))
28  return (1);
29  return (0);
30 }
31 
39 static char *join_path(char *dir, char *cmd)
40 {
41  char *path_with_slash;
42  char *full_path;
43 
44  path_with_slash = ft_strjoin(dir, "/");
45  if (!path_with_slash)
46  return (NULL);
47  full_path = ft_strjoin(path_with_slash, cmd);
48  free(path_with_slash);
49  return (full_path);
50 }
51 
61 char *search_in_dir(char *dir, char *cmd)
62 {
63  char *full_path;
64 
65  if (!dir || !*dir)
66  return (NULL);
67  full_path = join_path(dir, cmd);
68  if (!full_path)
69  return (NULL);
70  if (access(full_path, X_OK) == 0)
71  return (full_path);
72  free(full_path);
73  return (NULL);
74 }
75 
85 static char *search_in_path(char *cmd, char *path_var)
86 {
87  char **dirs;
88  char *result;
89  int i;
90 
91  i = 0;
92  result = NULL;
93  dirs = ft_split(path_var, ':');
94  if (!dirs)
95  return (NULL);
96  while (dirs[i] && !result)
97  {
98  result = search_in_dir(dirs[i], cmd);
99  i++;
100  }
101  ft_free_strarray(dirs);
102  return (result);
103 }
104 
115 char *find_cmd_path(char *cmd, t_env *env)
116 {
117  char *path_var;
118 
119  if (!cmd || !*cmd)
120  return (NULL);
121  if (is_direct_path(cmd))
122  {
123  if (access(cmd, F_OK) == 0)
124  return (ft_strdup(cmd));
125  return (NULL);
126  }
127  path_var = get_env_value(env, "PATH");
128  if (!path_var)
129  return (search_in_dir(".", cmd));
130  return (search_in_path(cmd, path_var));
131 }
Environment variable management prototypes.
char * get_env_value(t_env *env_list, char *key)
Look up an environment variable's value by its key.
Executor prototypes for commands, pipes, redirections & heredocs.
char * find_cmd_path(char *cmd, t_env *env)
Resolve a command name into its full executable path.
Definition: path.c:115
static int is_direct_path(char *cmd)
Check if a command contains a slash (= direct path)
Definition: path.c:23
static char * search_in_path(char *cmd, char *path_var)
Search for a command in all PATH directories.
Definition: path.c:85
char * search_in_dir(char *dir, char *cmd)
Search an executable command in a directory.
Definition: path.c:61
static char * join_path(char *dir, char *cmd)
Join a directory and command name with a / in between.
Definition: path.c:39
Environment variable (doubly linked list)
Definition: structs.h:87