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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

5
minishell.c

@ -6,7 +6,7 @@
/* By: mea <mea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{

Loading…
Cancel
Save