diff options
| author | Christopher Uhlig | 2025-01-22 16:50:05 +0100 |
|---|---|---|
| committer | Christopher Uhlig | 2025-01-22 16:50:05 +0100 |
| commit | ad5eae00584c6c4a88e873b85645c10c47cd7bf8 (patch) | |
| tree | 77d00c9f815d26b05594fe75c11e364b3568e3ec /src/get_cmd_path.c | |
| parent | f6e474d27a1398c6d4f2e88c7f2d3797b85217da (diff) | |
| download | minishell-ad5eae00584c6c4a88e873b85645c10c47cd7bf8.tar.gz minishell-ad5eae00584c6c4a88e873b85645c10c47cd7bf8.zip | |
uiuiuiui
Diffstat (limited to 'src/get_cmd_path.c')
| -rw-r--r-- | src/get_cmd_path.c | 115 |
1 files changed, 50 insertions, 65 deletions
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 713c397..01f24b1 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/22 00:01:48 by chuhlig ### ########.fr */ +/* Updated: 2025/01/22 16:17:27 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,92 +61,77 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) return (result); } -void free_split_path(char **path) +static char *find_in_path(char *cmd, t_env *env, int *return_code) { - char **tmp = path; - while (*tmp) - { - free(*tmp); - tmp++; - } - free(path); -} + char *cur_path; + char *cmd_path; + char **path; -char **get_split_path(t_env *env) -{ - char *path_env; - char **split_path; - path_env = env_get(env, "PATH"); - if (!path_env) - return (NULL); - split_path = ft_split(path_env, ':'); - free(path_env); - return (split_path); + path = get_split_path(env); + cmd_path = NULL; + while (*path) + { + if (cmd_path) + free(cmd_path); + cur_path = ft_strjoin(*path, "/"); + if (!cur_path) + return (NULL); + cmd_path = ft_strjoin(cur_path, cmd); + free(cur_path); + if (!cmd_path) + return (NULL); + if (access(cmd_path, X_OK) != -1) + return (cmd_path); + path++; + } + *return_code = 127; + free(cmd_path); + command_not_found_error(cmd); + return (NULL); } -static char *find_in_path(char *cmd, t_env *env, int *return_code) -{ - char *cur_path; - char *cmd_path; - char **path; - char **path_start; // To keep track of the start of the path array - - path = get_split_path(env); - path_start = path; // Save the start of the path array - cmd_path = NULL; - while (*path) - { - if (cmd_path) - free(cmd_path); - cur_path = ft_strjoin(*path, "/"); - if (!cur_path) - { - free_split_path(path_start); // Free the entire path array - return (NULL); - } - cmd_path = ft_strjoin(cur_path, cmd); - free(cur_path); - if (!cmd_path) - { - free_split_path(path_start); // Free the entire path array - return (NULL); - } - if (access(cmd_path, X_OK) != -1) - { - free_split_path(path_start); // Free the entire path array - return (cmd_path); - } - path++; - } - free_split_path(path_start); // Free the entire path array - *return_code = 127; - command_not_found_error(cmd); - return (NULL); -} +// static char *get_simple_cmd_path(char *cmd, int *return_code) +// { +// char *result; +// result = ft_strdup(cmd); +// if (!result) +// return (NULL); +// if (access(result, F_OK) == -1) +// { +// free(result); +// return (error(ENOENT, cmd, 127, return_code)); +// } +// if (access(result, X_OK) == -1) +// { +// free(result); +// return (error(EACCES, cmd, 126, return_code)); +// } +// if (is_directory(cmd)) +// { +// free(result); +// return (error(EISDIR, cmd, 126, return_code)); +// } +// return (result); +// } static char *get_simple_cmd_path(char *cmd, int *return_code) { char *result; result = ft_strdup(cmd); - if (!result) - return (NULL); if (access(result, F_OK) == -1) { free(result); return (error(ENOENT, cmd, 127, return_code)); } - if (access(result, X_OK) == -1) + else if (access(result, X_OK) == -1) { free(result); return (error(EACCES, cmd, 126, return_code)); } if (is_directory(cmd)) - { - free(result); return (error(EISDIR, cmd, 126, return_code)); - } return (result); } |
