|
@ -6,13 +6,14 @@ |
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ |
|
|
/* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ |
|
|
/* Updated: 2022/05/06 13:39:03 by narnaud ### ########.fr */ |
|
|
/* Updated: 2022/05/07 23:38:40 by narnaud@stude ### ########.fr */ |
|
|
/* */ |
|
|
/* */ |
|
|
/* ************************************************************************** */ |
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
|
#include "minishell.h" |
|
|
#include "minishell.h" |
|
|
|
|
|
|
|
|
static void clean_env(t_datas *datas); |
|
|
static void clean_env(t_datas *datas, int i); |
|
|
|
|
|
|
|
|
int ft_unset(t_datas *datas, t_command *cmd) |
|
|
int ft_unset(t_datas *datas, t_command *cmd) |
|
|
{ |
|
|
{ |
|
|
char **env; |
|
|
char **env; |
|
@ -32,14 +33,9 @@ int ft_unset(t_datas *datas, t_command *cmd) |
|
|
{ |
|
|
{ |
|
|
env = ft_split(datas->envp[j], '='); |
|
|
env = ft_split(datas->envp[j], '='); |
|
|
if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1)) |
|
|
if (!ft_strncmp(cmd->argv[i], env[0], name_len + 1)) |
|
|
{ |
|
|
clean_env(datas, j); |
|
|
free(datas->envp[j]); |
|
|
else if (++j) |
|
|
datas->envp[j] = NULL; |
|
|
|
|
|
clean_env(datas); |
|
|
|
|
|
continue; |
|
|
|
|
|
} |
|
|
|
|
|
ft_free_split(env); |
|
|
ft_free_split(env); |
|
|
j++; |
|
|
|
|
|
} |
|
|
} |
|
|
i++; |
|
|
i++; |
|
|
} |
|
|
} |
|
@ -66,6 +62,7 @@ int ft_env(t_datas *datas, t_command *cmd) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static int is_valid_identifier(char *ident); |
|
|
static int is_valid_identifier(char *ident); |
|
|
|
|
|
|
|
|
int ft_export(t_datas *datas, t_command *cmd) |
|
|
int ft_export(t_datas *datas, t_command *cmd) |
|
|
{ |
|
|
{ |
|
|
char **new; |
|
|
char **new; |
|
@ -79,32 +76,27 @@ int ft_export(t_datas *datas, t_command *cmd) |
|
|
new = ft_split(cmd->argv[1], '='); |
|
|
new = ft_split(cmd->argv[1], '='); |
|
|
name_len = ft_strlen(new[0]); |
|
|
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(new[0], 2); |
|
|
|
|
|
ft_putchar_fd('\n', 2); |
|
|
|
|
|
return (0); |
|
|
return (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(new[0], env[0], name_len + 1)) |
|
|
{ |
|
|
datas->envp[i] = (free(datas->envp[i]), NULL); |
|
|
datas->envp[i] = ft_strdup(cmd->argv[1]); |
|
|
else |
|
|
return (1); |
|
|
|
|
|
} |
|
|
|
|
|
ft_free_split(env); |
|
|
|
|
|
i++; |
|
|
i++; |
|
|
|
|
|
ft_free_split(env); |
|
|
} |
|
|
} |
|
|
|
|
|
ft_free_split(new); |
|
|
datas->envp[i] = ft_strdup(cmd->argv[1]); |
|
|
datas->envp[i] = ft_strdup(cmd->argv[1]); |
|
|
return (1); |
|
|
return (1); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
static void clean_env(t_datas *datas) |
|
|
static void clean_env(t_datas *datas, int i) |
|
|
{ |
|
|
{ |
|
|
int i; |
|
|
|
|
|
int j; |
|
|
int j; |
|
|
|
|
|
|
|
|
|
|
|
free(datas->envp[i]); |
|
|
|
|
|
datas->envp[i] = NULL; |
|
|
i = 0; |
|
|
i = 0; |
|
|
j = 0; |
|
|
j = 0; |
|
|
while (i < ENVP_MAX_SIZE) |
|
|
while (i < ENVP_MAX_SIZE) |
|
@ -125,15 +117,27 @@ static void clean_env(t_datas *datas) |
|
|
|
|
|
|
|
|
static int is_valid_identifier(char *ident) |
|
|
static int is_valid_identifier(char *ident) |
|
|
{ |
|
|
{ |
|
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
|
|
|
i = 0; |
|
|
if (ft_isalpha(ident[0]) || ident[0] == '_') |
|
|
if (ft_isalpha(ident[0]) || ident[0] == '_') |
|
|
{ |
|
|
{ |
|
|
while (*ident) |
|
|
while (ident[i]) |
|
|
|
|
|
{ |
|
|
|
|
|
if (!ft_isalpha(ident[i]) && !ft_isdigit(ident[i]) \ |
|
|
|
|
|
&& ident[i] != '_') |
|
|
{ |
|
|
{ |
|
|
if (!ft_isalpha(*ident) && !ft_isdigit(*ident) && *ident != '_') |
|
|
ft_putstr_fd("export: not an identifier: ", 2); |
|
|
|
|
|
ft_putstr_fd(ident, 2); |
|
|
|
|
|
ft_putchar_fd('\n', 2); |
|
|
return (0); |
|
|
return (0); |
|
|
ident++; |
|
|
} |
|
|
|
|
|
i++; |
|
|
} |
|
|
} |
|
|
return (1); |
|
|
return (1); |
|
|
} |
|
|
} |
|
|
|
|
|
ft_putstr_fd("export: not an identifier: ", 2); |
|
|
|
|
|
ft_putstr_fd(ident, 2); |
|
|
|
|
|
ft_putchar_fd('\n', 2); |
|
|
return (0); |
|
|
return (0); |
|
|
} |
|
|
} |
|
|