diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-15 19:59:41 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-15 19:59:41 +0200 |
commit | ca2e36781039eb7e9901ccde395600e7af87ff4f (patch) | |
tree | 8a9d85433e943fbbd079bc55700e048a54d9bf24 | |
parent | type builtin fix (diff) | |
download | 42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.gz 42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.bz2 42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.xz 42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.tar.zst 42-minishell-ca2e36781039eb7e9901ccde395600e7af87ff4f.zip |
Huge fixes and stack stuff
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/b_type.c | 35 | ||||
-rw-r--r-- | src/d_define.h | 1 | ||||
-rw-r--r-- | src/e_externs.c | 36 | ||||
-rw-r--r-- | src/e_externs_next.c | 102 | ||||
-rw-r--r-- | src/e_externs_pipes.c | 13 | ||||
-rw-r--r-- | src/f_exec.c | 22 | ||||
-rw-r--r-- | src/f_exec.h | 2 | ||||
-rw-r--r-- | src/m_loop.c | 29 | ||||
-rw-r--r-- | src/p_line.c | 21 | ||||
-rw-r--r-- | src/u_path.c | 72 | ||||
-rw-r--r-- | src/u_path.h (renamed from src/e_externs_next.h) | 14 | ||||
-rw-r--r-- | src/u_utils.c | 2 | ||||
-rw-r--r-- | src/u_vars.c | 18 | ||||
-rw-r--r-- | src/u_vars.h | 3 |
15 files changed, 176 insertions, 196 deletions
@@ -34,7 +34,6 @@ SRCS_NAME += b_type SRCS_NAME += b_unset SRCS_NAME += e_builtins SRCS_NAME += e_externs -SRCS_NAME += e_externs_next SRCS_NAME += e_externs_pipes SRCS_NAME += e_line SRCS_NAME += e_pipes @@ -75,6 +74,7 @@ SRCS_NAME += p_lblock_next SRCS_NAME += p_split SRCS_NAME += u_alias SRCS_NAME += u_parse +SRCS_NAME += u_path SRCS_NAME += u_utils SRCS_NAME += u_vars SRCS_NAME += u_vars_next diff --git a/src/b_type.c b/src/b_type.c index c09f441..d8c6cef 100644 --- a/src/b_type.c +++ b/src/b_type.c @@ -15,11 +15,13 @@ #include <dirent.h> #include <fcntl.h> #include <unistd.h> +#include <limits.h> +#include "d_define.h" #include "f_fail.h" #include "s_line.h" #include "s_struct.h" -#include "e_externs_next.h" +#include "u_path.h" #include "u_utils.h" static int8_t absolute_path_exists(char com[]) @@ -40,46 +42,33 @@ static int8_t absolute_path_exists(char com[]) return (0); } -static char *type_get_path(char com[], t_msh *msh) +static void type_get_path(char fullpath[], char com[], t_msh *msh) { - char **envpath; - char *fullpath; - - envpath = NULL; - fullpath = NULL; if (ft_ischarset("/.", com[0]) == TRUE) { if (absolute_path_exists(com)) { - if (!(fullpath = ft_strdup(com))) - { - f_alloc_and_destroy_msh(msh); - } - return (fullpath); + ft_strlcpy(fullpath, com, PATH_MAX); + return ; } - return (NULL); - } - else if ((envpath = get_env_path(msh)) != NULL) - { - fullpath = search_in_path(com, envpath, msh); - ft_delwords(envpath); + return ; } - return (fullpath); + u_search_in_path(fullpath, com, PATH_MAX, msh); } static uint8_t b_check_nonbuilt(char *ptr, uint8_t ret, t_msh *msh) { - char *fullpath; + char fullpath[PATH_MAX]; - fullpath = type_get_path(ptr, msh); - if (fullpath != NULL) + fullpath[0] = C_NUL; + type_get_path(fullpath, ptr, msh); + if (fullpath[0] != C_NUL) ft_printf("%s is %s\n", ptr, fullpath); else { ft_printf("minishell: type: %s: not found\n", ptr); ret = 1; } - ft_memdel((void*)&fullpath); return (ret); } diff --git a/src/d_define.h b/src/d_define.h index 1709bea..e0ca360 100644 --- a/src/d_define.h +++ b/src/d_define.h @@ -71,6 +71,7 @@ #define C_SUB 0x1a #define C_ESC 0x1b #define C_DQUOTE 0x22 +#define C_SHARP 0x23 #define C_DOLLAR 0x24 #define C_SQUOTE 0x27 #define C_AMP 0x26 diff --git a/src/e_externs.c b/src/e_externs.c index 253d758..8761986 100644 --- a/src/e_externs.c +++ b/src/e_externs.c @@ -16,15 +16,18 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <limits.h> #include <errno.h> #include "b_export_next.h" -#include "e_externs_next.h" +#include "d_define.h" #include "f_fail.h" #include "m_redirs.h" +#include "s_com.h" #include "s_destroy.h" #include "s_line.h" #include "s_struct.h" +#include "u_path.h" #include "u_utils.h" static void @@ -34,9 +37,9 @@ static void { if (execve(fullpath, ptr->argv, msh->envp) == -1) { - f_exec(fullpath); - ft_memdel((void*)&fullpath); + f_exec(fullpath, ptr->bin); u_eof_fd(msh->fd); + s_com_destroy(&msh->com); s_line_clear(&msh->curr); s_destroy(msh); exit(errno); @@ -83,33 +86,20 @@ static void } } -void - e_extern(t_com *ptr, - t_msh *msh) +void e_extern(t_com *ptr, t_msh *msh) { - char **envpath; - char *fullpath; + char fullpath[PATH_MAX]; + fullpath[0] = C_NUL; if (ft_ischarset("./", ptr->bin[0]) == TRUE) { - if ((fullpath = ft_strdup(ptr->bin)) == NULL) - return ; + ft_strlcpy(fullpath, ptr->bin, PATH_MAX); e_exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); return ; } - else if ((envpath = get_env_path(msh)) != NULL) + else { - fullpath = search_in_path(ptr->bin, envpath, msh); - ft_delwords(envpath); - if (fullpath == NULL) - { - f_fail_command_not_found(ptr->bin, msh); - } - else - { - e_exec_path(fullpath, ptr, msh); - ft_memdel((void*)&fullpath); - } + u_search_in_path(fullpath, ptr->bin, PATH_MAX, msh); + e_exec_path(fullpath, ptr, msh); } } diff --git a/src/e_externs_next.c b/src/e_externs_next.c deleted file mode 100644 index ed3ee84..0000000 --- a/src/e_externs_next.c +++ /dev/null @@ -1,102 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* 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 "f_fail.h" -#include "s_destroy.h" -#include "s_line.h" -#include "s_struct.h" -#include "u_utils.h" - -static char - *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)))) - { - f_alloc_and_destroy_msh(msh); - } - (void)ft_memcpy(fullpath, p_path, path_len); - *(fullpath + (path_len)) = '/'; - (void)ft_memcpy(fullpath + path_len + 1, d_name, name_len); - *(fullpath + (path_len + name_len + 1)) = '\0'; - return (fullpath); -} - -char - *search_in_path(const char com[], - char *envpath[], - t_msh *msh) -{ - struct dirent *ent; - char **p_path; - char *fullpath; - DIR *dir; - - 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 = get_fullpath(*p_path, ent->d_name, msh); - closedir(dir); - return (fullpath); - } - } - closedir(dir); - } - p_path++; - } - return (NULL); -} - -char - **get_env_path(t_msh *msh) -{ - size_t i; - char **env_dup; - char **envpath; - char *envline; - - env_dup = u_get_env_var_names(msh); - i = 0; - while (env_dup[i] && ft_strncmp("PATH", env_dup[i], 5) != 0) - i++; - if (env_dup[i] == NULL) - return (NULL); - ft_delwords(env_dup); - envline = ft_strchr(msh->envp[i], '='); - envline += 1; - if (*envline != '\0') - { - if ((envpath = ft_split(envline, ':')) == NULL) - { - f_alloc_and_destroy_msh(msh); - } - return (envpath); - } - return (NULL); -} diff --git a/src/e_externs_pipes.c b/src/e_externs_pipes.c index e6252e4..05a2cf7 100644 --- a/src/e_externs_pipes.c +++ b/src/e_externs_pipes.c @@ -16,16 +16,17 @@ #include <stdint.h> #include <unistd.h> #include <string.h> +#include <limits.h> #include <errno.h> #include "d_define.h" -#include "e_externs_next.h" #include "f_fail.h" #include "m_redirs.h" #include "s_destroy.h" #include "s_line.h" #include "s_lpipes.h" #include "s_struct.h" +#include "u_path.h" #include "u_utils.h" static uint8_t @@ -143,8 +144,8 @@ void { struct s_lpipes *head; struct s_lpipes *rptr; - char **envpath; char **fullpath; + char tmp[PATH_MAX]; size_t i; size_t pipes; uint8_t bu_id; @@ -163,7 +164,7 @@ void if ((fullpath[i] = ft_strdup(rptr->com->bin)) == NULL) f_alloc_and_destroy_msh(msh); } - else if ((envpath = get_env_path(msh)) != NULL) + else { if ((bu_id = get_builtin_id(rptr->com->bin, msh)) < FT_BUILTINS_COUNT) @@ -172,8 +173,10 @@ void f_alloc_and_destroy_msh(msh); } else - fullpath[i] = search_in_path(rptr->com->bin, envpath, msh); - ft_delwords(envpath); + { + u_search_in_path(tmp, rptr->com->bin, PATH_MAX, msh); + fullpath[i] = ft_strdup(tmp); + } } i++; rptr = rptr->next; diff --git a/src/f_exec.c b/src/f_exec.c index fccfdea..37cb0f2 100644 --- a/src/f_exec.c +++ b/src/f_exec.c @@ -15,13 +15,23 @@ #include <errno.h> #include <unistd.h> +#include "d_define.h" #include "s_struct.h" -void - f_exec(const char fullpath[]) +void f_exec(const char fullpath[], const char bin[]) { - ft_dprintf(STDERR_FILENO, - "minishell: %s: %s\n", - fullpath, - strerror(errno)); + if (fullpath[0] != C_NUL) + { + ft_dprintf(STDERR_FILENO, + "minishell: %s: %s\n", + fullpath, + strerror(errno)); + } + else + { + ft_dprintf(STDERR_FILENO, + "minishell: %s: %s\n", + bin, + strerror(errno)); + } } diff --git a/src/f_exec.h b/src/f_exec.h index 17f3484..1ea0683 100644 --- a/src/f_exec.h +++ b/src/f_exec.h @@ -15,6 +15,6 @@ #include "s_struct.h" -void f_exec(const char fullpath[]); +void f_exec(const char fullpath[], const char bin[]); #endif diff --git a/src/m_loop.c b/src/m_loop.c index 50357eb..41c9e36 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -25,6 +25,8 @@ #include "s_com.h" #include "s_lpipes.h" #include "s_line.h" +#include "u_utils.h" +#include "u_parse.h" #include "u_vars.h" static void @@ -93,6 +95,32 @@ static void } } +static void m_delete_comments(char line[]) +{ + char *ptr; + t_quote_mode mode; + + ptr = line; + mode = Q_NONE; + while (*ptr != C_NUL) + { + if (*ptr == C_DQUOTE) + mode = u_meet_dquote(line, ptr, mode); + else if (*ptr == C_SQUOTE) + mode = u_meet_squote(line, ptr, mode); + else if (mode == Q_NONE && *ptr == C_SHARP + && u_is_not_escaped(line, ptr) == TRUE) + { + if (ptr - line == 0) + *ptr = C_NUL; + else if (ptr - line > 0 && ft_iswhitespace(*(ptr - 1)) == TRUE + && u_is_not_escaped(line, ptr - 1) == TRUE) + *ptr = C_NUL; + } + ptr++; + } +} + uint8_t m_loop(int32_t fd, t_msh *msh) { @@ -107,6 +135,7 @@ uint8_t if (fd == STDIN_FILENO) m_prompt_psx(1, msh); gnl = get_next_line(fd, &line); + m_delete_comments(line); if (line[0] != C_NUL) { line = m_check_multi_backslash(fd, line, msh); diff --git a/src/p_line.c b/src/p_line.c index cdb2fd4..c8381c8 100644 --- a/src/p_line.c +++ b/src/p_line.c @@ -21,26 +21,6 @@ #include "s_struct.h" #include "u_utils.h" -static void - p_delete_comments(char line[]) -{ - char *ptr; - - ptr = line; - while (*ptr != '\0') - { - if (*ptr == '#') - { - if ((ptr - line) == 0 || ft_iswhitespace(*(ptr - 1)) == TRUE) - { - *ptr = '\0'; - return ; - } - } - ptr++; - } -} - static t_bool p_check_whitespaces_only(char line[]) { @@ -61,7 +41,6 @@ static t_bool void p_line(char line[], t_msh *msh) { - p_delete_comments(line); if (p_check_whitespaces_only(line) == TRUE) { return ; diff --git a/src/u_path.c b/src/u_path.c new file mode 100644 index 0000000..b43946f --- /dev/null +++ b/src/u_path.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* u_path.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 <stddef.h> +#include <dirent.h> +#include <limits.h> + +#include "s_struct.h" +#include "u_vars.h" + +static void u_get_fullpath(char fullpath[], + const char p_path[], + const char d_name[], + size_t dstsize) +{ + const size_t path_len = ft_strlen(p_path); + const size_t name_len = ft_strlen(d_name); + + fullpath[0] = C_NUL; + if (path_len + name_len < dstsize) + { + (void)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'; + } +} + +uint8_t u_search_in_path(char fullpath[], + const char com[], + size_t dstsize, + t_msh *msh) +{ + struct dirent *ent; + char tmp[ARG_MAX]; + char *tok_path; + DIR *dir; + + if (u_get_var_value(tmp, "$PATH", ARG_MAX, msh) != 0) + return (1); + tok_path = ft_strtok(tmp, ":"); + while (tok_path != NULL) + { + if ((dir = opendir(tok_path)) != NULL) + { + while ((ent = readdir(dir)) != NULL) + { + if (ft_strncmp(com, ent->d_name, ft_strlen(com) + 1) == 0) + { + u_get_fullpath(fullpath, tok_path, ent->d_name, dstsize); + closedir(dir); + if (fullpath[0] == C_NUL) + return (1); + return (0); + } + } + closedir(dir); + } + tok_path = ft_strtok(NULL, ":"); + } + return (1); +} diff --git a/src/e_externs_next.h b/src/u_path.h index db03bac..0c8e256 100644 --- a/src/e_externs_next.h +++ b/src/u_path.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* e_externs_next.h :+: :+: :+: */ +/* u_path.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,12 +10,16 @@ /* */ /* ************************************************************************** */ -#ifndef E_EXTERNS_NEXT_H -#define E_EXTERNS_NEXT_H +#ifndef U_PATH_H +#define U_PATH_H + +#include <stdint.h> #include "s_struct.h" -char **get_env_path(t_msh *msh); -char *search_in_path(const char com[], char *envpath[], t_msh *msh); +uint8_t u_search_in_path(char fullpath[], + const char com[], + size_t dstsize, + t_msh *msh); #endif diff --git a/src/u_utils.c b/src/u_utils.c index dd1634b..4e332b3 100644 --- a/src/u_utils.c +++ b/src/u_utils.c @@ -70,7 +70,7 @@ uint64_t uint64_t argc; argc = 0; - while (args[argc]) + while (args[argc] != NULL) { argc++; } diff --git a/src/u_vars.c b/src/u_vars.c index 95c6ffa..97fc580 100644 --- a/src/u_vars.c +++ b/src/u_vars.c @@ -98,22 +98,26 @@ void ** varname[] wasn't found. */ -void - u_get_var_value(char str[], - const char varname[], - size_t dstsize, - t_msh *msh) +uint8_t u_get_var_value(char str[], + const char varname[], + size_t dstsize, + t_msh *msh) { str[0] = C_NUL; u_get_special_var(str, varname, dstsize, msh); if (str[0] != C_NUL) { - return ; + return (0); } u_get_custom_var(str, varname, dstsize, msh); if (str[0] != C_NUL) { - return ; + return (0); } u_get_frm_env(str, varname, dstsize, msh); + if (str[0] != C_NUL) + { + return (0); + } + return (1); } diff --git a/src/u_vars.h b/src/u_vars.h index 27c975f..c76518e 100644 --- a/src/u_vars.h +++ b/src/u_vars.h @@ -13,6 +13,7 @@ #ifndef U_VARS_H #define U_VARS_H +#include <stdint.h> #include <stddef.h> #include "s_struct.h" @@ -21,7 +22,7 @@ void u_get_custom_var(char str[], const char varname[], size_t dstsize, t_msh *msh); -void u_get_var_value(char str[], +uint8_t u_get_var_value(char str[], const char varname[], size_t dstsize, t_msh *msh); |