]> git.dkaiser.de - 42/pipex.git/commitdiff
Add env_utils.c
authorDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 7 May 2024 13:14:34 +0000 (15:14 +0200)
committerDominik Kaiser <dkaiser@1-C-7.42heilbronn.de>
Tue, 7 May 2024 13:14:34 +0000 (15:14 +0200)
Being lazy and stupid at the same time isn't a good idea.

src/env_utils.c [new file with mode: 0644]

diff --git a/src/env_utils.c b/src/env_utils.c
new file mode 100644 (file)
index 0000000..b352d72
--- /dev/null
@@ -0,0 +1,83 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   env_utils.c                                        :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/05/02 16:19:31 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/05/07 15:13:51 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "pipex.h"
+
+char   **get_split_path(char *envp[])
+{
+       char    *path;
+
+       while (!ft_strnstr(*envp, "PATH=", 5))
+               envp++;
+       if (!*envp)
+               return (NULL);
+       path = *envp + 5;
+       return (ft_split(path, ':'));
+}
+
+char   *get_pwd(char *envp[])
+{
+       while (!ft_strnstr(*envp, "PWD=", 4))
+               envp++;
+       if (!*envp)
+               return (NULL);
+       return (*envp + 4);
+}
+
+char   *find_in_path(char *cmd, char **path)
+{
+       char    *cur_path;
+       char    *cmd_path;
+
+       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 (cur_path);
+               path++;
+       }
+       return (NULL);
+}
+
+char   *get_cmd_path(char *cmd, char **path, char *pwd)
+{
+       char    *cur_dir;
+
+       if (cmd[0] == '/')
+               return (cmd); // TODO: Maybe use duplicate instead,
+                       so there will be no problem on free()
+       else if (strchr(cmd, '/'))
+       {
+               cur_dir = ft_strjoin(pwd, "/");
+               // TODO: Free on fail
+               // TODO: Maybe check if executable, else there might be a problem...
+               return (ft_strjoin(cur_dir, cmd));
+       }
+       else if (strchr(cmd, ' '))
+       {
+               //AAAAAAAAAAA
+               return (NULL);
+       }
+       else
+       {
+               return (find_in_path(cmd, path));
+       }
+}