mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
echo: implement builtin echo
This commit is contained in:
parent
715c2aced8
commit
0de583cf45
6 changed files with 97 additions and 3 deletions
61
src/executing/simple_cmd/builtin_echo.c
Normal file
61
src/executing/simple_cmd/builtin_echo.c
Normal 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);
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue