From 96da8e54c3a49e717bc1188f75e0fa0e986b7fe6 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Thu, 16 Feb 2023 18:28:10 +0100 Subject: [PATCH] add: env variable support, but invalid read for a non existant variable --- .fill_env.c.swp => .env_fill.c.swp | Bin 12288 -> 12288 bytes Makefile | 2 +- cmd.c | 16 ++++-- cmds.c | 15 +++--- env_fill.c | 76 +++++++++++++++++++++++++++++ execution.c | 35 ++++++------- infile.c | 5 +- main.c | 6 +-- minishell.h | 14 +++--- outfile.c | 7 ++- 10 files changed, 133 insertions(+), 43 deletions(-) rename .fill_env.c.swp => .env_fill.c.swp (77%) create mode 100644 env_fill.c diff --git a/.fill_env.c.swp b/.env_fill.c.swp similarity index 77% rename from .fill_env.c.swp rename to .env_fill.c.swp index 164f87ed8368dd090827827fcd1c796dcb97e8ac..13854ebd11d64021f9ab261651de475bc371854a 100644 GIT binary patch literal 12288 zcmeI2UuYaf9LJ|stgFUoeNjP}-qqga?!3Lbgqm&*hz*F6_#i|O+b)~E*=Fcw@47pi z_AJ^L`=G>UMG*y`{ewQ%R|Vh2r#|=(9|fO8Q1LsnGk2RdmZ0-M}Q0^n-gXov=`qgdcy^ju(Fx&|8Ehc2wtxE6Ie0!l6BBc-a9BlLm8Yg19R zBGv)xfOWt+U>&dySO=^F)&c9l{%}AH50EQJ^`K67ML!SR@YJvNVI8mzSO=^F)&c8) zb-+4c9k32q2do3u0qej%bRg&x^4T4P{B;P)?Z!S8os zPH+vp1Gd3Mkb-5<1ugIhcnBN@vw#5U-JjAwTdV`t0qcNuz&fxm9C(E|DHC~`(E1=3 zb0>*|7Kb;RsHv)hX1YWL+Z@WKqHR3pxwVxQIS3^ZJ{zVy5mCJ|zS`%}%^;2kA&mka zv;OI%Qbb>tLwH2%QkO0*Q6(fBqm(g|3PLb7r%ej+Or$YO>RUmavpM=0?W$>u@wz`{ z!#D`l@D^2pEy`PTq%$-Nog^(YP;om_&@EE*9235Jt>cbji^2;iBA+f)kpq``X!TLy znR9UiD^+g3G&)0%EYaHOPK_QrCI_3Aqc=9t+MHbcNH15dO?p%k<7Z@&wWCF(-XYzO zOnK(>#J`XagkC?Nlkr{XLTIOsP6}Pp-wuqFn|_l$|6)fmh4fOLmGNd4C#TrDem<;Y z2jLZQaH%wP8p>8oUA*b@RlHyzIx_w;^Mx~-RQXAAnG_*1H+^fArFG%QJQK9hP`eZk z61>Pb+K_V+Qaa^oTDfKFpq*#M;V@fXsi0@KQ(*$e2f|+;`50XXEsANS<73sN+)=+>6#dM(|Y1ZWcW5^&q&ot01j5rJ{Lg zIr`t0+=Q6^fe>nAxryLYBX6>-C(Ht$%mj8m5v5z-#aJcM^o&wT(=53($KI+^ZK8-g zwYm{BzX#gk-t-XVHN7HMSBi%b+_OwM(DRHWYOvI5CH+HKh!2lYcd-7UCU(?B+JV{q( VbF6L(Ba%szAL?js{-Vdj2moSeBW?fy diff --git a/Makefile b/Makefile index 8d576dc..ff84e10 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c env_fill.c OBJS = ${SRCS:.c=.o} diff --git a/cmd.c b/cmd.c index e6581ef..cfe007d 100644 --- a/cmd.c +++ b/cmd.c @@ -6,10 +6,11 @@ /* By: cchauvet args = args; diff --git a/cmds.c b/cmds.c index 3f316cb..e05b8eb 100644 --- a/cmds.c +++ b/cmds.c @@ -6,13 +6,14 @@ /* By: cchauvet -static char *ft_get_variable(char **env, char *variable) -{ - size_t i; - - i = 0; - while (env[i] != NULL) - { - if (ft_strncmp(variable, env[i], ft_strlen(variable)) == 0) - return (ft_strchr(env[i], '=') + 1); - i++; - } - return (NULL); -} - -static char *ft_get_executable_path(char *executable_name, char **env) +static char *ft_get_executable_path(char *executable_name, t_list **env) { char *path; char *temp; @@ -33,10 +19,15 @@ static char *ft_get_executable_path(char *executable_name, char **env) ft_eprintf("minishell: malloc failed\n"); return (NULL); } + if (access(path, X_OK) == 0) + { + ft_eprintf("minishell: %s: permission denied\n", path); + return (NULL); + } } else { - tab = ft_split(ft_get_variable(env, "PATH"), ':'); + tab = ft_split(get_value_by_key("PATH", env), ':'); if (tab == NULL) return (NULL); i = 0; @@ -66,10 +57,11 @@ static char *ft_get_executable_path(char *executable_name, char **env) return (path); } -static int ft_excutor(t_cmd *cmd, char **env) +static int ft_excutor(t_cmd *cmd, t_list **env) { int pid; int return_value; + char **tab; if (cmd->fd_in == -1 || cmd->fd_out == -1) return (1); @@ -78,16 +70,21 @@ static int ft_excutor(t_cmd *cmd, char **env) return (1); if (pid == 0) { + //TODO DO THIS + /* tab = t_env2tab(env); */ + /* if (tab == NULL) */ + /* return (1); */ + tab = NULL; dup2(cmd->fd_out, 1); dup2(cmd->fd_in, 0); - execve(cmd->executable, cmd->args, env); + execve(cmd->executable, cmd->args, tab); } else waitpid(pid, &return_value, 0); return (return_value); } -int ft_cmds_executor(t_list **cmds, char **env) +int ft_cmds_executor(t_list **cmds, t_list **env) { t_cmd *content; t_list *current; diff --git a/infile.c b/infile.c index 805ab12..c0e09ce 100644 --- a/infile.c +++ b/infile.c @@ -6,7 +6,7 @@ /* By: cchauvet t_list **init_env(char **env); -int set_value_key(t_list **env, char *key, char *value); -char *get_value_index(int index, t_list **head); -char *get_value_key(char *key, t_list **head); +int set_value_by_key(char *key, char *value, t_list **env); +char *get_value_by_key(char *key, t_list **head); int ft_syntatic_verif(const char *str); int ft_file_is_readable(const char *path); int ft_file_is_writable(const char *path); @@ -35,13 +34,14 @@ int ft_infile(char *line); int ft_outfile(char *line); int ft_heredoc(char *stop); size_t ft_seglen_quoted(const char *str, char c); -int ft_cmds_executor(t_list **cmds, char **env); +int ft_cmds_executor(t_list **cmds, t_list **env); char **ft_split_quoted(const char *s, char c); void ft_cmddel(void *content); void env_del(void *content); -t_list **ft_parse_cmds(char *line); -int ft_cmd_filler(t_list *current, char **args); +t_list **ft_parse_cmds(char *line, t_list **env); +int ft_cmd_filler(t_list *current, char **args, t_list **env); char *ft_normalizer(char *str); +char *ft_env_filler(t_list **env, const char *str); typedef struct s_cmd { diff --git a/outfile.c b/outfile.c index f2e3c6b..ec1a8eb 100644 --- a/outfile.c +++ b/outfile.c @@ -6,7 +6,7 @@ /* By: cchauvet ') + if (fd != 1) + close(fd); if (ft_strcmp(">", tab[i]) == 0) fd = ft_file_is_writable(ft_quote_remover(tab[i + 1])); else if (ft_strcmp(">>", tab[i]) == 0)