in file & out file work prety well
This commit is contained in:
		
							
								
								
									
										6
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								Makefile
									
									
									
									
									
								
							@ -1,5 +1,5 @@
 | 
			
		||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c filetester.c
 | 
			
		||||
SRCS = main.c ${UTILS_SRC}
 | 
			
		||||
UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_getstr.c
 | 
			
		||||
SRCS = main.c file.c infile.c outfile.c ${UTILS_SRC}
 | 
			
		||||
 | 
			
		||||
OBJS = ${SRCS:.c=.o}
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,7 @@ NAME = minishell
 | 
			
		||||
 | 
			
		||||
CC = clang
 | 
			
		||||
 | 
			
		||||
CFLAGS = -Wall -Werror -Wextra
 | 
			
		||||
CFLAGS = -Wall -Werror -Wextra -g
 | 
			
		||||
 | 
			
		||||
LIBS = libftx/libftx.a
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,8 +1,9 @@
 | 
			
		||||
#include "minishell"
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_readable(char *path)
 | 
			
		||||
{
 | 
			
		||||
	int	readable;
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = open(path, O_RDONLY);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
@ -23,11 +24,12 @@ int	ft_file_is_readable(char *path)
 | 
			
		||||
int	ft_file_is_writeable(char *path)
 | 
			
		||||
{
 | 
			
		||||
	int	writeable;
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = open(path, O_WRONLY | O_CREAT, 0644);
 | 
			
		||||
	if (fd == -1)
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("bizarre l'erreur");
 | 
			
		||||
		ft_eprintf("minishell: %s: Permission denied\n", path);
 | 
			
		||||
		return (0);
 | 
			
		||||
	}
 | 
			
		||||
	writeable = write(fd, "", 0);
 | 
			
		||||
@ -39,3 +41,19 @@ int	ft_file_is_writeable(char *path)
 | 
			
		||||
	close(fd);
 | 
			
		||||
	return (1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
char	*ft_get_file_path(char *infile)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
 | 
			
		||||
	i = 1;
 | 
			
		||||
	while (infile[i] == ' ')
 | 
			
		||||
		i++;
 | 
			
		||||
	if (infile[i] == '\0')
 | 
			
		||||
	{
 | 
			
		||||
		ft_eprintf("minishell: syntax error near ");
 | 
			
		||||
		ft_eprintf("unexpected token `newline'\n");
 | 
			
		||||
		return (NULL);
 | 
			
		||||
	}
 | 
			
		||||
	return (ft_getstr(infile, i + 1));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										75
									
								
								infile.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								infile.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,75 @@
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_get_infile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	path = NULL;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (line[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			if (path != NULL)
 | 
			
		||||
				free(path);
 | 
			
		||||
			path = ft_get_file_path(line + i);
 | 
			
		||||
			if (path == NULL || ft_file_is_readable(path) == 0)
 | 
			
		||||
			{
 | 
			
		||||
				free(path);
 | 
			
		||||
				return (-1);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	if (path == NULL)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	i = open(path, O_RDONLY);
 | 
			
		||||
	free(path);
 | 
			
		||||
	return (i);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_remove_infile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int	separator;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (line[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if (line[i] == '<' && ft_is_in_quote(line, i) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			line[i++] = ' ';
 | 
			
		||||
			while (line[i] == ' ')
 | 
			
		||||
				i++;
 | 
			
		||||
			separator = ft_is_in_quote(line, i);
 | 
			
		||||
			if (separator == 0)
 | 
			
		||||
				separator = ' ';
 | 
			
		||||
			else if (separator == 1)
 | 
			
		||||
				separator = '\'';
 | 
			
		||||
			else
 | 
			
		||||
				separator = '\"';
 | 
			
		||||
			while (line[i] != separator && line[i] != '\0')
 | 
			
		||||
				line[i++] = ' ';
 | 
			
		||||
			if (line[i] != '\0' 
 | 
			
		||||
				&& (separator == '\'' || separator == '\"'))
 | 
			
		||||
				line[i++] = ' ';
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_infile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = ft_get_infile(line);
 | 
			
		||||
	if (ft_remove_infile(line))
 | 
			
		||||
	{
 | 
			
		||||
		if (fd > 0)
 | 
			
		||||
			close(fd);
 | 
			
		||||
		return (-1);
 | 
			
		||||
	}
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								libftx/libftx.a
									
									
									
									
									
								
							
							
						
						
									
										
											BIN
										
									
								
								libftx/libftx.a
									
									
									
									
									
								
							
										
											Binary file not shown.
										
									
								
							@ -12,7 +12,7 @@
 | 
			
		||||
 | 
			
		||||
#include "ft_printf.h"
 | 
			
		||||
 | 
			
		||||
int	ft_printf(const char *format, ...)
 | 
			
		||||
int	ft_eprintf(const char *format, ...)
 | 
			
		||||
{
 | 
			
		||||
	va_list	va;
 | 
			
		||||
	int		i;
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								libftx/printf/ft_eprintf.o
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								libftx/printf/ft_eprintf.o
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
							
								
								
									
										40
									
								
								main.c
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								main.c
									
									
									
									
									
								
							@ -1,32 +1,5 @@
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
int	ft_get_infile(char **line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	path = NULL;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while ((*line)[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if ((*line)[i] == '<' && ft_is_in_quote(*line, i) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			if (path != NULL)
 | 
			
		||||
				free(path);
 | 
			
		||||
			path = ft_getstr(*line, i);
 | 
			
		||||
			if (path == NULL)
 | 
			
		||||
				return (-1);
 | 
			
		||||
			if (ft_file_is_readable(path) == 0)
 | 
			
		||||
			{
 | 
			
		||||
				free(path);
 | 
			
		||||
				return (-1);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (open(path, O_RDONLY));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
t_list	**ft_parse_cmd(char *line)
 | 
			
		||||
{
 | 
			
		||||
	char	infile;
 | 
			
		||||
@ -36,19 +9,16 @@ t_list	**ft_parse_cmd(char *line)
 | 
			
		||||
 | 
			
		||||
	(void) outfile;
 | 
			
		||||
	(void) cmds;
 | 
			
		||||
	(void) infile;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (line[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	ft_outfile(line);
 | 
			
		||||
	ft_infile(line);
 | 
			
		||||
	ft_printf("%s\n", line);
 | 
			
		||||
	return (NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	main(int ac, char **av)
 | 
			
		||||
{
 | 
			
		||||
	int	fd;
 | 
			
		||||
	int	i;
 | 
			
		||||
 | 
			
		||||
	if (ac == 1)
 | 
			
		||||
		return (1);
 | 
			
		||||
	ft_parse_cmd(av[1]);
 | 
			
		||||
 | 
			
		||||
@ -8,6 +8,9 @@
 | 
			
		||||
 | 
			
		||||
int	ft_file_is_readable(char *path);
 | 
			
		||||
int	ft_file_is_writeable(char *path);
 | 
			
		||||
char	*ft_get_file_path(char *infile);
 | 
			
		||||
int	ft_infile(char *line);
 | 
			
		||||
int	ft_outfile(char *line);
 | 
			
		||||
 | 
			
		||||
typedef struct cmd
 | 
			
		||||
{
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										88
									
								
								outfile.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								outfile.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,88 @@
 | 
			
		||||
#include "minishell.h"
 | 
			
		||||
 | 
			
		||||
static int	ft_get_outfile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int		append;
 | 
			
		||||
	char	*path;
 | 
			
		||||
 | 
			
		||||
	path = NULL;
 | 
			
		||||
	append = 0;
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (line[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			if (line[i + 1] == '>')
 | 
			
		||||
			{
 | 
			
		||||
				i++;
 | 
			
		||||
				append = 1;
 | 
			
		||||
			}
 | 
			
		||||
			else
 | 
			
		||||
				append = 0;
 | 
			
		||||
			if (path != NULL)
 | 
			
		||||
				free(path);
 | 
			
		||||
			path = ft_get_file_path(line + i);
 | 
			
		||||
			if (path == NULL || ft_file_is_writeable(path) == 0)
 | 
			
		||||
			{
 | 
			
		||||
				free(path);
 | 
			
		||||
				return (-1);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	if (path == NULL)
 | 
			
		||||
		return (-2);
 | 
			
		||||
	if (append == 1)
 | 
			
		||||
		i = open(path, O_APPEND | O_CREAT, 0664);
 | 
			
		||||
	else
 | 
			
		||||
		i = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0664);
 | 
			
		||||
	free(path);
 | 
			
		||||
	return (i);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int	ft_remove_outfile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	size_t	i;
 | 
			
		||||
	int		separator;
 | 
			
		||||
 | 
			
		||||
	i = 0;
 | 
			
		||||
	while (line[i] != '\0')
 | 
			
		||||
	{
 | 
			
		||||
		if (line[i] == '>' && ft_is_in_quote(line, i) == 0)
 | 
			
		||||
		{
 | 
			
		||||
			while (line[i] == '>')
 | 
			
		||||
				line[i++] = ' ';
 | 
			
		||||
			while (line[i] == ' ')
 | 
			
		||||
				i++;
 | 
			
		||||
			separator = ft_is_in_quote(line, i);
 | 
			
		||||
			if (separator == 0)
 | 
			
		||||
				separator = ' ';
 | 
			
		||||
			else if (separator == 1)
 | 
			
		||||
				separator = '\'';
 | 
			
		||||
			else
 | 
			
		||||
				separator = '\"';
 | 
			
		||||
			while (line[i] != separator && line[i] != '\0')
 | 
			
		||||
				line[i++] = ' ';
 | 
			
		||||
			if (line[i] != '\0'
 | 
			
		||||
				&& (separator == '\'' || separator == '\"'))
 | 
			
		||||
				line[i++] = ' ';
 | 
			
		||||
		}
 | 
			
		||||
		i++;
 | 
			
		||||
	}
 | 
			
		||||
	return (0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int	ft_outfile(char *line)
 | 
			
		||||
{
 | 
			
		||||
	int	fd;
 | 
			
		||||
 | 
			
		||||
	fd = ft_get_outfile(line);
 | 
			
		||||
	if (ft_remove_outfile(line))
 | 
			
		||||
	{
 | 
			
		||||
		if (fd > 0)
 | 
			
		||||
			close(fd);
 | 
			
		||||
		return (-1);
 | 
			
		||||
	}
 | 
			
		||||
	return (fd);
 | 
			
		||||
}
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
		Reference in New Issue
	
	Block a user