diff --git a/Makefile b/Makefile index eec1abb..dee7039 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,8 @@ srcs = \ src/executing/here_doc/here_doc_expand_line.c \ src/executing/here_doc/random_filename.c \ src/executing/here_doc/strip_newline.c \ + src/executing/simple_cmd/builtin_invalid.c \ + src/executing/simple_cmd/builtin_pwd.c \ src/executing/simple_cmd/simple_cmd_execute.c \ src/ft_errno.c \ src/get_command.c \ diff --git a/src/executing/simple_cmd/builtin_invalid.c b/src/executing/simple_cmd/builtin_invalid.c new file mode 100644 index 0000000..dd92c74 --- /dev/null +++ b/src/executing/simple_cmd/builtin_invalid.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_invalid.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/31 14:17:14 by khais #+# #+# */ +/* Updated: 2025/03/31 14:17:42 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" + +t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app) +{ + (void)cmd; + (void)app; + return (BUILTIN_INVALID); +} diff --git a/src/executing/simple_cmd/builtin_pwd.c b/src/executing/simple_cmd/builtin_pwd.c new file mode 100644 index 0000000..29aabde --- /dev/null +++ b/src/executing/simple_cmd/builtin_pwd.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/31 14:21:52 by khais #+# #+# */ +/* Updated: 2025/03/31 14:48:23 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" +#include "libft.h" +#include "../../ft_errno.h" +#include "errno.h" + +static char *get_current_dir(void) +{ + char *path; + size_t size; + + size = 64; + path = ft_calloc(size, sizeof(char)); + if (path == NULL) + return (ft_errno(FT_EERRNO), NULL); + while (getcwd(path, size) == NULL) + { + if (errno != ERANGE) + return (ft_errno(FT_EERRNO), NULL); + free(path); + size *= 2; + path = ft_calloc(size, sizeof(char)); + if (path == NULL) + return (ft_errno(FT_EERRNO), NULL); + } + return (path); +} + +t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app) +{ + char *path; + + (void)cmd; + path = get_current_dir(); + if (path == NULL) + { + app->last_return_value = 1; + ft_perror("pwd: failed to get current directory"); + } + else + { + ft_printf("%s\n", path); + free(path); + app->last_return_value = 0; + } + return (BUILTIN_PWD); +} diff --git a/src/executing/simple_cmd/builtins.h b/src/executing/simple_cmd/builtins.h new file mode 100644 index 0000000..6a79181 --- /dev/null +++ b/src/executing/simple_cmd/builtins.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/31 14:16:13 by khais #+# #+# */ +/* Updated: 2025/03/31 14:21:46 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTINS_H +# define BUILTINS_H + +# include "simple_cmd_execute.h" + +t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app); +t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app); + +#endif // BUILTINS_H diff --git a/src/executing/simple_cmd/simple_cmd_execute.c b/src/executing/simple_cmd/simple_cmd_execute.c index 6a283e8..a0a4e47 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.c +++ b/src/executing/simple_cmd/simple_cmd_execute.c @@ -6,11 +6,12 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */ -/* Updated: 2025/03/28 16:56:47 by khais ### ########.fr */ +/* Updated: 2025/03/31 14:21:40 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "simple_cmd_execute.h" +#include "builtins.h" #include "libft.h" #include "../../subst/subst.h" #include "../../env/env_convert.h" @@ -38,6 +39,28 @@ static void command_not_found(t_simple_cmd *cmd) cmd->words->word->word); } +static t_builtin_type get_builtin(t_simple_cmd *cmd) +{ + char *word; + + word = cmd->words->word->word; + if (ft_strcmp("pwd", word) == 0) + return (BUILTIN_PWD); + return (BUILTIN_INVALID); +} + +static t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app) +{ + t_builtin_type type; + static t_builtin_type (*builtins[])(t_simple_cmd *, t_minishell *) = { + [BUILTIN_INVALID] = builtin_invalid, + [BUILTIN_PWD] = builtin_pwd, + }; + + type = get_builtin(cmd); + return (builtins[type](cmd, app)); +} + void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) { char *exe; @@ -45,6 +68,8 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app) if (cmd == NULL || cmd->words == NULL || cmd->words->word == NULL) return ; + if (execute_builtin(cmd, app) != BUILTIN_INVALID) + return ; exe = get_cmdpath(cmd->words->word->word, app); if (exe == NULL) { diff --git a/src/executing/simple_cmd/simple_cmd_execute.h b/src/executing/simple_cmd/simple_cmd_execute.h index 76a195b..b3ad798 100644 --- a/src/executing/simple_cmd/simple_cmd_execute.h +++ b/src/executing/simple_cmd/simple_cmd_execute.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/27 16:20:31 by khais #+# #+# */ -/* Updated: 2025/03/27 16:40:32 by khais ### ########.fr */ +/* Updated: 2025/03/31 13:59:26 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,4 +18,10 @@ void simple_cmd_execute(t_simple_cmd *cmd, t_minishell *app); +typedef enum e_builtin_type +{ + BUILTIN_INVALID, + BUILTIN_PWD, +} t_builtin_type; + #endif // SIMPLE_CMD_EXECUTE_H diff --git a/test.sh b/test.sh index 2c2ea49..017f4d2 100755 --- a/test.sh +++ b/test.sh @@ -143,6 +143,39 @@ minishell: qwertyuiop: command not found minishell: poiuytrewq: command not found EOF +when_run <