export: handle invalid identifiers

This commit is contained in:
Khaïs COLIN 2025-04-01 15:53:24 +02:00
parent 299e016a27
commit 3cdf7c3f76
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 30 additions and 14 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 14:00:32 by khais #+# #+# */
/* Updated: 2025/04/01 14:08:53 by khais ### ########.fr */
/* Updated: 2025/04/01 15:53:06 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,25 +15,39 @@
#include "../../env/env_manip.h"
#include "libft.h"
#include "../../ft_errno.h"
#include <unistd.h>
#include "../../parser/matchers/identifier.h"
static void single_export(char *arg, t_minishell *app)
{
char *key;
char *value;
if (ft_strchr(arg, '=') != NULL)
{
key = envp_get_key(arg);
value = envp_get_val(arg);
if (is_identifier(key) == false)
{
ft_dprintf(STDERR_FILENO,
"minishell: export: `%s': not a valid identifier\n", arg);
free2(key, value);
}
else if (env_set_entry(&app->env, key, value) == NULL)
ft_perror("minishell: export");
}
}
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app)
{
int i;
t_worddesc *arg;
char *key;
char *value;
i = 1;
arg = wordlist_get(cmd->words, i++);
while (arg != NULL)
{
if (ft_strchr(arg->word, '=') != NULL)
{
key = envp_get_key(arg->word);
value = envp_get_val(arg->word);
if (env_set_entry(&app->env, key, value) == NULL)
ft_perror("minishell: export");
}
single_export(arg->word, app);
arg = wordlist_get(cmd->words, i++);
}
return (BUILTIN_EXPORT);

10
test.sh
View file

@ -318,13 +318,15 @@ blue=[]
blue=
EOF
when_run <<EOF "export with invalid identifiers"
export =
export 123=456
when_run <<"EOF" "export with invalid identifiers"
export = 123=456 hello=hi 456=789
echo hello=$hello
EOF
todo <<"EOF"
expecting <<"EOF"
minishell: export: `=': not a valid identifier
minishell: export: `123=456': not a valid identifier
minishell: export: `456=789': not a valid identifier
hello=hi
EOF
when_run <<EOF "quoted parentheses are not operators"