aboutsummaryrefslogtreecommitdiff
path: root/src/create_files.c
diff options
context:
space:
mode:
authorChristopher Uhlig2025-01-22 02:40:27 +0100
committerChristopher Uhlig2025-01-22 02:40:27 +0100
commitf6e474d27a1398c6d4f2e88c7f2d3797b85217da (patch)
tree7dd7bd5f2a151b39498991b567ae51cff8242782 /src/create_files.c
parent78dc50a2bce3c6e31405437189e2990d8fc720ac (diff)
downloadminishell-f6e474d27a1398c6d4f2e88c7f2d3797b85217da.tar.gz
minishell-f6e474d27a1398c6d4f2e88c7f2d3797b85217da.zip
kinda fix for pipe error and again.vs for you db just chnage workfolder gn
Diffstat (limited to 'src/create_files.c')
-rw-r--r--src/create_files.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/create_files.c b/src/create_files.c
new file mode 100644
index 0000000..8c04d8f
--- /dev/null
+++ b/src/create_files.c
@@ -0,0 +1,65 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* create_files.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */
+/* Updated: 2025/01/21 13:17:47 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include <sys/fcntl.h>
+#include <unistd.h>
+
+static int cant_write(char *filename);
+static void create_file(char *filename, int mode);
+
+int create_files(t_list *files)
+{
+ t_redirection *file;
+
+ while (files)
+ {
+ if (files->content != NULL)
+ {
+ 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 ;
+ }
+ 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)
+{
+ int fd;
+
+ fd = open(filename, O_WRONLY | O_CREAT | mode, 0644);
+ if (fd != -1)
+ close(fd);
+}
+
+void q4fc(t_list **queue, t_redirection *redir)
+{
+ ft_lstadd_back(queue, ft_lstnew(redir));
+}