From ea17acaa1d3ab5decc1ec3888868a48d59436438 Mon Sep 17 00:00:00 2001 From: narnaud Date: Tue, 17 May 2022 10:01:27 +0200 Subject: [PATCH] fix binaries search --- Makefile | 2 +- caller.c | 17 +++++++---------- utils_2.c => errors.c | 14 ++++++++++++-- lexer_2.c | 12 +++++------- libft/Makefile | 2 +- libft/libft.h | 3 ++- libft/str/ft_join_with.c | 27 +++++++++++++++++++++++++++ minishell.h | 19 +++++++++---------- utils_1.c => utils.c | 0 9 files changed, 64 insertions(+), 32 deletions(-) rename utils_2.c => errors.c (83%) mode change 100755 => 100644 create mode 100644 libft/str/ft_join_with.c rename utils_1.c => utils.c (100%) diff --git a/Makefile b/Makefile index 6e9af5d..9f0836e 100755 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ NAME = minishell LIBFT = libft.a SRCS = minishell.c lexer.c lexer_2.c lexer_3.c parser.c caller.c -SRCS += built-in.c env.c utils_1.c utils_2.c +SRCS += built-in.c env.c utils.c errors.c OBJS = ${SRCS:.c=.o} diff --git a/caller.c b/caller.c index a461368..8eccebe 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/16 11:57:13 by narnaud ### ########.fr */ +/* Updated: 2022/05/17 09:58:36 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,26 +39,23 @@ static void call_binary(t_datas *datas, t_command *cmd) { char **path_dirs; char *path; - size_t path_length; if (ft_strchr(cmd->argv[0], '/')) execve(cmd->argv[0], cmd->argv, datas->envp); else { - path_dirs = ft_split(getenv("PATH"), ':'); - while (path_dirs) + path = get_var_value(datas, "PATH", 4); + path_dirs = ft_split(path, ':'); + free(path); + while (path_dirs && *path_dirs) { - path_length = ft_strlen(*path_dirs) + ft_strlen(cmd->argv[0]) + 2; - path = ft_calloc(path_length, sizeof(char)); - ft_memcpy(path, *path_dirs, ft_strlen(*path_dirs)); - ft_memcpy(path + ft_strlen(*path_dirs), "/", 1); - ft_memcpy(path + ft_strlen(*path_dirs) + 1, \ - cmd->argv[0], ft_strlen(cmd->argv[0])); + path = ft_strjoin_with(*path_dirs, cmd->argv[0], "/"); execve(path, cmd->argv, datas->envp); free(path); path_dirs++; } } + cmd_error(cmd->argv[0], "command not found"); } static pid_t call(t_datas *datas, t_command *cmd) diff --git a/utils_2.c b/errors.c old mode 100755 new mode 100644 similarity index 83% rename from utils_2.c rename to errors.c index 524e71d..5e76de4 --- a/utils_2.c +++ b/errors.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* utils_2.c :+: :+: :+: */ +/* errors.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ -/* Updated: 2022/05/16 12:52:38 by narnaud ### ########.fr */ +/* Updated: 2022/05/17 09:59:56 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,16 @@ int file_error(char *cmd, char *file, char *msg) return (0); } +int cmd_error(char *cmd, char *msg) +{ + ft_putstr_fd("Minishell: ", 2); + ft_putstr_fd(cmd, 2); + ft_putstr_fd(": ", 2); + ft_putstr_fd(msg, 2); + ft_putchar_fd('\n', 2); + return (0); +} + int is_valid_identifier(char *ident) { int i; diff --git a/lexer_2.c b/lexer_2.c index 1ba8ab6..9e4e3d0 100644 --- a/lexer_2.c +++ b/lexer_2.c @@ -6,14 +6,12 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */ -/* Updated: 2022/05/16 10:22:46 by mea ### ########.fr */ +/* Updated: 2022/05/17 08:36:11 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static char *get_var_value(t_datas *datas, char **line, int name_len); - int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) { int name_len; @@ -28,10 +26,10 @@ int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) else { name_len = 1; - while (ft_isalpha((*line)[name_len]) \ + while (ft_isalpha((*line)[name_len]) || ft_isdigit((*line)[name_len]) || (*line)[name_len] == '_') name_len++; - value = get_var_value(datas, line, name_len); + value = get_var_value(datas, *line + 1, name_len - 1); } i = 0; while (value[i]) @@ -61,14 +59,14 @@ char *expend_str(t_datas *datas, char *line) return (tmp); } -static char *get_var_value(t_datas *datas, char **line, int name_len) +char *get_var_value(t_datas *datas, char *line, int name_len) { int i; char **env; char *name; char *value; - name = ft_substr(*line, 1, name_len - 1); + name = ft_substr(line, 0, name_len); i = 0; value = NULL; while (datas->envp[i] && !value) diff --git a/libft/Makefile b/libft/Makefile index 2686b6f..8a7ca8f 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -6,7 +6,7 @@ SRCS = is/ft_isalpha.c is/ft_isdigit.c is/ft_isascii.c is/ft_isprint.c \ SRCS += str/ft_len.c str/ft_lcpy.c str/ft_lcat.c str/ft_chr.c \ str/ft_rchr.c str/ft_ncmp.c str/ft_nstr.c str/ft_dup.c \ str/ft_sub.c str/ft_join.c str/ft_trim.c str/ft_split.c \ - str/ft_rev.c str/ft_mapi.c str/ft_iteri.c + str/ft_rev.c str/ft_mapi.c str/ft_iteri.c str/ft_join_with.c SRCS += conv/ft_toupper.c conv/ft_tolower.c conv/ft_atoi.c conv/ft_itoa.c \ conv/ft_itox.c conv/ft_utoa.c diff --git a/libft/libft.h b/libft/libft.h index 721fdf7..b9d3edf 100755 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ -/* Updated: 2022/05/09 08:59:36 by narnaud ### ########.fr */ +/* Updated: 2022/05/17 09:55:08 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,6 +74,7 @@ int ft_tolower(int c); int ft_toupper(int c); char *ft_substr(char const *s, unsigned int start, size_t len); char *ft_strjoin(const char *s1, const char *s2); +char *ft_strjoin_with(char *s1, char *s2, char *inter); char *ft_strtrim(char const *s1, char const *set); char **ft_split(char const *str, char ch); char *ft_itoa(int n); diff --git a/libft/str/ft_join_with.c b/libft/str/ft_join_with.c new file mode 100644 index 0000000..66860a5 --- /dev/null +++ b/libft/str/ft_join_with.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_join_with.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/17 09:44:59 by narnaud #+# #+# */ +/* Updated: 2022/05/17 09:56:37 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_strjoin_with(char *s1, char *s2, char *inter) +{ + int path_length; + char *ret; + + path_length = ft_strlen(s1) + ft_strlen(s2) + ft_strlen(inter) + 1; + ret = ft_calloc(path_length, sizeof(char)); + ft_memcpy(ret, s1, ft_strlen(s1)); + ft_memcpy(ret + ft_strlen(s1), inter, ft_strlen(inter)); + ft_memcpy(ret + ft_strlen(s1) + ft_strlen(inter), \ + s2, ft_strlen(s2)); + return (ret); +} diff --git a/minishell.h b/minishell.h index 19d2ded..3fcc803 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/16 10:42:18 by mea ### ########.fr */ +/* Updated: 2022/05/17 09:11:13 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,22 +50,17 @@ typedef struct s_datas int silent; } t_datas; -// ----------------------------------Utils_1.c +// ---------------------------------- Utils.c int is_empty(char *line); int handle_status(t_datas *datas, int status); void nothing(int sig_num); void sigs_handler(int sig_num); void halt(t_datas *datas, int ret_code, int silent); -typedef struct s_lex t_lexer; -// ----------------------------------Utils_2.c +// ---------------------------------- Errors.c int file_error(char *cmd, char *file, char *msg); -int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i); -char *expend_str(t_datas *datas, char *line); -int set_redir(t_lexer *lex, char **line, char ch); -// ----------------------------------Utils_3.c +int cmd_error(char *cmd, char *msg); int is_valid_identifier(char *ident); -int create_wc(t_lexer *lex, char *tmp); // ----------------------------------Builtins.c int is_builtin(char *cmd); @@ -129,8 +124,12 @@ typedef struct s_lex t_token *lexer(t_datas *datas, char *line); int create_token(t_lexer *lex, char str[]); +char *get_var_value(t_datas *datas, char *line, int name_len); int check_state(t_lexer *lex, char **line); +char *expend_str(t_datas *datas, char *line); +int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i); +int set_redir(t_lexer *lex, char **line, char ch); int create_wc(t_lexer *lex, char *wc); -#endif \ No newline at end of file +#endif diff --git a/utils_1.c b/utils.c similarity index 100% rename from utils_1.c rename to utils.c