」 feat: echo done(hopefully)

This commit is contained in:
2024-06-29 20:05:18 +02:00
parent 26b8603933
commit 817ed058dd
6 changed files with 80 additions and 7 deletions

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 13:05:18 by adjoly #+# #+# */
/* Updated: 2024/06/27 15:38:17 by mmoussou ### ########.fr */
/* Updated: 2024/06/29 19:39:01 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@
void ft_pwd(void);
void ft_cd(t_env *env, char *args);
void ft_echo(char **args);
char *ret_cwd(void);
void ft_env(t_env *env);

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */
/* Updated: 2024/06/24 10:54:36 by mmoussou ### ########.fr */
/* Updated: 2024/06/29 16:25:47 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@
# define ERROR_SYNTAX ": syntax error"
# define ERROR_NO_REDIR ": need redirection file"
# define ERROR_NO_EOF ": need delimiter to heredoc"
# define ERROR_NO_FILE "No such file or directory"
# define ERROR_COREDUMP "(core dumped)"

@ -6,10 +6,11 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 15:07:24 by adjoly #+# #+# */
/* Updated: 2024/06/25 17:13:46 by adjoly ### ########.fr */
/* Updated: 2024/06/29 16:36:56 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "error_msg.h"
#include "minishell.h"
#include "env.h"
#include "libft.h"
@ -39,7 +40,7 @@ char *__relative_path(char *args, char *pwd)
path = ft_split(args, '/');
tmp = path;
new_path = ft_strdup(pwd);
new_path = pwd;
if (!new_path)
return (NULL);
while (*tmp)
@ -54,7 +55,9 @@ char *__relative_path(char *args, char *pwd)
}
tmp++;
}
return (new_path);
*tmp = ft_strdup(new_path);
ft_free("a", path);
return (*tmp);
}
void ft_cd(t_env *env, char *args)
@ -75,8 +78,11 @@ void ft_cd(t_env *env, char *args)
new_pwd = __relative_path(args, pwd);
ret = chdir(new_pwd);
if (ret == -1)
{
printf("./minishell: cd: %s: %s\n", args, ERROR_NO_FILE);
return ;
env_edit("PWD", new_pwd, env);
}
env_edit("PWD", ft_strdup(new_pwd), env);
env_edit("OLDPWD", ft_strdup(pwd), env);
free(args);
}

60
src/builtins/ft_echo.c Normal file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 19:19:57 by adjoly #+# #+# */
/* Updated: 2024/06/29 20:04:36 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdbool.h>
//#include <stdlib.h>
#include "libft.h"
#include <stdio.h>
size_t __nl_option(char **args)
{
char **tmp;
char *tmp_arg;
size_t i;
tmp = args;
i = 0;
while ((*tmp)[0] == '-' && (*tmp)[1] == 'n')
{
tmp_arg = (*tmp);
tmp_arg++;
while (*tmp_arg == 'n')
tmp_arg++;
if (*tmp_arg)
break ;
i++;
tmp++;
}
return (i);
}
void ft_echo(char **args)
{
char **tmp;
bool new_line;
tmp = args;
if (__nl_option(args) > 0)
new_line = false;
else
new_line = true;
tmp += __nl_option(args);
while (*tmp)
{
ft_putstr_fd(*tmp, STDOUT_FILENO);
tmp++;
if (*tmp != NULL)
ft_putchar_fd(' ', STDOUT_FILENO);
}
if (new_line == true)
ft_putchar_fd('\n', STDOUT_FILENO);
}

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/06/29 15:34:53 by adjoly ### ########.fr */
/* Updated: 2024/06/29 19:44:30 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -129,6 +129,11 @@ int main(int ac, char **av, char **env)
ft_cd(&env_l, lll[1]);
continue ;
}
else if (is_str(test, "echo"))
{
ft_echo(lll + 1);
continue ;
}
else if (is_str(test, "exit"))
exit(EXIT_SUCCESS);
check_quote(test);