aboutsummaryrefslogtreecommitdiff
path: root/src/get_cmd_path.c
diff options
context:
space:
mode:
authorChristopher Uhlig2025-01-13 11:06:54 +0100
committerChristopher Uhlig2025-01-13 11:06:54 +0100
commit78dc50a2bce3c6e31405437189e2990d8fc720ac (patch)
treed61d9f0d279e191c1bfb34929908f412cf58c02d /src/get_cmd_path.c
parentae5512ea0d6d8be833ca3a9b39f93239109f45b4 (diff)
downloadminishell-78dc50a2bce3c6e31405437189e2990d8fc720ac.tar.gz
minishell-78dc50a2bce3c6e31405437189e2990d8fc720ac.zip
here
Diffstat (limited to 'src/get_cmd_path.c')
-rw-r--r--src/get_cmd_path.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c
new file mode 100644
index 0000000..8a27584
--- /dev/null
+++ b/src/get_cmd_path.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* get_cmd_path.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */
+/* Updated: 2024/12/17 19:20:08 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "minishell.h"
+
+static char *get_absolute_cmd_path(char *cmd, t_env *env);
+static char *find_in_path(char *cmd, t_env *env);
+char **get_split_path(t_env *env);
+
+char *get_cmd_path(char *cmd, t_env *env)
+{
+ if (cmd[0] == '/')
+ return (ft_strdup(cmd));
+ else if (ft_strchr(cmd, '/'))
+ return (get_absolute_cmd_path(cmd, env));
+ else
+ return (find_in_path(cmd, env));
+}
+
+static char *get_absolute_cmd_path(char *cmd, t_env *env)
+{
+ char *cur_dir;
+ char *result;
+
+ cur_dir = ft_strjoin(env_get(env, "PWD"), "/");
+ if (!cur_dir)
+ return (NULL);
+ result = ft_strjoin(cur_dir, cmd);
+ free(cur_dir);
+ if (!result)
+ return (NULL);
+ if (access(result, X_OK) == -1)
+ {
+ free(result);
+ return (NULL);
+ }
+ return (result);
+}
+
+static char *find_in_path(char *cmd, t_env *env)
+{
+ char *cur_path;
+ char *cmd_path;
+ char **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 (NULL);
+}
+
+char **get_split_path(t_env *env)
+{
+ char *path;
+
+ path = env_get(env, "PATH");
+ return (ft_split(path, ':'));
+}