diff --git a/Makefile b/Makefile index 7428d0c..281e713 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRCS = src/ # include directory INCLUDES = include/ libtf/ libft/ # .c files in src/ without the extension -CODE = main ask_command error path parse_command exec_command +CODE = main ask_command error path parse_command exec_command builtin # directories to 'make' LIBRARIES = libtf libft # .a files to include diff --git a/include/include.h b/include/include.h index 261a55c..4616cbe 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/05/31 13:45:38 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 13:52:11 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -97,6 +97,14 @@ const char **get_path(const t_memclass mc, char *const envp[]); const char *search_path( const t_memclass mc, const char **path, const char *prog); +///// BUILTIN ///// + +// Check if the command is a builtin, return 1 if yes, -1 if no +int is_builtin(const char *str); + +// Execute the builtin command +int exec_builtin(t_call call); + ///// MAIN ///// extern t_memclass g_mc; diff --git a/src/builtin.c b/src/builtin.c new file mode 100644 index 0000000..b9e17a5 --- /dev/null +++ b/src/builtin.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jschaft +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/03 12:02:45 by jschaft #+# #+# */ +/* Updated: 2024/06/03 14:18:50 by jschaft ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "include.h" + +int is_builtin(const char *str) +{ + if (ft_strncmp(str, "cd", 3) == 0) + return (1); + if (ft_strncmp(str, "exit", 5) == 0) + return (1); + if (ft_strncmp(str, "export", 7) == 0) + return (1); + if (ft_strncmp(str, "unset", 6) == 0) + return (1); + return (-1); +} + +static int cd_builtin(const char *path) +{ + int r; + + r = chdir(path); + if (r) + { + minishell_error("cd: unavailable path"); + return (-1); + } + return (1); +} + +int exec_builtin(t_call call) +{ + if (ft_strncmp(call.program, "cd", 3) == 0) + return (cd_builtin((const char*)call.argv[1])); +/* if (ft_strncmp(call.program, "exit", 5) == 0) + exit_builtin(); + if (ft_strncmp(call.program, "export", 7) == 0) + return (export_builtin(call.argv)); + if (ft_strncmp(call.program, "unset", 6) == 0) + return (unset_builtin());*/ + return (1); +} \ No newline at end of file diff --git a/src/exec_command.c b/src/exec_command.c index a4adfe9..15e7928 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/05/17 14:43:37 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 14:01:02 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */ @@ -102,7 +102,10 @@ static int exec_each_call(t_exec_command_global *global, { inout[0] = global->pipes[i].pipe[0]; inout[1] = global->pipes[i + 1].pipe[1]; - r = execute_call(global, command.calls[i], inout, envp); + if (is_builtin(command.calls[i].program) == 1) + r = exec_builtin(command.calls[i]); + else + r = execute_call(global, command.calls[i], inout, envp); if (r) return (r); } diff --git a/src/path.c b/src/path.c index 78f0660..ef5ee59 100644 --- a/src/path.c +++ b/src/path.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/05/01 17:53:38 by mcolonna ### ########.fr */ +/* Updated: 2024/06/03 11:39:59 by jschaft ### ########.fr */ /* */ /* ************************************************************************** */