mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
env: implement builtin env
This commit is contained in:
parent
0de583cf45
commit
1ef8b7a0ae
6 changed files with 72 additions and 5 deletions
1
Makefile
1
Makefile
|
|
@ -35,6 +35,7 @@ srcs = \
|
||||||
src/executing/here_doc/strip_newline.c \
|
src/executing/here_doc/strip_newline.c \
|
||||||
src/executing/simple_cmd/builtin_cd.c \
|
src/executing/simple_cmd/builtin_cd.c \
|
||||||
src/executing/simple_cmd/builtin_echo.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_exit.c \
|
||||||
src/executing/simple_cmd/builtin_export.c \
|
src/executing/simple_cmd/builtin_export.c \
|
||||||
src/executing/simple_cmd/builtin_invalid.c \
|
src/executing/simple_cmd/builtin_invalid.c \
|
||||||
|
|
|
||||||
30
src/executing/simple_cmd/builtin_env.c
Normal file
30
src/executing/simple_cmd/builtin_env.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
|
/* 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);
|
return (BUILTIN_EXIT);
|
||||||
if (ft_strcmp("echo", word) == 0)
|
if (ft_strcmp("echo", word) == 0)
|
||||||
return (BUILTIN_ECHO);
|
return (BUILTIN_ECHO);
|
||||||
|
if (ft_strcmp("env", word) == 0)
|
||||||
|
return (BUILTIN_ENV);
|
||||||
return (BUILTIN_INVALID);
|
return (BUILTIN_INVALID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,6 +43,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
|
||||||
[BUILTIN_EXPORT] = builtin_export,
|
[BUILTIN_EXPORT] = builtin_export,
|
||||||
[BUILTIN_EXIT] = builtin_exit,
|
[BUILTIN_EXIT] = builtin_exit,
|
||||||
[BUILTIN_ECHO] = builtin_echo,
|
[BUILTIN_ECHO] = builtin_echo,
|
||||||
|
[BUILTIN_ENV] = builtin_env,
|
||||||
};
|
};
|
||||||
|
|
||||||
type = get_builtin(cmd);
|
type = get_builtin(cmd);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
|
/* 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_export(t_simple_cmd *cmd, t_minishell *app);
|
||||||
t_builtin_type builtin_exit(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_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 get_builtin(t_simple_cmd *cmd);
|
||||||
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
|
/* Created: 2025/04/03 14:17/26 by khais #+# #+# */
|
||||||
/* Updated: 2025/04/03 13:58:41 by khais ### ########.fr */
|
/* Updated: 2025/04/03 14:17:26 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -26,6 +26,7 @@ typedef enum e_builtin_type
|
||||||
BUILTIN_EXPORT,
|
BUILTIN_EXPORT,
|
||||||
BUILTIN_EXIT,
|
BUILTIN_EXIT,
|
||||||
BUILTIN_ECHO,
|
BUILTIN_ECHO,
|
||||||
|
BUILTIN_ENV,
|
||||||
} t_builtin_type;
|
} t_builtin_type;
|
||||||
|
|
||||||
#endif // SIMPLE_CMD_EXECUTE_H
|
#endif // SIMPLE_CMD_EXECUTE_H
|
||||||
|
|
|
||||||
33
test.sh
33
test.sh
|
|
@ -305,6 +305,7 @@ EOF
|
||||||
|
|
||||||
EXTRAENV=-i
|
EXTRAENV=-i
|
||||||
when_run <<EOF "export with strange inputs"
|
when_run <<EOF "export with strange inputs"
|
||||||
|
export ENV=$(which env)
|
||||||
export PATH=$PATH
|
export PATH=$PATH
|
||||||
export var
|
export var
|
||||||
echo status=\$?
|
echo status=\$?
|
||||||
|
|
@ -312,7 +313,7 @@ echo var=[\$var]
|
||||||
export blue=
|
export blue=
|
||||||
echo status=\$?
|
echo status=\$?
|
||||||
echo blue=[\$blue]
|
echo blue=[\$blue]
|
||||||
env -u PATH env
|
\$ENV -u PATH -u ENV env
|
||||||
EOF
|
EOF
|
||||||
expecting <<EOF
|
expecting <<EOF
|
||||||
status=0
|
status=0
|
||||||
|
|
@ -451,6 +452,36 @@ hi -n hello
|
||||||
goodbye
|
goodbye
|
||||||
EOF
|
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"
|
when_run <<EOF "quoted parentheses are not operators"
|
||||||
echo unclosed '('
|
echo unclosed '('
|
||||||
EOF
|
EOF
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue