fix: empty input segfault

This commit is contained in:
Camille Chauvet 2023-02-15 21:31:49 +01:00
parent 99fdd578e9
commit 56eead9241
7 changed files with 57 additions and 23 deletions

Binary file not shown.

Binary file not shown.

4
cmds.c
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:17:26 by cchauvet #+# #+# */
/* Updated: 2023/02/15 17:53:59 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 21:21:42 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -109,7 +109,7 @@ t_list **ft_parse_cmds(char *line)
cmds = malloc(sizeof(t_list *));
outfile = ft_outfile(line);
infile = ft_infile(line);
if (infile == -2)
if (infile == -2 || outfile == -2)
return (NULL);
if (ft_syntatic_verif(line) == 1)
return (NULL);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 17:52:10 by cchauvet #+# #+# */
/* Updated: 2023/02/15 20:47:19 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 21:22:24 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,6 +25,8 @@ static int ft_infile_is_valid(const char *line)
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
i = 0;
while (tab[i + 1] != NULL)
i++;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 18:01:07 by cchauvet #+# #+# */
/* Updated: 2023/02/15 20:48:39 by cchauvet ### ########.fr */
/* Updated: 2023/02/15 21:22:51 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,8 @@ static int ft_outfile_is_valid(const char *line)
ft_eprintf("minishell: malloc failed\n");
return (0);
}
if (tab[0] == NULL)
return (1);
i = 0;
while (tab[i + 1] != NULL)
i++;

View File

@ -1,5 +1,6 @@
#include "libftx/libftx.h"
#include "minishell.h"
#include "utils/utils.h"
static int ft_quote_verif(const char *str)
{
@ -12,47 +13,76 @@ static int ft_quote_verif(const char *str)
return (0);
}
static int ft_multipipe(const char *str)
static int ft_pipe_is_alone(const char *str)
{
size_t i;
int check;
check = 0;
i = 0;
while (str[i] != '\0')
{
if (str[i] != '|')
check = 1;
else
{
if (check == 0)
{
ft_eprintf("minishell: Pipe must be followed and ");
ft_eprintf("preced by a command or redirection\n");
return (1);
}
check = 0;
}
i++;
}
return (0);
}
static int ft_special_char_dub(const char *str)
{
size_t i;
size_t y;
i = 0;
while (str[i] != '\0')
{
if (ft_is_in_quote(str, i))
continue ;
if (ft_is_in("|<>", str[i]))
{
y = 0;
while (str[i + y] == '|' && !ft_is_in_quote(str, i))
while (str[i] == str[i + y])
y++;
if ((y > 2 && (str[i] == '>' || str[i] == '<'))
|| (y > 1 && str[i] == '|'))
{
if (y > 0)
{
ft_eprintf("minishell: Multiple pipes is not supported\n");
ft_eprintf("minishell: to many %c in a row", str, str[i]);
return (1);
}
y++;
i = i + y;
}
i++;
}
return (0);
}
static int ft_pipe_is_alone(const char *str)
int ft_empty_verif(const char *str)
{
size_t i;
i = ft_strlen(str) - 1;
while (str[i] != '|' && i > 0)
{
if (str[i] != ' ')
return (0);
i--;
}
if (i == 0)
return (0);
ft_eprintf("minishell: Pipe must be followed by a command or redirection\n");
return (1);
i = 0;
while (str[i] == ' ')
i++;
if (str[i] == '\0')
ft_eprintf("minishell: %s: command not found \n", str);
return (str[i] == '\0');
}
int ft_syntatic_verif(const char *str)
{
return (ft_quote_verif(str) || ft_multipipe(str) || ft_pipe_is_alone(str));
return (ft_quote_verif(str)
|| ft_pipe_is_alone(str)
|| ft_empty_verif(str)
|| ft_special_char_dub(str));
}

0
t
View File