diff --git a/Makefile b/Makefile index 5d140a2..72e1f82 100755 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ NAME = minishell LIBFT = libft.a -SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c +SRCS = minishell.c lexer.c parser.c utils.c caller.c built-in.c env.c errors.c OBJS = ${SRCS:.c=.o} diff --git a/built-in.c b/built-in.c index 683aa63..564000c 100755 --- a/built-in.c +++ b/built-in.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/01/06 09:02:57 by narnaud #+# #+# */ -/* Updated: 2022/05/03 18:18:23 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 13:37:47 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,13 @@ int ft_echo(t_command *cmd) { - int no_nl; + int no_nl; + int fd_out; + + if (cmd->fd[1]) + fd_out = cmd->fd[1]; + else + fd_out = 1; no_nl = 0; if (*(++cmd->argv)) @@ -23,25 +29,29 @@ int ft_echo(t_command *cmd) no_nl = 1; while (*(cmd->argv)) { - ft_putstr_fd(*(cmd->argv), 1); + ft_putstr_fd(*(cmd->argv), fd_out); if (*(++cmd->argv)) - ft_putstr_fd(" ", 1); + ft_putstr_fd(" ", fd_out); } } if (!no_nl) - ft_putstr_fd("\n", 1); + ft_putstr_fd("\n", fd_out); return (0); } int ft_pwd(t_command *cmd) { char *dir; + int fd_out; - (void)cmd; + if (cmd->fd[1]) + fd_out = cmd->fd[1]; + else + fd_out = 1; dir = ft_calloc(PATHS_MAX_SIZE, sizeof(char)); getcwd(dir, PATHS_MAX_SIZE); - ft_putstr_fd(dir, 1); - ft_putstr_fd("\n", 1); + ft_putstr_fd(dir, fd_out); + ft_putstr_fd("\n", fd_out); free(dir); return (0); } @@ -51,15 +61,9 @@ int ft_cd(t_command *cmd) if (cmd->argv[1]) { if (access(cmd->argv[1], X_OK)) - { - printf("Minishell: %s: %s: Permission denied\n", cmd->argv[0], cmd->argv[1]); - return (0); - } + return(file_error(cmd->argv[0], cmd->argv[1], "Permission denied\n")); else if (access(cmd->argv[1], F_OK)) - { - printf("Minishell: %s: %s: Not a directory\n", cmd->argv[0], cmd->argv[1]); - return (0); - } + return(file_error(cmd->argv[0], cmd->argv[1], "Not a directory\n")); chdir(cmd->argv[1]); return (1); } diff --git a/caller.c b/caller.c index b6c1554..c32dd1f 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/06 12:14:27 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 13:32:33 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int builtin_call(t_datas *datas, t_command *cmd) else if (ft_strncmp(cmd->argv[0], "unset", 6) == 0) return (ft_unset(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "env", 4) == 0) - return (ft_env(datas)); + return (ft_env(datas, cmd)); else if (ft_strncmp(cmd->argv[0], "exit", 5) == 0) return (close_minishell(0)); else @@ -78,12 +78,14 @@ pid_t caller(t_datas *datas, t_command *cmd) pid = fork(); if (!pid) { - if (cmd->fd[0]) - dup2(cmd->fd[0], STDIN_FILENO); - if (cmd->fd[1]) - dup2(cmd->fd[1], STDOUT_FILENO); if (builtin_call(datas, cmd) == -1) + { + if (cmd->fd[0]) + dup2(cmd->fd[0], STDIN_FILENO); + if (cmd->fd[1]) + dup2(cmd->fd[1], STDOUT_FILENO); exe(datas, cmd); + } exit(EXIT_SUCCESS); } return pid; diff --git a/env.c b/env.c index 39eb825..20fe4eb 100755 --- a/env.c +++ b/env.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/16 09:41:29 by narnaud #+# #+# */ -/* Updated: 2022/05/05 09:30:51 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 13:39:03 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,14 +38,20 @@ int ft_unset(t_datas *datas, t_command *cmd) return (0); } -int ft_env(t_datas *datas) +int ft_env(t_datas *datas, t_command *cmd) { int i; + int fd_out; + if (cmd->fd[1]) + fd_out = cmd->fd[1]; + else + fd_out = 1; i = 0; while (datas->envp[i]) { - printf("%s\n", datas->envp[i]); + ft_putstr_fd(datas->envp[i], fd_out); + ft_putchar_fd('\n', fd_out); i++; } return (0); @@ -61,7 +67,9 @@ int ft_export(t_datas *datas, t_command *cmd) new = ft_split(cmd->argv[1], '='); if (!is_valid_identifier(new[0])) { - printf ("export: not an identifier: %s\n", new[0]); + ft_putstr_fd("export: not an identifier: ", 2); + ft_putstr_fd(new[0], 2); + ft_putchar_fd('\n', 2); return (0); } while (datas->envp[i]) @@ -78,7 +86,3 @@ int ft_export(t_datas *datas, t_command *cmd) datas->envp[i + 1] = NULL; return (1); } - - - - diff --git a/errors.c b/errors.c new file mode 100644 index 0000000..0453235 --- /dev/null +++ b/errors.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* errors.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/06 13:15:59 by narnaud #+# #+# */ +/* Updated: 2022/05/06 13:25:32 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int file_error(char *cmd, char *file, char *msg) +{ + ft_putstr_fd("Minishell: ", 2); + ft_putstr_fd(cmd, 2); + ft_putstr_fd(": ", 2); + ft_putstr_fd(file, 2); + ft_putstr_fd(": ", 2); + ft_putstr_fd(msg, 2); + ft_putchar_fd('\n', 2); + return (0); +} diff --git a/minishell.h b/minishell.h index 414445b..78a168c 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/06 11:49:32 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 13:30:59 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,8 @@ typedef struct s_datas char **envp; int exit_code; } t_datas; +// ----------------------------------Errors.c +int file_error(char *cmd, char *file, char *msg); // ----------------------------------Utils.c int is_empty(char *line); @@ -63,7 +65,7 @@ int ft_cd(t_command *cmd); // ----------------------------------Env.c int is_valid_identifier(char *ident); int ft_unset(t_datas *datas, t_command *cmd); -int ft_env(t_datas *datas); +int ft_env(t_datas *datas, t_command *cmd); int ft_export(t_datas *datas, t_command *cmd); // ----------------------------------Caller.c diff --git a/utils.c b/utils.c index 6ce579c..7a8c965 100755 --- a/utils.c +++ b/utils.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 08:57:53 by narnaud #+# #+# */ -/* Updated: 2022/05/06 12:13:48 by narnaud ### ########.fr */ +/* Updated: 2022/05/06 13:15:33 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */