From 7a231646a05158c1b632cc5c218cb4635cf57033 Mon Sep 17 00:00:00 2001 From: narnaud Date: Wed, 18 May 2022 15:52:49 +0200 Subject: [PATCH] new save_var fct --- env.c | 65 +++++++++++++++++++++++++++++------------------------ minishell.c | 5 +++-- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/env.c b/env.c index e3a7294..54f66f9 100755 --- a/env.c +++ b/env.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ -/* Updated: 2022/05/18 15:24:19 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/18 15:50:14 by narnaud@stude ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,45 +63,52 @@ int ft_env(t_datas *datas, t_command *cmd, int declared_pop) return (0); } -int ft_export(t_datas *datas, t_command *cmd) +int save_var(t_datas *datas, char *argv) { - char **new; + int i; int name_len; char **env; + char **var; + + var = ft_split(argv, '='); + name_len = ft_strlen(var[0]); + if (!is_valid_identifier(var[1])) + return (1); + i = 0; + while (datas->envp[i]) + { + env = ft_split(datas->envp[i], '='); + if (!ft_strncmp(var[0], env[0], name_len + 1)) + datas->envp[i] = (free(datas->envp[i]), NULL); + else + i++; + ft_free_split(env); + } + datas->envp[i] = ft_strdup(argv); + ft_free_split(var); + return (0); +} + +int ft_export(t_datas *datas, t_command *cmd) +{ int i; - int j; - int ret ; + int tmp; + int ret; - ret = 0; if (cmd->argc < 2 && (ft_env(datas, cmd, 1), 1)) - return (ret); - j = 0; - while (++j < cmd->argc) + return (0); + ret = 0; + i = 0; + while (++i < cmd->argc) { - if (!cmd->argv[j][0] || cmd->argv[j][0] == '=') + if (!cmd->argv[i][0] || cmd->argv[i][0] == '=') { ret = 1; continue ; } - new = ft_split(cmd->argv[j], '='); - name_len = ft_strlen(new[0]); - if (!is_valid_identifier(new[0])) - { - ret = 1; - continue ; - } - i = 0; - while (datas->envp[i]) - { - env = ft_split(datas->envp[i], '='); - if (!ft_strncmp(new[0], env[0], name_len + 1)) - datas->envp[i] = (free(datas->envp[i]), NULL); - else - i++; - ft_free_split(env); - } - ft_free_split(new); - datas->envp[i] = ft_strdup(cmd->argv[j]); + tmp = save_var(datas, cmd->argv[i]); + if (tmp) + ret = tmp; } return (ret); } diff --git a/minishell.c b/minishell.c index eef1c20..3e52b38 100755 --- a/minishell.c +++ b/minishell.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/18 14:57:34 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/18 15:50:52 by narnaud@stude ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,8 @@ int main(int argc, char **argv, char **envp) while (*envp) datas.envp[i++] = ft_strdup(*(envp++)); if (!*get_var_value(&datas, "MINISH", 7)) - datas.envp[i] = ft_strjoin_with("MINISH=", "/minishell", getcwd(NULL, 0)); + datas.envp[i] = ft_strjoin_with("MINISH=", "/minishell", + getcwd(NULL, 0)); datas.exit_code = 0; if (datas.silent) {