Browse Source

export and unset

master
narnaud 3 years ago
parent
commit
a535e5808f
  1. 63
      env.c

63
env.c

@ -26,14 +26,57 @@ int is_valid_identifier(char *ident)
} }
return (0); return (0);
} }
void clean_env(t_datas *datas)
{
int i;
int j;
i = 0;
j = 0;
while (i < 1024)
{
if (!datas->envp[i])
{
j = 1;
while (!datas->envp[i + j] && (i + j) < 1024)
j++;
if (i + j == 1024)
return ;
datas->envp[i] = datas->envp[i + j];
datas->envp[i + j] = NULL;
}
i++;
}
}
int ft_unset(t_datas *datas, t_command *cmd) int ft_unset(t_datas *datas, t_command *cmd)
{ {
(void)datas; char **env;
if (!cmd->argv[1]) int i;
{ int j;
//read(input_fd, PIPE_MAX_SIZE); int name_len;
if (cmd->argc < 2)
return (1); return (1);
env = datas->envp;
i = 1;
while (i < cmd->argc && datas->envp[i - 1])
{
name_len = ft_strlen(cmd->argv[i]);
j = 0;
while (datas->envp[j])
{
env = ft_split(datas->envp[j], '=');
if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1))
{
free(datas->envp[j]);
datas->envp[j] = NULL;
clean_env(datas);
continue;
}
ft_free_split(env);
j++;
}
i++;
} }
return (0); return (0);
} }
@ -60,11 +103,15 @@ int ft_env(t_datas *datas, t_command *cmd)
int ft_export(t_datas *datas, t_command *cmd) int ft_export(t_datas *datas, t_command *cmd)
{ {
char **new; char **new;
int name_len;
char **env; char **env;
int i; int i;
i = 0; i = 0;
if (cmd->argc < 2)
return (0);
new = ft_split(cmd->argv[1], '='); new = ft_split(cmd->argv[1], '=');
name_len = ft_strlen(new[0]);
if (!is_valid_identifier(new[0])) if (!is_valid_identifier(new[0]))
{ {
ft_putstr_fd("export: not an identifier: ", 2); ft_putstr_fd("export: not an identifier: ", 2);
@ -75,14 +122,14 @@ int ft_export(t_datas *datas, t_command *cmd)
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], ft_strlen(new[0]) + 1)) if (!ft_strncmp(new[0], env[0], name_len + 1))
{ {
datas->envp[i] = cmd->argv[1]; datas->envp[i] = ft_strdup(cmd->argv[1]);
return (1); return (1);
} }
ft_free_split(env);
i++; i++;
} }
datas->envp[i] = cmd->argv[1]; datas->envp[i] = ft_strdup(cmd->argv[1]);
datas->envp[i + 1] = NULL;
return (1); return (1);
} }

Loading…
Cancel
Save