fix all?
This commit is contained in:
parent
4495ac459c
commit
af92795001
@ -6,7 +6,7 @@
|
|||||||
/* 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/04/16 02:50:22 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:08:28 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -72,14 +72,16 @@ void print_prompt(char **path)
|
|||||||
if (ft_strlen(input) && next_space(input, 0) && input)
|
if (ft_strlen(input) && next_space(input, 0) && input)
|
||||||
{
|
{
|
||||||
cmd = set_cmd(input, path, err_var);
|
cmd = set_cmd(input, path, err_var);
|
||||||
|
//path = ft_dup_double(path);
|
||||||
if (cmd)
|
if (cmd)
|
||||||
{
|
{
|
||||||
//cmd->err_var = 0;
|
//cmd->err_var = 0;
|
||||||
execute(cmd, path);
|
execute(cmd, path);
|
||||||
err_var = cmd->err_var;
|
err_var = cmd->err_var;
|
||||||
|
free_double(path);
|
||||||
path = ft_dup_double(cmd->env);
|
path = ft_dup_double(cmd->env);
|
||||||
|
//cmd = 0;
|
||||||
free_cmd(cmd);
|
free_cmd(cmd);
|
||||||
cmd = 0;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
|
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/15 06:29:09 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:34:00 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -91,11 +91,11 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
|
|||||||
ft_putstr_fd("Minishell: command not found: ", 2);
|
ft_putstr_fd("Minishell: command not found: ", 2);
|
||||||
ft_putstr_fd(cmd->current_s_cmd->cmd + 1, 2);
|
ft_putstr_fd(cmd->current_s_cmd->cmd + 1, 2);
|
||||||
ft_putstr_fd("\n", 2);
|
ft_putstr_fd("\n", 2);
|
||||||
exit(0);
|
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);
|
ft_putstr_fd("Minishell: exec error\n", 2);
|
||||||
exit(0);
|
exit(126);
|
||||||
}
|
}
|
||||||
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||||
cmd->err_var = 127;
|
cmd->err_var = 127;
|
||||||
@ -112,6 +112,7 @@ void execute(t_cmd *cmd, char **env)
|
|||||||
int tmpout;
|
int tmpout;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
//printf("exec\n");
|
||||||
i = 0;
|
i = 0;
|
||||||
fdpipe[1] = -1;
|
fdpipe[1] = -1;
|
||||||
tmpin = dup(0);
|
tmpin = dup(0);
|
||||||
@ -120,7 +121,7 @@ void execute(t_cmd *cmd, char **env)
|
|||||||
{
|
{
|
||||||
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||||
if (fdin < 0)
|
if (fdin < 0)
|
||||||
printf("Minishell: open error\n");
|
printf("Minishell: open : bad file descriptor\n");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
fdin=dup(tmpin);
|
fdin=dup(tmpin);
|
||||||
@ -132,7 +133,7 @@ void execute(t_cmd *cmd, char **env)
|
|||||||
{
|
{
|
||||||
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||||
if (fdin < 0)
|
if (fdin < 0)
|
||||||
printf("Minishell: open error\n");
|
printf("Minishell: open : bad file descriptor\n");
|
||||||
}
|
}
|
||||||
if (i == cmd->nb_s_cmd - 1)
|
if (i == cmd->nb_s_cmd - 1)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/16 02:41:51 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:43:03 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ void free_cmd(t_cmd *cmd)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (cmd->s_cmds[i])
|
while (cmd->s_cmds && cmd->s_cmds[i])
|
||||||
{
|
{
|
||||||
free_double(cmd->s_cmds[i]->args);
|
free_double(cmd->s_cmds[i]->args);
|
||||||
if (cmd->s_cmds[i]->cmd)
|
if (cmd->s_cmds[i]->cmd)
|
||||||
|
|||||||
@ -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/04/16 02:50:09 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:33:32 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -104,34 +104,29 @@ t_cmd *set_cmd(char *input, char **env, int nb)
|
|||||||
t_cmd *cmd;
|
t_cmd *cmd;
|
||||||
char **cmds;
|
char **cmds;
|
||||||
|
|
||||||
if (!is_quote_good(input))
|
if (!is_quote_good(input) || !is_pipe_good(input))
|
||||||
return (0);
|
|
||||||
if (!is_pipe_good(input))
|
|
||||||
return (0);
|
return (0);
|
||||||
cmds = ft_split_with_quote(input, '|');
|
cmds = ft_split_with_quote(input, '|');
|
||||||
//print_double_fd(cmds, 1);
|
|
||||||
if (!cmds)
|
if (!cmds)
|
||||||
return (0);
|
return (0);
|
||||||
cmd = malloc(sizeof(t_cmd));
|
cmd = malloc(sizeof(t_cmd));
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
return (0);
|
return (0);
|
||||||
//cmd->err_var = 0;
|
|
||||||
cmd->s_cmds = calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
cmd->s_cmds = calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
||||||
if (!cmd->s_cmds)
|
if (!cmd->s_cmds)
|
||||||
|
//free(cmd);
|
||||||
return (0);
|
return (0);
|
||||||
cmd->err_var = nb;
|
cmd->err_var = nb;
|
||||||
cmd->path = 0;
|
|
||||||
//cmd->s_cmds[double_size(cmds)] = NULL;
|
|
||||||
cmd->path = get_path(env);
|
cmd->path = get_path(env);
|
||||||
cmd->env = env;
|
cmd->env = ft_dup_double(env);
|
||||||
//set_quote_and_var;
|
|
||||||
//cmd->err_var = 0;
|
|
||||||
cmd->nb_s_cmd = double_size(cmds);
|
cmd->nb_s_cmd = double_size(cmds);
|
||||||
|
//printf("call split cmd\n");
|
||||||
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
||||||
|
//printf("end split cdm\n");
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
|
//free_cmd(cmd);
|
||||||
return (0);
|
return (0);
|
||||||
parse_quote(cmd);
|
parse_quote(cmd);
|
||||||
//printf("after parse quote -%s-\n", cmd->s_cmds[0]->cmd);
|
|
||||||
free(cmds);
|
free(cmds);
|
||||||
if (cmd)
|
if (cmd)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/15 02:41:29 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:43:06 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||||
/* Updated: 2022/04/15 00:13:27 by apommier ### ########.fr */
|
/* Updated: 2022/04/16 04:33:24 by apommier ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -76,6 +76,22 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
|
|||||||
i++;
|
i++;
|
||||||
cmd->infile = get_word(line, word_index);
|
cmd->infile = get_word(line, word_index);
|
||||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||||
|
i = open(cmd->infile, O_RDONLY);
|
||||||
|
if (i == -1)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: ", 2);
|
||||||
|
ft_putstr_fd(cmd->infile, 2);
|
||||||
|
ft_putstr_fd(": no such file\n", 2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
if (access(cmd->infile, R_OK))
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Minishell: ", 2);
|
||||||
|
ft_putstr_fd(cmd->infile, 2);
|
||||||
|
ft_putstr_fd(": Permission denied\n", 2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
close(i);
|
||||||
line = cut_str(line, index, i);
|
line = cut_str(line, index, i);
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
@ -96,7 +112,7 @@ char *set_output(char *line, t_s_cmd *cmd, int index)
|
|||||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||||
i++;
|
i++;
|
||||||
cmd->outfile = get_word(line, index);
|
cmd->outfile = get_word(line, index);
|
||||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||||
line = cut_str(line, index, i);
|
line = cut_str(line, index, i);
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
@ -117,8 +133,10 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
|
|||||||
else
|
else
|
||||||
cmd->in_type = 0;
|
cmd->in_type = 0;
|
||||||
if (next == '<' || next == '>' || !next)
|
if (next == '<' || next == '>' || !next)
|
||||||
error_redirect("problem in ft_input");
|
return (0);
|
||||||
line = set_input(line, cmd, i);
|
line = set_input(line, cmd, i);
|
||||||
|
if (!line)
|
||||||
|
return (0);
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -137,21 +155,28 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
|
|||||||
else
|
else
|
||||||
cmd->in_type = 0;
|
cmd->in_type = 0;
|
||||||
if (next == '<' || next == '>' || !next)
|
if (next == '<' || next == '>' || !next)
|
||||||
error_redirect("problem in ft_output");
|
return (0);
|
||||||
line = set_output(line, cmd, i);
|
line = set_output(line, cmd, i);
|
||||||
return (line);
|
return (line);
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_file(char *file)
|
int set_file(char *file)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
printf("setfile= %s\n", file);
|
//printf("setfile= %s\n", file);
|
||||||
fd = open(file, O_TRUNC | O_CREAT, 0644);
|
fd = open(file, O_TRUNC | O_CREAT, 0644);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
error_redirect("can't set file");
|
{
|
||||||
|
ft_putstr_fd("Minishell: ", 2);
|
||||||
|
ft_putstr_fd(file, 2);
|
||||||
|
ft_putstr_fd(": Permission denied\n", 2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
//error_redirect("can't set file");
|
||||||
if (fd)
|
if (fd)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **add_line(char **tab, char *line)
|
char **add_line(char **tab, char *line)
|
||||||
@ -285,11 +310,14 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
|||||||
// printf("enter redirection\n");
|
// printf("enter redirection\n");
|
||||||
while (line[i])
|
while (line[i])
|
||||||
{
|
{
|
||||||
|
//printf("line[i] i= %d\n", i);
|
||||||
if(line[i] == '<')
|
if(line[i] == '<')
|
||||||
{
|
{
|
||||||
if (!is_in_quote(line, i))
|
if (!is_in_quote(line, i))
|
||||||
{
|
{
|
||||||
line = ft_input(line, cmd, i);
|
line = ft_input(line, cmd, i);
|
||||||
|
if (!line)
|
||||||
|
return (0);
|
||||||
if (cmd->in_type == 1)
|
if (cmd->in_type == 1)
|
||||||
{
|
{
|
||||||
if (!wait_prompt(cmd, index))
|
if (!wait_prompt(cmd, index))
|
||||||
@ -304,11 +332,14 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
|||||||
{
|
{
|
||||||
line = ft_output(line, cmd, i);
|
line = ft_output(line, cmd, i);
|
||||||
//if (cmd->in_type == 0)
|
//if (cmd->in_type == 0)
|
||||||
set_file(cmd->outfile);
|
if (!set_file(cmd->outfile))
|
||||||
|
return (0);
|
||||||
i = 0;
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
if ((line[i] == '<' || line[i] == '>') && is_in_quote(line, i))
|
||||||
|
i++;
|
||||||
|
else if (line[i] != '<' && line[i] != '>')
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return(line);
|
return(line);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user