summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-10-05 18:34:26 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-10-05 18:34:26 +0200
commitca0db4893e765081d91ff9b1aed285eb7aacb107 (patch)
tree34bbafb158929c1e632af3dfe70c4f256a3e0a05
parentTODO (diff)
download42-minishell-ca0db4893e765081d91ff9b1aed285eb7aacb107.tar.gz
42-minishell-ca0db4893e765081d91ff9b1aed285eb7aacb107.tar.bz2
42-minishell-ca0db4893e765081d91ff9b1aed285eb7aacb107.tar.xz
42-minishell-ca0db4893e765081d91ff9b1aed285eb7aacb107.tar.zst
42-minishell-ca0db4893e765081d91ff9b1aed285eb7aacb107.zip
Fixed no shebang scripts
-rw-r--r--Makefile1
-rw-r--r--TODO.org3
-rw-r--r--src/e_externs.c20
-rw-r--r--src/e_pipes_next.c39
-rw-r--r--src/e_unshebanged.c75
-rw-r--r--src/e_unshebanged.h25
6 files changed, 151 insertions, 12 deletions
diff --git a/Makefile b/Makefile
index 3f5a74f..6880e17 100644
--- a/Makefile
+++ b/Makefile
@@ -39,6 +39,7 @@ SRCS_NAME += e_line
SRCS_NAME += e_pipes
SRCS_NAME += e_pipes_next
SRCS_NAME += e_redirs
+SRCS_NAME += e_unshebanged
SRCS_NAME += f_alloc
SRCS_NAME += f_chdir
SRCS_NAME += f_com
diff --git a/TODO.org b/TODO.org
index dd550ae..6912e36 100644
--- a/TODO.org
+++ b/TODO.org
@@ -29,7 +29,8 @@
** DONE [#B] forked write(2) stuff on cd
** DONE [#B] Multiline && ||
** DONE [#B] ./qwe.sh <=== handle bad shebang
-** TODO [#B] msh ~> ./qwe.sh <---- without shebang (maybe works fine already)
+** DONE [#B] msh ~> ./qwe.sh <---- without shebang (maybe works fine already)
+ CLOSED: [2020-10-05 Mon 18:34]
** TODO [#B] 'qweqwe=qweqwe'
** DONE [#C] fix that goddamn exit(2) 25 leak
** DONE [#C] lpipes leaks on builtins (same as above man)
diff --git a/src/e_externs.c b/src/e_externs.c
index a3d3594..7b25516 100644
--- a/src/e_externs.c
+++ b/src/e_externs.c
@@ -15,6 +15,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
@@ -23,6 +24,7 @@
#include "d_define.h"
#include "f_fail.h"
#include "e_redirs.h"
+#include "e_unshebanged.h"
#include "s_com.h"
#include "s_destroy.h"
#include "s_line.h"
@@ -32,6 +34,21 @@
static void e_extern_child(const char fullpath[], t_com *ptr, t_msh *msh)
{
+ 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, FALSE);
+ return ;
+ }
+ close(fd);
+ }
if (execve(fullpath, ptr->argv, msh->envp) == -1)
{
f_exec(fullpath, ptr->bin);
@@ -103,7 +120,8 @@ void e_extern(t_com *ptr, t_msh *msh)
uint8_t fp_ret;
fullpath[0] = C_NUL;
- if (ft_ischarset("./", ptr->bin[0]) == TRUE)
+ if (ptr->bin != NULL && (ft_ischarset("./", ptr->bin[0]) == TRUE
+ || ft_strchr(ptr->bin, '/') != NULL))
{
ft_strlcpy(fullpath, ptr->bin, PATH_MAX);
e_exec_path(fullpath, ptr, 0, msh);
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 <libft.h>
#include <stdlib.h>
#include <stdint.h>
+#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#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);
}
}
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);
+ }
+}
diff --git a/src/e_unshebanged.h b/src/e_unshebanged.h
new file mode 100644
index 0000000..d9499ef
--- /dev/null
+++ b/src/e_unshebanged.h
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* e_unshebanged.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* 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 */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_E_UNSHEBANGED_H
+#define FT_E_UNSHEBANGED_H
+
+#include <libft.h>
+
+#include "s_struct.h"
+
+void e_extern_read_script(const char fullpath[],
+ t_com *ptr,
+ t_msh *msh,
+ t_bool pipe);
+
+#endif