Browse Source

new save_var fct

master
narnaud 3 years ago
parent
commit
7a231646a0
  1. 65
      env.c
  2. 5
      minishell.c

65
env.c

@ -6,7 +6,7 @@
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ /* By: mea <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ /* 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); 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; int name_len;
char **env; 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 i;
int j; int tmp;
int ret ; int ret;
ret = 0;
if (cmd->argc < 2 && (ft_env(datas, cmd, 1), 1)) if (cmd->argc < 2 && (ft_env(datas, cmd, 1), 1))
return (ret); return (0);
j = 0; ret = 0;
while (++j < cmd->argc) 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; ret = 1;
continue ; continue ;
} }
new = ft_split(cmd->argv[j], '='); tmp = save_var(datas, cmd->argv[i]);
name_len = ft_strlen(new[0]); if (tmp)
if (!is_valid_identifier(new[0])) ret = tmp;
{
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]);
} }
return (ret); return (ret);
} }

5
minishell.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */ /* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ /* 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) while (*envp)
datas.envp[i++] = ft_strdup(*(envp++)); datas.envp[i++] = ft_strdup(*(envp++));
if (!*get_var_value(&datas, "MINISH", 7)) 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; datas.exit_code = 0;
if (datas.silent) if (datas.silent)
{ {

Loading…
Cancel
Save