From ca0db4893e765081d91ff9b1aed285eb7aacb107 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Mon, 5 Oct 2020 18:34:26 +0200 Subject: Fixed no shebang scripts --- src/e_pipes_next.c | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) (limited to 'src/e_pipes_next.c') diff --git a/src/e_pipes_next.c b/src/e_pipes_next.c index b39df15..291ba62 100644 --- a/src/e_pipes_next.c +++ b/src/e_pipes_next.c @@ -13,10 +13,12 @@ #include #include #include +#include #include #include #include "e_redirs.h" +#include "e_unshebanged.h" #include "f_fail.h" #include "s_com.h" #include "s_destroy.h" @@ -37,15 +39,33 @@ static void e_fullpath_not_found(t_com *ptr, t_msh *msh) exit(127); } -static void e_execve_failed(const char fullpath[], t_com *ptr, t_msh *msh) +static void e_exec_child(const char fullpath[], t_com *ptr, t_msh *msh) { - f_exec(fullpath, ptr->bin); - u_eof_fd(msh->fd); - s_com_destroy(&msh->com); - s_lpipes_clear(&msh->pipes); - s_line_clear(&msh->curr); - s_destroy(msh); - exit(errno); + char buff[7]; + int32_t fd; + + if ((fd = open(fullpath, O_RDONLY)) != -1) + { + if (read(fd, buff, 7) != -1) + if (ft_strncmp(buff, "\177ELF\002\001\001", 7) != 0 + && ft_strncmp(buff, "#!", 2) != 0) + { + close(fd); + e_extern_read_script(fullpath, ptr, msh, TRUE); + return ; + } + close(fd); + } + if (execve(fullpath, ptr->argv, msh->envp) == -1) + { + f_exec(fullpath, ptr->bin); + u_eof_fd(msh->fd); + s_com_destroy(&msh->com); + s_lpipes_clear(&msh->pipes); + s_line_clear(&msh->curr); + s_destroy(msh); + exit(errno); + } } void e_pipe_child(char fullpath[], uint8_t fp_ret, t_com *ptr, t_msh *msh) @@ -69,8 +89,7 @@ void e_pipe_child(char fullpath[], uint8_t fp_ret, t_com *ptr, t_msh *msh) { if (fp_ret == 2) e_fullpath_not_found(ptr, msh); - else if (execve(fullpath, ptr->argv, msh->envp) == -1) - e_execve_failed(fullpath, ptr, msh); + e_exec_child(fullpath, ptr, msh); } } -- cgit v1.2.3