Browse Source

new save_var fct

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

61
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;
int i; char **var;
int j;
int ret ;
ret = 0; var = ft_split(argv, '=');
if (cmd->argc < 2 && (ft_env(datas, cmd, 1), 1)) name_len = ft_strlen(var[0]);
return (ret); if (!is_valid_identifier(var[1]))
j = 0; return (1);
while (++j < cmd->argc)
{
if (!cmd->argv[j][0] || cmd->argv[j][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; i = 0;
while (datas->envp[i]) while (datas->envp[i])
{ {
env = ft_split(datas->envp[i], '='); env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(new[0], env[0], name_len + 1)) if (!ft_strncmp(var[0], env[0], name_len + 1))
datas->envp[i] = (free(datas->envp[i]), NULL); datas->envp[i] = (free(datas->envp[i]), NULL);
else else
i++; i++;
ft_free_split(env); ft_free_split(env);
} }
ft_free_split(new); datas->envp[i] = ft_strdup(argv);
datas->envp[i] = ft_strdup(cmd->argv[j]); ft_free_split(var);
return (0);
}
int ft_export(t_datas *datas, t_command *cmd)
{
int i;
int tmp;
int ret;
if (cmd->argc < 2 && (ft_env(datas, cmd, 1), 1))
return (0);
ret = 0;
i = 0;
while (++i < cmd->argc)
{
if (!cmd->argv[i][0] || cmd->argv[i][0] == '=')
{
ret = 1;
continue ;
}
tmp = save_var(datas, cmd->argv[i]);
if (tmp)
ret = tmp;
} }
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