From: Dominik Kaiser Date: Thu, 2 May 2024 14:47:43 +0000 (+0200) Subject: Put env util functions in separate file X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=babfb66a69e285c007699481dcba3661f8ac06f6;p=42%2Fpipex.git Put env util functions in separate file --- diff --git a/include/pipex.h b/include/pipex.h index 1a79cf1..8ec565d 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,14 +6,27 @@ /* By: dkaiser +typedef struct s_pxdata +{ + int in_fd; + int out_fd; + char **cmds; +} t_pxdata; + +char **get_split_path(char *envp[]); +char *get_pwd(char *envp[]); +char *find_in_path(char *cmd, char **path); +char *get_cmd_path(char *cmd, char **path, char *pwd); +t_pxdata *get_pxdata(int argc, char *argv[], char *envp[]); + #endif // PIPEX_H diff --git a/src/env_utils.c b/src/env_utils.c new file mode 100644 index 0000000..6ad5ffc --- /dev/null +++ b/src/env_utils.c @@ -0,0 +1,78 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser #include -static 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, ':')); -} - -static char *get_pwd(char *envp[]) -{ - while (!ft_strnstr(*envp, "PWD=", 4)) - envp++; - if (!*envp) - return (NULL); - return (*envp + 4); -} - -static 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, "/"); - // TODO: Free on fail - cmd_path = ft_strjoin(cur_path, cmd); - free(cur_path); - if (access(cmd_path, X_OK) != -1) - return (cur_path); - path++; - } - return (NULL); -} - -static 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 - { - return (find_in_path(cmd, path)); - } -} - static char **get_cmds(int argc, char *argv[], char *envp[]) { char **cmds;