aboutsummaryrefslogtreecommitdiff
path: root/src/create_files.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/create_files.c')
-rw-r--r--src/create_files.c58
1 files changed, 37 insertions, 21 deletions
diff --git a/src/create_files.c b/src/create_files.c
index 8689f88..eee2860 100644
--- a/src/create_files.c
+++ b/src/create_files.c
@@ -6,40 +6,56 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
-/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */
+/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
+#include <sys/fcntl.h>
#include <unistd.h>
-void create_files(t_list *files)
+static int cant_write(char *filename);
+static void create_file(char *filename, int mode);
+
+int create_files(t_list *files)
{
t_redirection *file;
- int fd;
while (files)
{
- dbg("Test");
- if (files->content == NULL)
- continue ;
- file = (t_redirection *)files->content;
- if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1)
- break ;
- if (file->type == OUTPUT_OVERRIDE)
- {
- fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
- close(fd);
- }
- else if (file->type == OUTPUT_APPEND)
+ if (files->content != NULL)
{
- fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
- close(fd);
+ file = (t_redirection *)files->content;
+ if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1
+ || access(file->specifier, R_OK) == -1))
+ return (EXIT_FAILURE);
+ if (cant_write(file->specifier))
+ break ;
+ if (file->type == OUTPUT_OVERRIDE)
+ create_file(file->specifier, O_TRUNC);
+ else if (file->type == OUTPUT_APPEND)
+ create_file(file->specifier, O_APPEND);
+ if (files->next == NULL)
+ break ;
+ if (((t_redirection *)files->next->content)->type == 0)
+ break ;
}
- if (files->next == NULL)
- break ;
- if (((t_redirection *) files->next->content)->type == 0)
- break ;
files = files->next;
}
+ return (EXIT_SUCCESS);
+}
+
+static int cant_write(char *filename)
+{
+ return (access(filename, F_OK) != -1 && access(filename, W_OK) == -1);
+}
+
+static void create_file(char *filename, int mode)
+{
+ close(open(filename, O_WRONLY | O_CREAT | mode, 0644));
+}
+
+void q4fc(t_list **queue, t_redirection *redir)
+{
+ ft_lstadd_back(queue, ft_lstnew(redir));
}