diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-16 21:09:14 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-16 21:09:14 +0200 |
commit | c8ba75c31c949d3b5e5ba44e389550ea10096c5d (patch) | |
tree | 3fd61cd13f2ba9452c49cc9b48449c2458580454 /src/m_argv.c | |
parent | Todo update (diff) | |
download | 42-minishell-c8ba75c31c949d3b5e5ba44e389550ea10096c5d.tar.gz 42-minishell-c8ba75c31c949d3b5e5ba44e389550ea10096c5d.tar.bz2 42-minishell-c8ba75c31c949d3b5e5ba44e389550ea10096c5d.tar.xz 42-minishell-c8ba75c31c949d3b5e5ba44e389550ea10096c5d.tar.zst 42-minishell-c8ba75c31c949d3b5e5ba44e389550ea10096c5d.zip |
Leak fix
Diffstat (limited to 'src/m_argv.c')
-rw-r--r-- | src/m_argv.c | 34 |
1 files changed, 30 insertions, 4 deletions
diff --git a/src/m_argv.c b/src/m_argv.c index 98a8e3e..bdebd23 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -10,7 +10,9 @@ /* */ /* ************************************************************************** */ +#include <sys/stat.h> #include <libft.h> +#include <stdlib.h> #include <stdint.h> #include <fcntl.h> #include <unistd.h> @@ -22,25 +24,49 @@ #include "m_loop.h" #include "m_mshrc.h" #include "s_struct.h" +#include "u_vars.h" -static void - m_read_script(char *const argv[], t_msh *msh) +static char *m_get_prev_hist(t_msh *msh) +{ + struct stat sb; + char *hist; + char histfile[PATH_MAX]; + int32_t fd; + + hist = NULL; + u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh); + if (histfile[0] == C_NUL) + return (NULL); + if ((fd = open(histfile, O_RDONLY)) == -1) + return (NULL); + if (stat(histfile, &sb) == -1) + return (NULL); + if ((hist = (char*)malloc(sb.st_size * sizeof(char))) == NULL) + return (NULL); + if (read(fd, hist, sb.st_size) == -1) + return (NULL); + return (hist); +} + +static void m_read_script(char *const argv[], t_msh *msh) { int32_t fd; if ((fd = open(*(argv + 1), O_RDONLY)) == -1) f_open_file(*(argv + 1), msh); + msh->fd = fd; msh->ret = m_loop(fd, msh); close(fd); } -uint8_t - m_argv(int argc, char *const argv[], t_msh *msh) +uint8_t m_argv(int argc, char *const argv[], t_msh *msh) { if (argc == 1) { m_init_custom_vars(msh); + msh->prev_hist = m_get_prev_hist(msh); msh->ret = m_source_mshrc(msh); + msh->fd = STDIN_FILENO; msh->ret = m_loop(STDIN_FILENO, msh); } else if (argc > 1 && ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3) == 0) |