diff --git a/Makefile b/Makefile index ac9d00c..70ce727 100644 --- a/Makefile +++ b/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 diff --git a/d b/d new file mode 100644 index 0000000..4bcfe98 --- /dev/null +++ b/d @@ -0,0 +1 @@ +d diff --git a/e b/e new file mode 100644 index 0000000..e69de29 diff --git a/filetester.c b/file.c similarity index 63% rename from filetester.c rename to file.c index 0e72bf4..7d3f287 100644 --- a/filetester.c +++ b/file.c @@ -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)); +} diff --git a/file.o b/file.o new file mode 100644 index 0000000..7b79ab5 Binary files /dev/null and b/file.o differ diff --git a/g b/g new file mode 100644 index 0000000..e69de29 diff --git a/infile.c b/infile.c new file mode 100644 index 0000000..a5cf9a7 --- /dev/null +++ b/infile.c @@ -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); +} diff --git a/infile.o b/infile.o new file mode 100644 index 0000000..cc20d3e Binary files /dev/null and b/infile.o differ diff --git a/libftx/libftx.a b/libftx/libftx.a index 707690d..2346697 100644 Binary files a/libftx/libftx.a and b/libftx/libftx.a differ diff --git a/libftx/printf/ft_eprintf.c b/libftx/printf/ft_eprintf.c index 985cdee..857a9ed 100644 --- a/libftx/printf/ft_eprintf.c +++ b/libftx/printf/ft_eprintf.c @@ -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; diff --git a/libftx/printf/ft_eprintf.o b/libftx/printf/ft_eprintf.o new file mode 100644 index 0000000..2b649ef Binary files /dev/null and b/libftx/printf/ft_eprintf.o differ diff --git a/libftx/printf/ft_printf.a b/libftx/printf/ft_printf.a index 0ad5475..1b8bba7 100644 Binary files a/libftx/printf/ft_printf.a and b/libftx/printf/ft_printf.a differ diff --git a/main.c b/main.c index 8cd6276..b6ac71c 100644 --- a/main.c +++ b/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]); diff --git a/main.o b/main.o index 3f8ef4e..37789ca 100644 Binary files a/main.o and b/main.o differ diff --git a/minishell b/minishell index 4395a1c..f1d744f 100755 Binary files a/minishell and b/minishell differ diff --git a/minishell.h b/minishell.h index 237180e..19129ad 100644 --- a/minishell.h +++ b/minishell.h @@ -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 { diff --git a/outfile.c b/outfile.c new file mode 100644 index 0000000..8939ef3 --- /dev/null +++ b/outfile.c @@ -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); +} diff --git a/outfile.o b/outfile.o new file mode 100644 index 0000000..13f900d Binary files /dev/null and b/outfile.o differ diff --git a/t b/t new file mode 100644 index 0000000..e69de29 diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o index 8e8314f..fa8b630 100644 Binary files a/utils/ft_getstr.o and b/utils/ft_getstr.o differ diff --git a/utils/ft_is_in_quote.o b/utils/ft_is_in_quote.o index e732a62..1177bf1 100644 Binary files a/utils/ft_is_in_quote.o and b/utils/ft_is_in_quote.o differ diff --git a/utils/ft_strnchr.o b/utils/ft_strnchr.o index d90543e..c1cc6b1 100644 Binary files a/utils/ft_strnchr.o and b/utils/ft_strnchr.o differ diff --git a/utils/ft_strncpy.o b/utils/ft_strncpy.o index ba1c26e..f84950c 100644 Binary files a/utils/ft_strncpy.o and b/utils/ft_strncpy.o differ diff --git a/utils/ft_strreplace.o b/utils/ft_strreplace.o index d5ff2c5..afdaa9c 100644 Binary files a/utils/ft_strreplace.o and b/utils/ft_strreplace.o differ