aboutsummaryrefslogtreecommitdiff
path: root/src/create_files.c
blob: 8c04d8f158ce07979f4c2026ed77a9d26c4552d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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));
}