Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
fix echo (accept -n multiple times; fix github action
Browse files Browse the repository at this point in the history
  • Loading branch information
nicknamemohaji committed Apr 9, 2024
1 parent 6a2be4b commit e75c563
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions codes/builtins/builtin_echo.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
/* By: kyungjle <kyungjle@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 12:45:54 by kyungjle #+# #+# */
/* Updated: 2024/04/09 14:55:55 by kyungjle ### ########.fr */
/* Updated: 2024/04/09 18:01:44 by kyungjle ### ########.fr */
/* */
/* ************************************************************************** */

#include "builtin.h"

int builtin_echo(char *args[], t_ld_map_env *env);
int builtin_echo(char *args[], t_ld_map_env *env);
static t_bool parse_args(char *args[], int *index);

/*
int builtin_echo(char *args[], t_ld_map_env *env)
Expand All @@ -22,24 +23,43 @@ int builtin_echo(char *args[], t_ld_map_env *env)
*/
int builtin_echo(char *args[], t_ld_map_env *env)
{
int i;
int index;
t_bool print_nl;

(void) env;
i = 1;
if (args[1] != NULL
&& ft_strlen(args[1]) == 2 && ft_strncmp(args[1], "-n", 2) == 0)
i += 1;
while (args[i] != NULL)
print_nl = parse_args(args, &index);
while (args[index] != NULL)
{
printf("%s", args[i]);
if (args[i + 1] != NULL)
printf("%s", args[index]);
if (args[index + 1] != NULL)
printf(" ");
i++;
index++;
}
if (args[1] != NULL
&& !(ft_strlen(args[1]) == 2 && ft_strncmp(args[1], "-n", 2) == 0))
printf("\n");
if (args[1] == NULL)
if (print_nl || args[1] == NULL)
printf("\n");
return (EXIT_SUCCESS);
}

static t_bool parse_args(char *args[], int *index)
{
t_bool ret;
int i;

ret = TRUE;
*index = 1;
while (args[*index] != NULL)
{
if (args[*index][0] != '-')
break ;
i = 1;
while (args[*index][i] != '\0')
{
if (args[*index][i] != 'n')
return (ret);
i += 1;
}
ret = FALSE;
*index += 1;
}
return (ret);
}

0 comments on commit e75c563

Please sign in to comment.