update: added echo builtin
This commit is contained in:
32
src/builtins/echo/echo.c
Normal file
32
src/builtins/echo/echo.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "builtins.h"
|
||||
#include "echo_def.h"
|
||||
|
||||
u_int8_t builtin_echo(
|
||||
t_command cmd
|
||||
){
|
||||
const t_args args = read_args(cmd);
|
||||
size_t i;
|
||||
|
||||
i = -1;
|
||||
while (args.strings.data.sp[++i])
|
||||
{
|
||||
ft_putstr(args.strings.data.sp[i]);
|
||||
if (args.strings.data.sp[i + 1])
|
||||
ft_putchar(SPACE);
|
||||
}
|
||||
if (args.no_newline.data.b == false)
|
||||
ft_putchar(NEWLINE);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
69
src/builtins/echo/echo_def.c
Normal file
69
src/builtins/echo/echo_def.c
Normal file
@@ -0,0 +1,69 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo_def.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/01 20:06:30 by sede-san #+# #+# */
|
||||
/* Updated: 2025/11/02 03:11:01 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "echo_def.h"
|
||||
|
||||
static void assign_flags(t_args *args, t_command cmd, size_t *i);
|
||||
static u_int8_t is_valid_flag(t_args *args, char *flag);
|
||||
|
||||
static void assign_default_flags(
|
||||
t_args *args
|
||||
){
|
||||
args->no_newline = (t_arg){
|
||||
.short_name = 'n', .long_name = NULL,
|
||||
.data = {.b = false}
|
||||
};
|
||||
}
|
||||
|
||||
t_args read_args(
|
||||
t_command cmd
|
||||
){
|
||||
t_args args;
|
||||
size_t i;
|
||||
|
||||
assign_default_flags(&args);
|
||||
i = 0;
|
||||
assign_flags(&args, cmd, &i);
|
||||
args.strings.data.sp = &cmd.argv[i];
|
||||
return (args);
|
||||
}
|
||||
|
||||
static void assign_flags(
|
||||
t_args *args,
|
||||
t_command cmd,
|
||||
size_t *i
|
||||
){
|
||||
while (cmd.argv[++(*i)] && ft_strchr(cmd.argv[*i], '-'))
|
||||
{
|
||||
if (!is_valid_flag(args, cmd.argv[*i]))
|
||||
break ;
|
||||
else
|
||||
args->no_newline.data.b = true;
|
||||
}
|
||||
}
|
||||
|
||||
static u_int8_t is_valid_flag(
|
||||
t_args *args,
|
||||
char *flag
|
||||
){
|
||||
size_t i;
|
||||
|
||||
if (!flag[1])
|
||||
return (0);
|
||||
i = 0;
|
||||
while (flag[++i])
|
||||
{
|
||||
if (flag[i] != args->no_newline.short_name)
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
32
src/builtins/echo/echo_def.h
Normal file
32
src/builtins/echo/echo_def.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* echo_def.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: sede-san <sede-san@student.42madrid.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/10/31 10:31:08 by sede-san #+# #+# */
|
||||
/* Updated: 2025/11/02 03:11:10 by sede-san ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef ECHO_DEF_H
|
||||
# define ECHO_DEF_H
|
||||
|
||||
# include "builtins.h"
|
||||
|
||||
typedef struct s_args
|
||||
{
|
||||
t_arg no_newline;
|
||||
t_arg strings;
|
||||
} t_args;
|
||||
|
||||
/******************************************************************************/
|
||||
/* Functions */
|
||||
/******************************************************************************/
|
||||
|
||||
// echo_def.c
|
||||
|
||||
extern t_args read_args(t_command cmd);
|
||||
|
||||
#endif /* ECHO_DEF_H */
|
||||
Reference in New Issue
Block a user