update: integrated hashmaps in builtins, and replaced command calls by builtins
This commit is contained in:
@@ -6,33 +6,43 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/29 22:09:51 by sede-san #+# #+# */
|
||||
/* Updated: 2025/11/02 03:13:46 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/12/01 17:53:57 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef BUILTINS_H
|
||||
# define BUILTINS_H
|
||||
|
||||
# include "libft.h"
|
||||
# include "minishell.h"
|
||||
# include "ft_args.h"
|
||||
|
||||
typedef unsigned char (*t_builtin_func)(t_command cmd, t_minishell *msh);
|
||||
|
||||
|
||||
/******************************************************************************/
|
||||
/* Functions */
|
||||
/******************************************************************************/
|
||||
|
||||
// cd.c
|
||||
/* builtins.c */
|
||||
|
||||
extern u_int8_t set_builtins(t_minishell *msh);
|
||||
|
||||
extern u_int8_t is_builtin(const char *command_name, t_minishell *msh);
|
||||
|
||||
/* cd.c */
|
||||
|
||||
extern u_int8_t builtin_cd(t_command cmd, t_minishell *msh);
|
||||
|
||||
// echo.c
|
||||
/* echo.c */
|
||||
|
||||
extern u_int8_t builtin_echo(t_command cmd);
|
||||
extern u_int8_t builtin_echo(t_command cmd, t_minishell *msh);
|
||||
|
||||
// exit.c
|
||||
/* exit.c */
|
||||
|
||||
extern u_int8_t builtin_exit(t_command cmd, t_minishell *msh);
|
||||
|
||||
// pwd.c
|
||||
/* pwd.c */
|
||||
|
||||
extern u_int8_t builtin_pwd(t_command cmd, t_minishell *msh);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/22 19:10:13 by sede-san #+# #+# */
|
||||
/* Updated: 2025/12/01 13:33:37 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/12/01 17:02:10 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -49,6 +49,7 @@ typedef struct s_variables
|
||||
typedef struct s_minishell
|
||||
{
|
||||
t_variables variables;
|
||||
t_hashmap *builtins;
|
||||
u_int8_t exit_status;
|
||||
u_int8_t exit;
|
||||
} t_minishell;
|
||||
|
||||
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]));
|
||||
}
|
||||
|
||||
@@ -6,38 +6,39 @@
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/28 13:03:44 by sede-san #+# #+# */
|
||||
/* Updated: 2025/12/01 14:08:47 by sede-san ### ########.fr */
|
||||
/* Updated: 2025/12/02 09:07:28 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "executor.h"
|
||||
#include "builtins.h"
|
||||
|
||||
static char *solve_path(char *cmd_name, t_minishell *msh);
|
||||
static u_int8_t path_is_solved(char *cmd_name);
|
||||
static u_int8_t path_is_solved(char *cmd_name, t_minishell *msh);
|
||||
static void handle_child(t_command *cmd, t_minishell *msh);
|
||||
static void handle_parent(pid_t child_pid, t_command *cmd, t_minishell *msh);
|
||||
|
||||
u_int8_t execute(
|
||||
t_command cmd,
|
||||
t_minishell *msh
|
||||
){
|
||||
) {
|
||||
pid_t child_pid;
|
||||
|
||||
cmd.path = solve_path(cmd.argv[0], msh);
|
||||
if (!cmd.path)
|
||||
{
|
||||
// command not found
|
||||
ft_eprintf("minishell: %s: command not found\n", cmd.argv[0]);
|
||||
return (msh->exit_status = 127, msh->exit_status);
|
||||
}
|
||||
if (access(cmd.path, X_OK) != EXIT_SUCCESS)
|
||||
if (!is_builtin(cmd.path, msh) && access(cmd.path, X_OK) != EXIT_SUCCESS)
|
||||
{
|
||||
// permission denied
|
||||
ft_eputstr("minishell: ");
|
||||
perror(cmd.path);
|
||||
return (msh->exit_status = 126, msh->exit_status);
|
||||
}
|
||||
child_pid = fork();
|
||||
child_pid = 0;
|
||||
if (!is_builtin(cmd.path, msh))
|
||||
child_pid = fork();
|
||||
if (child_pid == -1)
|
||||
perror("minishell");
|
||||
else if (child_pid == 0)
|
||||
@@ -55,7 +56,7 @@ static char *solve_path(
|
||||
char **path;
|
||||
size_t i;
|
||||
|
||||
if (path_is_solved(cmd_name))
|
||||
if (path_is_solved(cmd_name, msh))
|
||||
// return a copy to avoid double free on parent
|
||||
return (ft_strdup(cmd_name));
|
||||
path = ft_split(get_env("PATH", msh), COLON);
|
||||
@@ -88,11 +89,13 @@ static char *solve_path(
|
||||
}
|
||||
|
||||
static u_int8_t path_is_solved(
|
||||
char *cmd_name
|
||||
char *cmd_name,
|
||||
t_minishell *msh
|
||||
){
|
||||
return (ft_strncmp(cmd_name, "/", 1) == 0
|
||||
|| (cmd_name[1] && ft_strncmp(cmd_name, "./", 2) == 0)
|
||||
|| (cmd_name[2] && ft_strncmp(cmd_name, "../", 3) == 0)
|
||||
|| is_builtin(cmd_name, msh)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -100,11 +103,20 @@ static void handle_child(
|
||||
t_command *cmd,
|
||||
t_minishell *msh
|
||||
){
|
||||
char **envp;
|
||||
char **envp;
|
||||
t_builtin_func builtin;
|
||||
|
||||
envp = get_envp(msh);
|
||||
execve(cmd->path, cmd->argv, envp);
|
||||
free_envp(envp);
|
||||
if (is_builtin(cmd->argv[0], msh))
|
||||
{
|
||||
builtin = ft_hashmap_get(msh->builtins, cmd->argv[0]);
|
||||
builtin(*cmd, msh);
|
||||
}
|
||||
else
|
||||
{
|
||||
envp = get_envp(msh);
|
||||
execve(cmd->path, cmd->argv, envp);
|
||||
free_envp(envp);
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_parent(
|
||||
|
||||
Reference in New Issue
Block a user