From 954e8c47fb6232ab66de54bebeb550702ddb34fb Mon Sep 17 00:00:00 2001 From: kinou-p Date: Tue, 19 Apr 2022 12:41:00 +0200 Subject: [PATCH] fix norm --- includes/minishell.h | 27 +++++------ libft/ft_atoi.c | 4 +- libft/get_next_line.c | 7 +-- srcs/built_in/cd.c | 11 ++--- srcs/built_in/utils_builtin.c | 10 +--- srcs/main.c | 47 +++++++++--------- srcs/pipe/pipe.c | 89 ++++++++++++++++------------------- srcs/pipe/pipex_utils.c | 8 ++-- srcs/set_cmd/set_cmd.c | 12 ++--- 9 files changed, 97 insertions(+), 118 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 4f1c881..04aca42 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ -/* Updated: 2022/04/19 08:37:43 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:18:26 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,12 +31,12 @@ // Command Data Structure // Describes a simple command and arguments -typedef struct s_simple { +typedef struct s_simple { struct s_command *big_cmd; int fd[2]; int pipe[2]; int last; - int last_pipe[2]; + int last_pipe[2]; int child; int nb_args; char *infile; @@ -49,23 +49,21 @@ typedef struct s_simple { // Describes a complete command with the multiple pipes if any // and input/output redirection if any. -typedef struct s_command { +typedef struct s_command { int tmpin; int tmpout; char **env; int nb_s_cmd; - struct s_simple **s_cmds; - //char *outfile; - //char *infile; + struct s_simple **s_cmds; int err_var; - struct s_simple *current_s_cmd; + struct s_simple *current_s_cmd; char **path; } t_cmd; char *error_parsing(void); //main.c -int main();//int ac, char **av, char **path); +int main(int ac, char **av, char **path); char **ft_dup_double(char **env); //pipe.c @@ -79,8 +77,8 @@ char **get_path(char **env); char *get_command(char **exec, char **env); //free_cmd -void free_cmd(t_cmd *cmd); -void exit_shell(t_cmd *cmd, int ret); +void free_cmd(t_cmd *cmd); +void exit_shell(t_cmd *cmd, int ret); //set_quote.c char *set_var(t_cmd *big_cmd, char *cmd); @@ -89,7 +87,7 @@ int is_quote_good(char *str); char **ft_split_with_quote(char const *s, char c); //signals -void crtl_c(int num); +void crtl_c(int num); void sig_heredoc(int num); //redirection.c set redirection and input good @@ -102,7 +100,7 @@ char *get_str(char *str, int start, int end); char *cut_str(char *str, int start, int end); int double_size(char **tab); void print_double_fd(char **tab, int fd); - void free_double(char **tab); +void free_double(char **tab); //builtins utils void ft_shlvl(char **env); @@ -117,7 +115,6 @@ int find_variable(char *variable, t_s_cmd *cmd); int find_it(char **str, char *s); int check_variable(char *variable); int cd_error(t_s_cmd *cmd, char *str, int i); -int msg_error(t_s_cmd *cmd, char *cm, char *entry, char *error, int i); int size_path(char **str); int check_return(t_s_cmd *cmd, int var); @@ -133,7 +130,7 @@ int ft_pwd(t_s_cmd *cmd); int open_directory(t_s_cmd *cmd);//cd //parse builtin -int is_builtin(char *cmd); +int is_builtin(char *cmd); void call_builtin(t_cmd *cmd); #endif \ No newline at end of file diff --git a/libft/ft_atoi.c b/libft/ft_atoi.c index 1cd671f..f4b50d3 100644 --- a/libft/ft_atoi.c +++ b/libft/ft_atoi.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/11/29 00:09:17 by apommier #+# #+# */ -/* Updated: 2022/04/19 08:44:19 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:11:56 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ long long ft_atoi(const char *nptr) { - int i; + int i; long long nbr; long long minus; diff --git a/libft/get_next_line.c b/libft/get_next_line.c index c280600..0a96a12 100644 --- a/libft/get_next_line.c +++ b/libft/get_next_line.c @@ -70,10 +70,11 @@ char *next_line(char *save, int *end, int fd) char *get_next_line(int fd) { - char *save = NULL; - int *end; - char *line; + char *save; + int *end; + char *line; + save = 0; line = 0; if (fd < 0) return (0); diff --git a/srcs/built_in/cd.c b/srcs/built_in/cd.c index 127c681..0b644c7 100644 --- a/srcs/built_in/cd.c +++ b/srcs/built_in/cd.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */ -/* Updated: 2022/04/17 10:05:05 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:32:13 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -159,11 +159,10 @@ int open_directory(t_s_cmd *cmd) } else { - return (msg_error(cmd, "Minishell: cd: ", cmd->args[1], ": No such directory\n", 1)); - // ft_putstr_fd("Minishell: cd: ", 2); - // ft_putstr_fd(cmd->args[1], 2); - // ft_putstr_fd(": No such directory\n", 2); - // return ;//(1); + ft_putstr_fd("Minishell: cd: ", 2); + ft_putstr_fd(cmd->args[1], 2); + ft_putstr_fd(": No such directory\n", 2); + return (check_return(cmd, 1)); } } return (check_return(cmd, 0)); diff --git a/srcs/built_in/utils_builtin.c b/srcs/built_in/utils_builtin.c index 50c040b..37a933e 100644 --- a/srcs/built_in/utils_builtin.c +++ b/srcs/built_in/utils_builtin.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */ -/* Updated: 2022/04/18 03:15:44 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:18:18 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,14 +42,6 @@ int cd_error(t_s_cmd *cmd, char *str, int i) return (check_return(cmd, i)); } -int msg_error(t_s_cmd *cmd, char *cm, char *entry, char *error, int i) -{ - ft_putstr_fd(cm, 2); - ft_putstr_fd(entry, 2); - ft_putstr_fd(error, 2); - return (check_return(cmd, i)); -} - int size_path(char **str) { int i; diff --git a/srcs/main.c b/srcs/main.c index 27c201e..6b49cf1 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,13 +6,13 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ -/* Updated: 2022/04/19 12:05:20 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:22:04 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -void crtl_c(int num) +void crtl_c(int num) { num = 0; printf("\n"); @@ -30,31 +30,31 @@ void sig_quit(int num) char **ft_dup_double(char **env) { char **new_tab; - int i; + int i; - i = 0; + i = 0; if (!env) return (0); - while (env[i]) - i++; - new_tab = ft_calloc(sizeof(char *), i + 1); - i = 0; - while (env[i]) - { - new_tab[i] = ft_strjoin(env[i], 0); - i++; - } - new_tab[i] = NULL; + while (env[i]) + i++; + new_tab = ft_calloc(sizeof(char *), i + 1); + i = 0; + while (env[i]) + { + new_tab[i] = ft_strjoin(env[i], 0); + i++; + } + new_tab[i] = NULL; return (new_tab); } void print_prompt(char **path) { - char *input; - t_cmd *cmd; - int err_var; - struct sigaction test; - + char *input; + t_cmd *cmd; + int err_var; + struct sigaction test; + memset(&test, 0, sizeof(test)); test.sa_handler = &sig_quit; test.sa_flags = 0; @@ -77,13 +77,12 @@ void print_prompt(char **path) add_history(input); if (ft_strlen(input) && next_space(input, 0) && input && path) { - cmd = set_cmd(input, path, err_var); + cmd = set_cmd(input, path, err_var); if (cmd) { free_double(path); execute(cmd, cmd->env); err_var = cmd->err_var; - path = ft_dup_double(cmd->env); free_cmd(cmd); cmd = 0; @@ -95,8 +94,8 @@ void print_prompt(char **path) int main(int ac, char **av, char **path) { - char **env; - + char **env; + if (!isatty(0)) { printf("Not today\n"); @@ -115,4 +114,4 @@ int main(int ac, char **av, char **path) ft_shlvl(env); print_prompt(env); return (0); -} \ No newline at end of file +} diff --git a/srcs/pipe/pipe.c b/srcs/pipe/pipe.c index d121c0d..4f3dfc5 100644 --- a/srcs/pipe/pipe.c +++ b/srcs/pipe/pipe.c @@ -6,16 +6,15 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/04/02 18:51:31 by apommier #+# #+# */ -/* Updated: 2022/04/19 12:00:46 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:30:01 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../includes/minishell.h" - void close_pipe(t_cmd *cmd) { - int i; + int i; i = 0; close(0); @@ -29,7 +28,7 @@ void close_pipe(t_cmd *cmd) int wait_exit(t_cmd *cmd) { - int i; + int i; int ret; int exit_pid; int status; @@ -38,33 +37,33 @@ int wait_exit(t_cmd *cmd) ret = 0; exit_pid = 1; while (exit_pid > 0) - { - i = 0; + { + i = 0; exit_pid = wait(&status); - while (exit_pid != -1 && cmd->s_cmds[i]) - { - if (exit_pid == cmd->s_cmds[i]->child) - { + while (exit_pid != -1 && cmd->s_cmds[i]) + { + if (exit_pid == cmd->s_cmds[i]->child) + { close(cmd->s_cmds[i]->fd[0]); close(cmd->s_cmds[i]->fd[1]); - if (WIFEXITED(status)) - cmd->err_var = WEXITSTATUS(status); - else if (WIFSIGNALED(status)) - { - if (WTERMSIG(status) != 13) - cmd->err_var = WTERMSIG(status); - if (WTERMSIG(status) == 3) - ft_putstr_fd("^\\Quit", 1); - ft_putstr_fd("\b\b\b\b\b", 1); - } + if (WIFEXITED(status)) + cmd->err_var = WEXITSTATUS(status); + else if (WIFSIGNALED(status)) + { + if (WTERMSIG(status) != 13) + cmd->err_var = WTERMSIG(status); + if (WTERMSIG(status) == 3) + ft_putstr_fd("^\\Quit", 1); + ft_putstr_fd("\b\b\b\b\b", 1); + } } i++; - } - } + } + } return (ret); } -void exec_cmd(t_cmd *cmd, char **env, int *fdpipe) +void exec_cmd(t_cmd *cmd, char **env, int *fdpipe) { cmd->current_s_cmd->child = fork(); if (cmd->current_s_cmd->child == 0) @@ -92,10 +91,10 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe) free_cmd(cmd); exit(127); } - if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env)) + if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env)) ft_putstr_fd("Minishell: exec error\n", 2); - exit(126); - } + exit(126); + } else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK)) cmd->err_var = 127; else @@ -104,21 +103,15 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe) void execute(t_cmd *cmd, char **env) { - int fdpipe[2]; - int fdout; - int fdin; - int tmpin = -1; - int tmpout = -1; - int i; + int fdpipe[2]; + int fdout; + int fdin; + int i; - tmpin = -1; - tmpout = -1; i = 0; fdpipe[1] = -1; - tmpin = dup(0); - tmpout = dup(1); - cmd->tmpin = tmpin; - cmd->tmpout = tmpout; + cmd->tmpin = dup(0); + cmd->tmpout = dup(1); if (cmd->current_s_cmd->infile) { fdin = open(cmd->current_s_cmd->infile, O_RDWR); @@ -126,8 +119,8 @@ void execute(t_cmd *cmd, char **env) printf("Minishell: open : bad file descriptor\n"); } else - fdin = dup(tmpin); - while(cmd->current_s_cmd) + fdin = dup(cmd->tmpin); + while (cmd->current_s_cmd) { cmd->current_s_cmd->child = 1; fdout = -1; @@ -144,7 +137,7 @@ void execute(t_cmd *cmd, char **env) if (cmd->current_s_cmd->outfile) fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666); else - fdout = dup(tmpout); + fdout = dup(cmd->tmpout); cmd->current_s_cmd->fd[0] = fdin; cmd->current_s_cmd->fd[1] = fdout; if (i == 0 && is_builtin(cmd->current_s_cmd->cmd)) @@ -152,7 +145,7 @@ void execute(t_cmd *cmd, char **env) else exec_cmd(cmd, env, 0); } - else + else { cmd->current_s_cmd->last = 0; pipe(fdpipe); @@ -164,7 +157,7 @@ void execute(t_cmd *cmd, char **env) } else cmd->current_s_cmd->fd[1] = fdpipe[1]; - fdin=fdpipe[0]; + fdin = fdpipe[0]; exec_cmd(cmd, env, fdpipe); close(cmd->current_s_cmd->fd[0]); } @@ -174,9 +167,9 @@ void execute(t_cmd *cmd, char **env) cmd->current_s_cmd = cmd->s_cmds[i]; } close_pipe(cmd); - dup2(tmpin, 0); - dup2(tmpout, 1); - close(tmpin); - close(tmpout); + dup2(cmd->tmpin, 0); + dup2(cmd->tmpout, 1); + close(cmd->tmpin); + close(cmd->tmpout); wait_exit(cmd); -} \ No newline at end of file +} diff --git a/srcs/pipe/pipex_utils.c b/srcs/pipe/pipex_utils.c index 68cae9a..306f027 100644 --- a/srcs/pipe/pipex_utils.c +++ b/srcs/pipe/pipex_utils.c @@ -6,14 +6,12 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 16:58:40 by apommier #+# #+# */ -/* Updated: 2022/04/19 12:01:06 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:34:06 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../includes/minishell.h" - - char **get_path(char **env) { int i; @@ -79,7 +77,7 @@ char *get_command(char **exec, char **path) if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK)) return (exec[0]); else if (exec[0][0] == '/') - return(0); + return (0); swap = does_access(path, exec); return (swap); -} \ No newline at end of file +} diff --git a/srcs/set_cmd/set_cmd.c b/srcs/set_cmd/set_cmd.c index 0876121..463ce23 100644 --- a/srcs/set_cmd/set_cmd.c +++ b/srcs/set_cmd/set_cmd.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ -/* Updated: 2022/04/19 12:01:46 by apommier ### ########.fr */ +/* Updated: 2022/04/19 12:35:26 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,9 +22,9 @@ t_s_cmd *set_s_cmd(char *line, int index) { t_s_cmd *s_cmd; char **split_line; - int i; + int i; - i = 0; + i = 0; s_cmd = malloc(sizeof(t_s_cmd)); if (!s_cmd) return (0); @@ -46,7 +46,7 @@ t_s_cmd *set_s_cmd(char *line, int index) return (s_cmd); } -t_cmd *split_cmd(t_cmd *cmd, char **cmds) +t_cmd *split_cmd(t_cmd *cmd, char **cmds) { int i; @@ -68,8 +68,8 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds) int is_pipe_good(char *str) { - int i; - char next; + int i; + char next; i = 0; while (str[i])