From 5c83326d9b989dc6cc2f355a727922028c839671 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Tue, 8 Mar 2022 20:49:33 +0100 Subject: [PATCH] set_cmd + exec work --- main.c | 14 +++++++++++++- pipe.c | 38 ++++++++++++++++---------------------- pipex_utils.c | 4 ++-- set_cmd.c | 9 +++++++-- 4 files changed, 38 insertions(+), 27 deletions(-) diff --git a/main.c b/main.c index fdd8b6c..cea8f1e 100644 --- a/main.c +++ b/main.c @@ -6,12 +6,22 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ -/* Updated: 2022/03/08 17:28:41 by apommier ### ########.fr */ +/* Updated: 2022/03/08 20:22:56 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +void red() +{ + printf("\033[1;31m"); +} + +void normal() +{ + printf("\033[0m"); +} + int main(int ac, char **av, char **path) { char *input; @@ -20,9 +30,11 @@ int main(int ac, char **av, char **path) printf("---MINISHELL START---\n"); while (1) { + red(); input = readline("$~ "); add_history(input); cmd = set_cmd(input, path); + normal(); execute(cmd); } return (0); diff --git a/pipe.c b/pipe.c index cef3bf6..c445fae 100644 --- a/pipe.c +++ b/pipe.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/07 11:13:32 by apommier #+# #+# */ -/* Updated: 2022/03/08 18:33:51 by apommier ### ########.fr */ +/* Updated: 2022/03/08 20:38:11 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,33 +20,26 @@ void execute(t_cmd *cmd) int tmpin = dup(0); int tmpout= dup(1); int fdin; - int fdpipe[2]; int i; i = 0; //set the initial input - if (cmd->current_s_cmd->infile) - { + if (cmd->current_s_cmd->infile)//redirection fdin = open(cmd->current_s_cmd->infile, O_RDONLY); - printf("good chosse -redirect infile- fdin= %d\n", fdin); - } - else if (cmd->infile) - { + else if (cmd->infile)//? fdin = open(cmd->infile, O_RDONLY); - printf("good chosse -infile- fdin= %d\n", fdin); - } else - { fdin=dup(tmpin); - printf("good chosse -standar in- fdin= %d\n", fdin); - } while( i < cmd->nb_s_cmd) { + //if (i) + // cmd->current_s_cmd++; if (i != 0 && cmd->current_s_cmd->infile) fdin = open(cmd->current_s_cmd->infile, O_RDONLY); //redirect input dup2(fdin, 0); close(fdin); + fdin = -1; //setup output if (i == cmd->nb_s_cmd - 1) { @@ -56,31 +49,33 @@ void execute(t_cmd *cmd) else if(cmd->outfile) fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); else// Use default output + { fdout=dup(tmpout); + } } else { //not last //simple command //create pipe - //int fdpipe[2]; + int fdpipe[2]; pipe(fdpipe); fdout=fdpipe[1]; fdin=fdpipe[0]; } - // Redirect output - dup2(fdout,1); + // redirect output + dup2(fdout, 1); close(fdout); - - // Create child process + // create child process + ret=fork(); - int return_exec; if(ret==0) { - return_exec = execvp(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args); - _exit(1); + execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, cmd->path); + //_exit(1); } i++; + cmd->current_s_cmd = cmd->s_cmds[i]; } //while //restore in/out defaults dup2(tmpin,0); @@ -90,5 +85,4 @@ void execute(t_cmd *cmd) // Wait for last command waitpid(ret, NULL, 0); - } // execute diff --git a/pipex_utils.c b/pipex_utils.c index 4abbe0d..6899cf0 100644 --- a/pipex_utils.c +++ b/pipex_utils.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 16:58:40 by apommier #+# #+# */ -/* Updated: 2022/03/08 18:32:29 by apommier ### ########.fr */ +/* Updated: 2022/03/08 20:40:43 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -111,7 +111,7 @@ char *get_command(char **exec, char **path) { printf("BAD PATH FOR CMD\n"); free_double(path); - exit(1); + return(0); } swap = does_access(path, exec); return (swap); diff --git a/set_cmd.c b/set_cmd.c index e97fc20..847ecec 100644 --- a/set_cmd.c +++ b/set_cmd.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ -/* Updated: 2022/03/08 18:33:53 by apommier ### ########.fr */ +/* Updated: 2022/03/08 20:41:42 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,9 +38,9 @@ t_s_cmd *set_s_cmd(char *line, t_cmd *cmd) return (0); } s_cmd->infile = 0; + s_cmd->outfile = 0; s_cmd->nb_args = double_size(split_line); s_cmd->args = split_line; - s_cmd->outfile = 0; return (s_cmd); } @@ -52,6 +52,11 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds) while (cmds[i]) { cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd); + if (!cmd->s_cmds[i]) + { + printf("get command crash"); + return (0); + } i++; } return (cmd);