in file & out file work prety well

This commit is contained in:
Camille Chauvet 2023-02-02 17:27:26 +01:00
parent 33cccda0ad
commit 088a3be5eb
24 changed files with 196 additions and 41 deletions

View File

@ -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 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 ${UTILS_SRC} SRCS = main.c file.c infile.c outfile.c ${UTILS_SRC}
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
@ -7,7 +7,7 @@ NAME = minishell
CC = clang CC = clang
CFLAGS = -Wall -Werror -Wextra CFLAGS = -Wall -Werror -Wextra -g
LIBS = libftx/libftx.a LIBS = libftx/libftx.a

1
d Normal file
View File

@ -0,0 +1 @@
d

0
e Normal file
View File

View File

@ -1,8 +1,9 @@
#include "minishell" #include "minishell.h"
int ft_file_is_readable(char *path) int ft_file_is_readable(char *path)
{ {
int readable; int readable;
int fd;
fd = open(path, O_RDONLY); fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)
@ -23,11 +24,12 @@ int ft_file_is_readable(char *path)
int ft_file_is_writeable(char *path) int ft_file_is_writeable(char *path)
{ {
int writeable; int writeable;
int fd;
fd = open(path, O_WRONLY | O_CREAT, 0644); fd = open(path, O_WRONLY | O_CREAT, 0644);
if (fd == -1) if (fd == -1)
{ {
ft_eprintf("bizarre l'erreur"); ft_eprintf("minishell: %s: Permission denied\n", path);
return (0); return (0);
} }
writeable = write(fd, "", 0); writeable = write(fd, "", 0);
@ -39,3 +41,19 @@ int ft_file_is_writeable(char *path)
close(fd); close(fd);
return (1); 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));
}

BIN
file.o Normal file

Binary file not shown.

0
g Normal file
View File

75
infile.c Normal file
View 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
infile.o Normal file

Binary file not shown.

Binary file not shown.

View File

@ -12,7 +12,7 @@
#include "ft_printf.h" #include "ft_printf.h"
int ft_printf(const char *format, ...) int ft_eprintf(const char *format, ...)
{ {
va_list va; va_list va;
int i; int i;

BIN
libftx/printf/ft_eprintf.o Normal file

Binary file not shown.

Binary file not shown.

40
main.c
View File

@ -1,32 +1,5 @@
#include "minishell.h" #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) t_list **ft_parse_cmd(char *line)
{ {
char infile; char infile;
@ -36,19 +9,16 @@ t_list **ft_parse_cmd(char *line)
(void) outfile; (void) outfile;
(void) cmds; (void) cmds;
(void) infile;
i = 0; i = 0;
while (line[i] != '\0') ft_outfile(line);
{ ft_infile(line);
ft_printf("%s\n", line);
i++; return (NULL);
}
} }
int main(int ac, char **av) int main(int ac, char **av)
{ {
int fd;
int i;
if (ac == 1) if (ac == 1)
return (1); return (1);
ft_parse_cmd(av[1]); ft_parse_cmd(av[1]);

BIN
main.o

Binary file not shown.

BIN
minishell

Binary file not shown.

View File

@ -8,6 +8,9 @@
int ft_file_is_readable(char *path); int ft_file_is_readable(char *path);
int ft_file_is_writeable(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 typedef struct cmd
{ {

88
outfile.c Normal file
View 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);
}

BIN
outfile.o Normal file

Binary file not shown.

0
t Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.