summaryrefslogtreecommitdiffstats
path: root/src/e_unshebanged.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/e_unshebanged.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/e_unshebanged.c b/src/e_unshebanged.c
new file mode 100644
index 0000000..3fd8fac
--- /dev/null
+++ b/src/e_unshebanged.c
@@ -0,0 +1,75 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* e_unshebanged.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 <stdlib.h>
+#include <unistd.h>
+#include <limits.h>
+#include <errno.h>
+
+#include "f_fail.h"
+#include "s_struct.h"
+#include "s_com.h"
+#include "s_destroy.h"
+#include "s_line.h"
+#include "s_lpipes.h"
+#include "u_utils.h"
+
+static char **e_get_new_argv(const char fullpath[], t_com *ptr, t_msh *msh)
+{
+ char **argv;
+ size_t i;
+
+ if ((argv = (char**)malloc((3 +
+ u_builtins_get_argc((const char**)ptr->argv)) * sizeof(char*))) == NULL)
+ f_alloc_and_destroy_msh(msh);
+ if ((argv[0] = ft_strdup(msh->argv[0])) == NULL
+ || (argv[1] = ft_strdup(fullpath)) == NULL)
+ {
+ ft_memdel((void*)&argv);
+ f_alloc_and_destroy_msh(msh);
+ }
+ i = 2;
+ while (ptr->argv[i - 2] != NULL)
+ {
+ if ((argv[i] = ft_strdup(ptr->argv[i - 2])) == NULL)
+ {
+ ft_memdel((void*)&argv);
+ f_alloc_and_destroy_msh(msh);
+ }
+ i++;
+ }
+ argv[i] = NULL;
+ return (argv);
+}
+
+void e_extern_read_script(const char fullpath[],
+ t_com *ptr,
+ t_msh *msh,
+ t_bool pipe)
+{
+ char **argv;
+
+ argv = e_get_new_argv(fullpath, ptr, msh);
+ if (execve(msh->argv[0], (char* const *)argv, msh->envp) == -1)
+ {
+ f_exec(msh->argv[0], argv[0]);
+ ft_delwords(argv);
+ u_eof_fd(msh->fd);
+ s_com_destroy(&msh->com);
+ if (pipe == TRUE)
+ s_lpipes_clear(&msh->pipes);
+ s_line_clear(&msh->curr);
+ s_destroy(msh);
+ exit(errno);
+ }
+}