echo: implement builtin echo

This commit is contained in:
Khaïs COLIN 2025-04-03 13:50:56 +02:00
parent 715c2aced8
commit 0de583cf45
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
6 changed files with 97 additions and 3 deletions

View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 13:59:13 by khais #+# #+# */
/* Updated: 2025/04/03 14:11:57 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include "libft.h"
#include "simple_cmd_execute.h"
static bool should_print_newline(t_wordlist **arg)
{
bool print_newline;
bool end_of_args;
size_t i;
print_newline = true;
while (*arg != NULL)
{
if ((*arg)->word->word[0] != '-')
break ;
i = 1;
end_of_args = false;
while ((*arg)->word->word[i] != '\0' && !end_of_args)
{
if ((*arg)->word->word[i++] != 'n')
end_of_args = true;
}
if (end_of_args)
break ;
print_newline = false;
(*arg) = (*arg)->next;
}
return (print_newline);
}
t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app)
{
t_wordlist *arg;
bool print_newline;
arg = cmd->words->next;
print_newline = should_print_newline(&arg);
while (arg != NULL)
{
ft_printf("%s", arg->word->word);
arg = arg->next;
if (arg != NULL)
ft_printf(" ");
}
if (print_newline)
ft_printf("\n");
app->last_return_value = 0;
return (BUILTIN_ECHO);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/01 18:17:25 by khais ### ########.fr */
/* Updated: 2025/04/03 13:59:01 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -26,6 +26,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
return (BUILTIN_EXPORT);
if (ft_strcmp("exit", word) == 0)
return (BUILTIN_EXIT);
if (ft_strcmp("echo", word) == 0)
return (BUILTIN_ECHO);
return (BUILTIN_INVALID);
}
@ -38,6 +40,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
[BUILTIN_CD] = builtin_cd,
[BUILTIN_EXPORT] = builtin_export,
[BUILTIN_EXIT] = builtin_exit,
[BUILTIN_ECHO] = builtin_echo,
};
type = get_builtin(cmd);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */
/* Updated: 2025/04/01 18:17:50 by khais ### ########.fr */
/* Updated: 2025/04/03 13:58:23 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@ t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
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_exit(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_echo(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd);
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */
/* Updated: 2025/04/01 18:17:33 by khais ### ########.fr */
/* Updated: 2025/04/03 13:58:41 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,6 +25,7 @@ typedef enum e_builtin_type
BUILTIN_CD,
BUILTIN_EXPORT,
BUILTIN_EXIT,
BUILTIN_ECHO,
} t_builtin_type;
#endif // SIMPLE_CMD_EXECUTE_H