diff --git a/.nfs00000000098c82b700000161 b/.nfs00000000098c82b700000161 deleted file mode 100755 index eaadc7c..0000000 Binary files a/.nfs00000000098c82b700000161 and /dev/null differ diff --git a/file.c b/file.c index 9b3e201..d0bc35c 100644 --- a/file.c +++ b/file.c @@ -39,37 +39,17 @@ int ft_file_is_writable(const char *path) int writeable; int fd; - fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open(path, O_WRONLY | O_CREAT, 0644); if (fd == -1) { ft_eprintf("minishell: %s: Permission denied\n", path); - return (-1); + return (0); } writeable = write(fd, "", 0); if (writeable == -1) { ft_eprintf("minishell: %s: Permission denied\n", path); - return (-1); - } - return (fd); -} - -int ft_file_is_appendable(const char *path) -{ - int writeable; - int fd; - - fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (fd == -1) - { - ft_eprintf("minishell: %s: Permission denied\n", path); - return (-1); - } - writeable = write(fd, "", 0); - if (writeable == -1) - { - ft_eprintf("minishell: %s: Permission denied\n", path); - return (-1); + return (0); } return (fd); } diff --git a/infile.c b/infile.c index 98af4d2..2f17bc8 100644 --- a/infile.c +++ b/infile.c @@ -40,6 +40,7 @@ static int ft_infile_is_valid(t_data *data, const char *line) ft_quote_remover(path); if (ft_file_is_readable(path) == 0) { + data->exit_code = 1; free(path); ft_freer_tab_ultimate(1, tab); return (0); @@ -130,10 +131,7 @@ int ft_infile(t_data *data, char *line) int fd; if (ft_infile_is_valid(data, line) == 0) - { - data->exit_code = 2; return (-2); - } fd = ft_get_infile(data, line); if (fd == -2) return (-2); diff --git a/outfile.c b/outfile.c index 1f8a510..35938e8 100644 --- a/outfile.c +++ b/outfile.c @@ -1,20 +1,21 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* outfile.c :+: :+: :+: */ +/* infile.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet ') { - if (tab[i + 1] != NULL && !ft_contain_only_str(tab[i + 1], "| <>")) - continue ; - ft_eprintf("minishell: %s: must be followed by an outfile\n", tab[i]); - ft_freer_tab_ultimate(1, tab); - return (0); + path = ft_env_filler(data, tab[i + 1]); + if (path == NULL) + return (0); + ft_quote_remover(path); + if (ft_file_is_writable(path) == 0) + { + data->exit_code = 1; + free(path); + ft_freer_tab_ultimate(1, tab); + return (0); + } + if (ft_contain_only_str(path, "| >>")) + { + free(path); + ft_eprintf("minishell: %s: must be followed by an outfile\n", path); + ft_freer_tab_ultimate(1, tab); + return (0); + } + free(path); } } ft_freer_tab_ultimate(1, tab); return (1); } -static int ft_get_outfile(const char *line) +static int ft_get_outfile(t_data *data, const char *line) { size_t i; int fd; char **tab; + char *path; tab = ft_split_quoted(line, ' '); if (tab == NULL) @@ -53,17 +69,22 @@ static int ft_get_outfile(const char *line) ft_eprintf("minishell: malloc failed\n"); return (-2); } - fd = 1; + fd = 0; i = 0; while (tab[i + 1] != NULL) { if (tab[i][0] == '>') - if (fd != 1) + if (fd != 0) close(fd); + path = ft_env_filler(data, tab[i + 1]); + if (path == NULL) + return (-2); + ft_quote_remover(path); if (ft_strcmp(">", tab[i]) == 0) - fd = ft_file_is_writable(ft_quote_remover(tab[i + 1])); + fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, 0644); else if (ft_strcmp(">>", tab[i]) == 0) - fd = ft_file_is_appendable(ft_quote_remover(tab[i + 1])); + fd = open(path, O_CREAT | O_APPEND, 0644); + free(path); i++; } ft_freer_tab_ultimate(1, tab); @@ -87,8 +108,11 @@ static int ft_remove_outfile(char *line) while (tab[i] != NULL) { if (tab[i][0] == '>') + { ft_strshift(line + y, (-1) * (ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1 + (line[ft_strlen(tab[i]) + ft_strlen(tab[i + 1]) + 1] != '\0'))); + i++; + } else y = y + ft_strlen(tab[i]) + (y != 0); i++; @@ -101,12 +125,9 @@ int ft_outfile(t_data *data, char *line) { int fd; - if (ft_outfile_is_valid(line) == 0) - { - data->exit_code = 2; + if (ft_outfile_is_valid(data, line) == 0) return (-2); - } - fd = ft_get_outfile(line); + fd = ft_get_outfile(data, line); if (fd == -2) return (-2); ft_remove_outfile(line); diff --git a/t b/t deleted file mode 100644 index e69de29..0000000