env: implement builtin env

This commit is contained in:
Khaïs COLIN 2025-04-02 19:45:18 +02:00
parent 0de583cf45
commit 1ef8b7a0ae
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
6 changed files with 72 additions and 5 deletions

View file

@ -35,6 +35,7 @@ srcs = \
src/executing/here_doc/strip_newline.c \
src/executing/simple_cmd/builtin_cd.c \
src/executing/simple_cmd/builtin_echo.c \
src/executing/simple_cmd/builtin_env.c \
src/executing/simple_cmd/builtin_exit.c \
src/executing/simple_cmd/builtin_export.c \
src/executing/simple_cmd/builtin_invalid.c \

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/02 19:35:52 by khais #+# #+# */
/* Updated: 2025/04/02 19:38:16 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "libft.h"
t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app)
{
t_env *env;
if (cmd->words->next != NULL)
ft_dprintf(STDERR_FILENO, "minishell: env: ignoring arguments\n");
env = app->env;
while (env != NULL)
{
ft_printf("%s=%s\n", env->key, env->value);
env = env->next;
}
app->last_return_value = 0;
return (BUILTIN_ENV);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/03 13:59:01 by khais ### ########.fr */
/* Updated: 2025/04/03 14:16:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,6 +28,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
return (BUILTIN_EXIT);
if (ft_strcmp("echo", word) == 0)
return (BUILTIN_ECHO);
if (ft_strcmp("env", word) == 0)
return (BUILTIN_ENV);
return (BUILTIN_INVALID);
}
@ -41,6 +43,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
[BUILTIN_EXPORT] = builtin_export,
[BUILTIN_EXIT] = builtin_exit,
[BUILTIN_ECHO] = builtin_echo,
[BUILTIN_ENV] = builtin_env,
};
type = get_builtin(cmd);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
/* Updated: 2025/04/03 13:58:23 by khais ### ########.fr */
/* Updated: 2025/04/03 14:17:17 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_env(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd);
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
/* Updated: 2025/04/03 13:58:41 by khais ### ########.fr */
/* Created: 2025/04/03 14:17/26 by khais #+# #+# */
/* Updated: 2025/04/03 14:17:26 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,7 @@ typedef enum e_builtin_type
BUILTIN_EXPORT,
BUILTIN_EXIT,
BUILTIN_ECHO,
BUILTIN_ENV,
} t_builtin_type;
#endif // SIMPLE_CMD_EXECUTE_H

33
test.sh
View file

@ -305,6 +305,7 @@ EOF
EXTRAENV=-i
when_run <<EOF "export with strange inputs"
export ENV=$(which env)
export PATH=$PATH
export var
echo status=\$?
@ -312,7 +313,7 @@ echo var=[\$var]
export blue=
echo status=\$?
echo blue=[\$blue]
env -u PATH env
\$ENV -u PATH -u ENV env
EOF
expecting <<EOF
status=0
@ -451,6 +452,36 @@ hi -n hello
goodbye
EOF
EXTRAENV="-i"
when_run <<EOF "env works"
echo this should be empty:
env
echo
export var=VALUE hi=hello bye=goodbye
echo this contains some values
env
EOF
expecting <<EOF
this should be empty:
this contains some values
var=VALUE
hi=hello
bye=goodbye
EOF
EXTRAENV="-i"
when_run <<EOF "env ignores additional arguments"
export var=VALUE hi=hello bye=goodbye
env -i name="The Other" ls
EOF
expecting <<EOF
minishell: env: ignoring arguments
var=VALUE
hi=hello
bye=goodbye
EOF
when_run <<EOF "quoted parentheses are not operators"
echo unclosed '('
EOF