From 35f6c8bbaa4993cc1f9c89430d7571b270e644bd Mon Sep 17 00:00:00 2001 From: kinou-p Date: Tue, 8 Mar 2022 18:05:39 +0100 Subject: [PATCH] set_cmd + ecex --- Makefile | 5 ++- main.c | 8 ++-- minishell.h | 16 ++++--- pipe.c | 60 +++++++++++++------------ pipex_utils.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++ set_cmd.c | 39 ++++++++++++----- 6 files changed, 199 insertions(+), 48 deletions(-) create mode 100644 pipex_utils.c diff --git a/Makefile b/Makefile index 2df6079..ddfeda6 100644 --- a/Makefile +++ b/Makefile @@ -6,13 +6,14 @@ # By: apommier +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2022/03/06 12:50:24 by apommier #+# #+# # -# Updated: 2022/03/08 15:20:19 by apommier ### ########.fr # +# Updated: 2022/03/08 17:30:48 by apommier ### ########.fr # # # # **************************************************************************** # - NAME = minishell SRCS = main.c\ + pipe.c\ + pipex_utils.c\ set_cmd.c OBJS = ${SRCS:.c=.o} CC = clang diff --git a/main.c b/main.c index 5cde8f7..fdd8b6c 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ -/* Updated: 2022/03/08 15:20:48 by apommier ### ########.fr */ +/* Updated: 2022/03/08 17:28:41 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,15 +15,15 @@ int main(int ac, char **av, char **path) { char *input; + t_cmd *cmd; printf("---MINISHELL START---\n"); - - execute(t_cmd *cmd, *infile, *outfile); while (1) { input = readline("$~ "); add_history(input); - set_cmd(input); + cmd = set_cmd(input, path); + execute(cmd); } return (0); } diff --git a/minishell.h b/minishell.h index 76579a7..0a87f3a 100644 --- a/minishell.h +++ b/minishell.h @@ -21,8 +21,8 @@ # include # include # include +#include # include -# include # include # include @@ -42,10 +42,11 @@ typedef struct s_simple { typedef struct s_command { int nb_s_cmd; struct s_simple **s_cmds; - char *out_file; - char *input_file; + char *outfile; + char *infile; char *err_file; struct s_simple *current_s_cmd; + char **path; } t_cmd; //main.c @@ -54,7 +55,12 @@ int main();//int ac, char **av, char **path); //pipe.c void execute(t_cmd *cmd); -//set_command -t_cmd *set_cmd(char *input); +//set_cmd.c +t_cmd *set_cmd(char *input, char **path); + +//pipex_utils.c +char **get_path(char **env); +char *get_command(char **exec, char **env); +void print_double(char **tab); #endif \ No newline at end of file diff --git a/pipe.c b/pipe.c index b1c2690..a33d8ff 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 14:53:42 by apommier ### ########.fr */ +/* Updated: 2022/03/08 17:16:44 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,72 +24,78 @@ void execute(t_cmd *cmd) int i; i = 0; + printf("in execute\n"); //set the initial input - if (current_s_cmd->cmd->infile) - fdin = open(current_s_cmd->cmd->infile, O_READ); + if (cmd->current_s_cmd->infile) + { + fdin = open(cmd->current_s_cmd->infile, O_RDONLY); + printf("good chosse -redirect infile- fdin= %d\n", fdin); + } else if (cmd->infile) - fdin = open(cmd->infile,O_READ); + { + 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 != 0 && current_s_cmd->cmd->infile) - fdin = open(current_s_cmd->cmd->infile,O_READ); + printf("enter while\n"); + if (i != 0 && cmd->current_s_cmd->infile) + fdin = open(cmd->current_s_cmd->infile, O_RDONLY); //redirect input dup2(fdin, 0); close(fdin); //setup output if (i == cmd->nb_s_cmd - 1) { + printf("if last cmd\n"); // Last simple command - if (current_s_cmd->cmd->outfile) - fdout = open(current_s_cmd->cmd->outfile, ……); + if (cmd->current_s_cmd->outfile) + fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); else if(cmd->outfile) - fdout=open(cmd->outfile, ……); + fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); else// Use default output fdout=dup(tmpout); } else { - // Not last + //not last //simple command //create pipe //int fdpipe[2]; pipe(fdpipe); fdout=fdpipe[1]; fdin=fdpipe[0]; - }// if/else - + } + printf("select if last or not done \n"); // Redirect output dup2(fdout,1); close(fdout); // Create child process - ret=fork(); + ret=fork(); + int return_exec; if(ret==0) { - execvp(scmd[i].args[0], scmd[i].args); - perror(“execvpâ€); + return_exec = execvp(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args); + printf("exec done ret exec= %d\n", return_exec); _exit(1); } i++; + printf("good i = %d\n", i); } //while - + printf("quit while\n"); //restore in/out defaults dup2(tmpin,0); dup2(tmpout,1); close(tmpin); close(tmpout); - - if (!background) - { - // Wait for last command - waitpid(ret, NULL); - } + + // Wait for last command + waitpid(ret, NULL, 0); } // execute - -int main(int argc, char **argv) -{ - execute(t_cmd *cmd, char *infile, char *outfile); -} \ No newline at end of file diff --git a/pipex_utils.c b/pipex_utils.c new file mode 100644 index 0000000..4527151 --- /dev/null +++ b/pipex_utils.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pipex_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: apommier +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */ +/* Updated: 2022/03/08 18:02:15 by apommier ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void free_double(char **tab) +{ + int i; + + i = 0; + if (tab) + { + while (tab[i]) + free(tab[i++]); + free(tab); + } +} + +void print_double(char **tab) +{ + int i; + + i = 0; + if (tab) + { + while (tab[i]) + { + printf("%d -%s-\n", i, tab[i]); + i++; + } + printf("end double\n"); + } +} + +char **get_path(char **env) +{ + int i; + char **line; + char *swap; + + i = 0; + line = 0; + swap = 0; + while (env[i++] && env[i]) + { + if (!ft_strncmp(env[i], "PATH=", 5)) + { + swap = ft_substr(env[i], 5, ft_strlen(env[i])); + if (!swap) + exit(1); + line = ft_split(swap, ':'); + if (!line) + exit(1); + free(swap); + return (line); + } + free(line); + } + return (0); +} + + +//verify access with cmd access +char *does_access(char **path, char **exec) +{ + int i; + char *cmd; + char *swap; + + swap = 0; + cmd = 0; + i = 0; + if (exec[0][0] != '/') + swap = ft_strjoin(path[i], "/"); + while (access(swap, F_OK) && path[i++]) + { + free(swap); + swap = ft_strjoin(path[i], "/"); + cmd = swap; + swap = ft_strjoin(swap, exec[0]); + free(cmd); + } + if (path[i]) + return (swap); + return (0); +} + +char *get_command(char **exec, char **path) +{ + char *swap; + + swap = 0; + printf("in get command exec=\n"); + print_double(exec); + if (exec[0][0] == '/' && !access(exec[0], F_OK)) + { + printf("first\n"); + free_double(path); + return (exec[0]); + } + else if (exec[0][0] == '/') + { + printf("BAD PATH FOR CMD\n"); + free_double(path); + exit(1); + } + swap = does_access(path, exec); + printf("swap bfore ret= %s\n", swap); + return (swap); +} \ No newline at end of file diff --git a/set_cmd.c b/set_cmd.c index 7e658a3..e5e1a67 100644 --- a/set_cmd.c +++ b/set_cmd.c @@ -6,10 +6,12 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ -/* Updated: 2022/03/08 15:19:47 by apommier ### ########.fr */ +/* Updated: 2022/03/08 18:04:06 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ +#include "minishell.h" + int double_size(char **tab) { int i; @@ -20,16 +22,28 @@ int double_size(char **tab) return (i); } -s_cmds *set_s_cmd(char *line) +t_s_cmd *set_s_cmd(char *line, t_cmd *cmd) { - t_s_cmd s_cmd; - tmp **split_line; + t_s_cmd *s_cmd; + char **split_line; int i; i = 0; split_line = ft_split(line, ' '); - s_cmd.cmd = split_line[i]; - + s_cmd = malloc(sizeof(t_s_cmd)); + s_cmd->cmd = get_command(split_line, cmd->path); + printf("scmd= %s\n", s_cmd->cmd); + if (!s_cmd->cmd) + { + printf("get command crash"); + return (0); + } + s_cmd->infile = 0; + s_cmd->nb_args = double_size(split_line); + s_cmd->args = split_line; + printf("args= %s\n", *(split_line)); + s_cmd->outfile = 0; + return (s_cmd); } t_cmd *split_cmd(t_cmd *cmd, char **cmds) @@ -37,15 +51,15 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds) int i; i = 0; - while (cmd->s_cmds[i]) + while (cmds[i]) { - cmd->s_cmds[i] = set_s_cmd(cmds[i]) + cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd); i++; } return (cmd); } -t_cmd *set_cmd(char *input) +t_cmd *set_cmd(char *input, char **env) { t_cmd *cmd; char **cmds; @@ -56,9 +70,14 @@ t_cmd *set_cmd(char *input) cmd = malloc(sizeof(t_cmd)); if (!cmd) return (0); - cmd->s_cmds = malloc(sizeof(t_s_cmds) * double_size(cmds)); + cmd->s_cmds = malloc(sizeof(t_s_cmd) * double_size(cmds)); if (!cmd->s_cmds) return (0); + cmd->path = get_path(env); + cmd->outfile = 0; + cmd->infile = 0; cmd->nb_s_cmd = double_size(cmds); cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd + cmd->current_s_cmd = cmd->s_cmds[0]; + return (cmd); } \ No newline at end of file