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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

38
pipe.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);