fix(builtin/export): show error on invalid identifiers such as % and $

This commit is contained in:
Khaïs COLIN 2025-04-18 10:11:12 +02:00
parent efaf4708f9
commit c5e15903e0
2 changed files with 27 additions and 9 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 14:00:32 by khais #+# #+# */
/* Updated: 2025/04/18 10:01:59 by khais ### ########.fr */
/* Updated: 2025/04/18 10:09:09 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -29,21 +29,24 @@ static void single_export(char *arg, t_minishell *app)
char *key;
char *value;
if (arg[0] == '\0')
invalid_ident(arg);
if (ft_strchr(arg, '=') != NULL)
key = envp_get_key(arg);
if (is_identifier(key))
{
key = envp_get_key(arg);
value = envp_get_val(arg);
if (is_identifier(key) == false)
if (value[0] == '\0' && ft_strchr(arg, '=') == NULL)
{
invalid_ident(arg);
free2(key, value);
app->last_return_value = 1;
return ;
}
else if (env_set_entry(&app->env, key, value) == NULL)
if (env_set_entry(&app->env, key, value) == NULL)
ft_perror("minishell: export");
}
else
{
free(key);
invalid_ident(arg);
app->last_return_value = 1;
}
}
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app)

15
test.sh
View file

@ -749,10 +749,25 @@ EOF
when_run <<EOF "export with empty arguments"
export "" ""
echo \$?
EOF
expecting <<"EOF"
minishell: export: `': not a valid identifier
minishell: export: `': not a valid identifier
1
EOF
when_run <<EOF "export with invalid identifiers"
export % $ 0 1 \$?
echo \$?
EOF
expecting <<"EOF"
minishell: export: `%': not a valid identifier
minishell: export: `$': not a valid identifier
minishell: export: `0': not a valid identifier
minishell: export: `1': not a valid identifier
minishell: export: `0': not a valid identifier
1
EOF
finalize