summaryrefslogtreecommitdiffstats
path: root/src/s_lalias.c
blob: b4b94308d99bb420e9ada1f9f93e382e49a34a2b (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_lalias.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 <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "s_struct.h"

void
	s_lalias_rebind(t_lalias **lalias,
					const char newname[],
					const char newval[])
{
	t_lalias	*tmp;

	tmp = *lalias;
	while (tmp != NULL &&
		ft_strncmp(tmp->name, newname, ft_strlen(newname) + 1) != 0)
	{
		tmp = tmp->next;
	}
	if (tmp == NULL)
	{
		return ;
	}
	ft_memdel((void*)&tmp->val);
	if ((tmp->val = ft_strdup(newval)) == NULL)
	{
		ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno));
	}
}

void
	s_lalias_add_front(t_lalias **lalias, t_lalias *new)
{
	if (lalias == NULL || new == NULL)
	{
		return ;
	}
	new->next = *lalias;
	*lalias = new;
}

void
	s_lalias_clear(t_lalias **lalias)
{
	t_lalias	*tmp;
	t_lalias	*renext;

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

t_lalias
	*s_lalias_new(const char name[], const char val[])	
{
	t_lalias	*link;

	if ((link = (t_lalias*)malloc(sizeof(t_lalias))) == NULL)
	{
		return (NULL);
	}
	if ((link->name = ft_strdup(name)) == NULL)
	{
		return (NULL);
	}
	if ((link->val = ft_strdup(val)) == NULL)
	{
		return (NULL);
	}
	link->next = NULL;
	return (link);
}