update: integrated hashmaps in builtins, and replaced command calls by builtins
This commit is contained in:
33
src/builtins/builtins.c
Normal file
33
src/builtins/builtins.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* builtins.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/12/01 17:04:57 by sede-san #+# #+# */
|
||||
/* Updated: 2025/12/01 17:54:26 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
u_int8_t set_builtins(
|
||||
t_minishell *msh
|
||||
) {
|
||||
msh->builtins = ft_hashmap_new(4, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (msh->builtins == NULL)
|
||||
return (0);
|
||||
ft_hashmap_put(msh->builtins, ft_strdup("cd"), builtin_cd);
|
||||
ft_hashmap_put(msh->builtins, ft_strdup("echo"), builtin_echo);
|
||||
ft_hashmap_put(msh->builtins, ft_strdup("exit"), builtin_exit);
|
||||
ft_hashmap_put(msh->builtins, ft_strdup("pwd"), builtin_pwd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
u_int8_t is_builtin(
|
||||
const char *command_name,
|
||||
t_minishell *msh
|
||||
) {
|
||||
return (ft_hashmap_contains_key(msh->builtins, command_name));
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/31 02:41:11 by sede-san #+# #+# */
|
||||
/* Updated: 2025/11/02 03:12:00 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/12/01 18:05:51 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -14,11 +14,13 @@
|
||||
#include "echo_def.h"
|
||||
|
||||
u_int8_t builtin_echo(
|
||||
t_command cmd
|
||||
t_command cmd,
|
||||
t_minishell *msh
|
||||
){
|
||||
const t_args args = read_args(cmd);
|
||||
size_t i;
|
||||
|
||||
(void)msh;
|
||||
i = -1;
|
||||
while (args.strings.data.sp[++i])
|
||||
{
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/30 01:20:48 by sede-san #+# #+# */
|
||||
/* Updated: 2025/10/30 09:08:02 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/12/01 19:15:08 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -19,7 +19,7 @@ u_int8_t builtin_exit(
|
||||
ft_eputendl("exit");
|
||||
if (cmd.argc == 1)
|
||||
{
|
||||
// msh.exit = 1;
|
||||
msh->exit = 1;
|
||||
// return the last exit_status, if none 0 is returned
|
||||
return (msh->exit_status);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ u_int8_t builtin_exit(
|
||||
}
|
||||
else
|
||||
{
|
||||
// msh.exit = 1;
|
||||
msh->exit = 1;
|
||||
// cast to u_int8_t causes to return a value between 0 and 255
|
||||
return ((u_int8_t)ft_atol(cmd.argv[1]));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user