summaryrefslogtreecommitdiffstats
path: root/src/s_lredir.c
blob: 0457434843d9e490138512c53367a65348322cf1 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_lredir.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "s_struct.h"

static t_lredir	*s_lredir_last(struct s_lredir *lredir)
{
	while (lredir->next != NULL)
	{
		lredir = lredir->next;
	}
	return (lredir);
}

void			s_lredir_add_back(t_lredir **lredir, t_lredir *new)
{
	struct s_lredir	*tmp;

	if (*lredir == NULL)
	{
		*lredir = new;
	}
	else
	{
		tmp = s_lredir_last(*lredir);
		tmp->next = new;
	}
}

void			s_lredir_clear(struct s_lredir **lredir)
{
	struct s_lredir	*renext;
	struct s_lredir	*tmp;

	if (lredir == NULL)
		return ;
	tmp = *lredir;
	while (tmp != NULL)
	{
		renext = tmp->next;
		if (tmp->heredoc != NULL)
			ft_memdel((void*)&tmp->heredoc);
		ft_memdel((void*)&tmp);
		tmp = renext;
	}
	*lredir = NULL;
}

struct s_lredir	*s_lredir_new(const char path[], int32_t fd, int8_t redir)
{
	struct s_lredir	*rdr;

	if ((rdr = (struct s_lredir*)malloc(sizeof(struct s_lredir))) == NULL)
		return (NULL);
	rdr->fd = fd;
	rdr->right_fd = -1;
	rdr->redir = redir;
	rdr->next = NULL;
	ft_strlcpy(rdr->path, path, PATH_MAX);
	return (rdr);
}