set_cmd + exec work

This commit is contained in:
kinou-p 2022-03-08 20:49:33 +01:00
parent 43007c1c31
commit 5c83326d9b
4 changed files with 38 additions and 27 deletions

14
main.c
View File

@ -6,12 +6,22 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 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" #include "minishell.h"
void red()
{
printf("\033[1;31m");
}
void normal()
{
printf("\033[0m");
}
int main(int ac, char **av, char **path) int main(int ac, char **av, char **path)
{ {
char *input; char *input;
@ -20,9 +30,11 @@ int main(int ac, char **av, char **path)
printf("---MINISHELL START---\n"); printf("---MINISHELL START---\n");
while (1) while (1)
{ {
red();
input = readline("$~ "); input = readline("$~ ");
add_history(input); add_history(input);
cmd = set_cmd(input, path); cmd = set_cmd(input, path);
normal();
execute(cmd); execute(cmd);
} }
return (0); return (0);

36
pipe.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 11:13:32 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 tmpin = dup(0);
int tmpout= dup(1); int tmpout= dup(1);
int fdin; int fdin;
int fdpipe[2];
int i; int i;
i = 0; i = 0;
//set the initial input //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); 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); fdin = open(cmd->infile, O_RDONLY);
printf("good chosse -infile- fdin= %d\n", fdin);
}
else else
{
fdin=dup(tmpin); fdin=dup(tmpin);
printf("good chosse -standar in- fdin= %d\n", fdin);
}
while( i < cmd->nb_s_cmd) while( i < cmd->nb_s_cmd)
{ {
//if (i)
// cmd->current_s_cmd++;
if (i != 0 && cmd->current_s_cmd->infile) if (i != 0 && cmd->current_s_cmd->infile)
fdin = open(cmd->current_s_cmd->infile, O_RDONLY); fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
//redirect input //redirect input
dup2(fdin, 0); dup2(fdin, 0);
close(fdin); close(fdin);
fdin = -1;
//setup output //setup output
if (i == cmd->nb_s_cmd - 1) if (i == cmd->nb_s_cmd - 1)
{ {
@ -56,31 +49,33 @@ void execute(t_cmd *cmd)
else if(cmd->outfile) else if(cmd->outfile)
fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666); fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
else// Use default output else// Use default output
{
fdout=dup(tmpout); fdout=dup(tmpout);
}
} }
else else
{ {
//not last //not last
//simple command //simple command
//create pipe //create pipe
//int fdpipe[2]; int fdpipe[2];
pipe(fdpipe); pipe(fdpipe);
fdout=fdpipe[1]; fdout=fdpipe[1];
fdin=fdpipe[0]; fdin=fdpipe[0];
} }
// Redirect output // redirect output
dup2(fdout,1); dup2(fdout, 1);
close(fdout); close(fdout);
// create child process
// Create child process
ret=fork(); ret=fork();
int return_exec;
if(ret==0) if(ret==0)
{ {
return_exec = execvp(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args); execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, cmd->path);
_exit(1); //_exit(1);
} }
i++; i++;
cmd->current_s_cmd = cmd->s_cmds[i];
} //while } //while
//restore in/out defaults //restore in/out defaults
dup2(tmpin,0); dup2(tmpin,0);
@ -90,5 +85,4 @@ void execute(t_cmd *cmd)
// Wait for last command // Wait for last command
waitpid(ret, NULL, 0); waitpid(ret, NULL, 0);
} // execute } // execute

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 16:58:40 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"); printf("BAD PATH FOR CMD\n");
free_double(path); free_double(path);
exit(1); return(0);
} }
swap = does_access(path, exec); swap = does_access(path, exec);
return (swap); return (swap);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 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); return (0);
} }
s_cmd->infile = 0; s_cmd->infile = 0;
s_cmd->outfile = 0;
s_cmd->nb_args = double_size(split_line); s_cmd->nb_args = double_size(split_line);
s_cmd->args = split_line; s_cmd->args = split_line;
s_cmd->outfile = 0;
return (s_cmd); return (s_cmd);
} }
@ -52,6 +52,11 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
while (cmds[i]) while (cmds[i])
{ {
cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd); cmd->s_cmds[i] = set_s_cmd(cmds[i], cmd);
if (!cmd->s_cmds[i])
{
printf("get command crash");
return (0);
}
i++; i++;
} }
return (cmd); return (cmd);