summaryrefslogtreecommitdiffstats
path: root/src/ft_e_externs_next.c
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-04-23 19:02:13 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-04-23 19:02:13 +0200
commit0d4d084859348ecc41ac2b3997ddc2be984636bd (patch)
treeb023549dec6eed6552f423bb821950de36274973 /src/ft_e_externs_next.c
parentok (diff)
download42-minishell-0d4d084859348ecc41ac2b3997ddc2be984636bd.tar.gz
42-minishell-0d4d084859348ecc41ac2b3997ddc2be984636bd.tar.bz2
42-minishell-0d4d084859348ecc41ac2b3997ddc2be984636bd.tar.xz
42-minishell-0d4d084859348ecc41ac2b3997ddc2be984636bd.tar.zst
42-minishell-0d4d084859348ecc41ac2b3997ddc2be984636bd.zip
Parsed bin path in $PATH, now time to execve(2)
Diffstat (limited to '')
-rw-r--r--src/ft_e_externs_next.c106
1 files changed, 106 insertions, 0 deletions
diff --git a/src/ft_e_externs_next.c b/src/ft_e_externs_next.c
new file mode 100644
index 0000000..492d7e5
--- /dev/null
+++ b/src/ft_e_externs_next.c
@@ -0,0 +1,106 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_e_externs_next.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <stddef.h>
+
+#include "ft_f_fail.h"
+#include "ft_s_destroy.h"
+#include "ft_s_lcom.h"
+#include "ft_s_struct.h"
+
+static char
+ *ft_get_fullpath(const char *p_path,
+ const char *d_name,
+ t_msh *msh)
+{
+ char *fullpath;
+ const size_t path_len = ft_strlen(p_path);
+ const size_t name_len = ft_strlen(d_name);
+
+ if (!(fullpath = (char*)malloc((path_len + name_len + 2) * sizeof(char))))
+ {
+ ft_lcom_clear(&msh->curr);
+ ft_s_destroy(msh);
+ ft_fail_alloc();
+ }
+ ft_memcpy(fullpath, p_path, path_len);
+ *(fullpath + (path_len)) = '/';
+ ft_memcpy(fullpath + path_len + 1, d_name, name_len);
+ *(fullpath + (path_len + name_len + 1)) = '\0';
+ return (fullpath);
+}
+
+char
+ *ft_search_in_path(const char *com,
+ char **envpath,
+ t_msh *msh)
+{
+ /* TODO: norme */
+ struct dirent *ent;
+ char **p_path;
+ char *fullpath;
+ DIR *dir;
+
+ (void)com;
+ p_path = envpath;
+ while (*p_path)
+ {
+ if ((dir = opendir(*p_path)) != NULL)
+ {
+ while ((ent = readdir(dir)) != NULL)
+ {
+ /* TODO: check for not bins (dirs, etc) */
+ if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0)
+ {
+ fullpath = ft_get_fullpath(*p_path, ent->d_name, msh);
+ closedir(dir);
+ return (fullpath);
+ }
+ }
+ closedir(dir);
+ }
+ p_path++;
+ }
+ return (NULL);
+}
+
+char
+ **ft_get_env_path(t_msh *msh)
+{
+ char **p_env;
+ char **envpath;
+ char *envline;
+
+ p_env = msh->envp;
+ while (*p_env && ft_strncmp("PATH", *p_env, 4) != 0)
+ {
+ p_env++;
+ }
+ if (*p_env == NULL)
+ return (NULL);
+ envline = ft_strchr(*p_env, '=');
+ envline += 1;
+ if (*envline != '\0')
+ {
+ if (!(envpath = ft_split(envline, ':')))
+ {
+ ft_lcom_clear(&msh->curr);
+ ft_s_destroy(msh);
+ ft_fail_alloc();
+ }
+ return (envpath);
+ }
+ return (NULL);
+}