summaryrefslogtreecommitdiffstats
path: root/src/b_export_next.c
blob: 0a569683ad9adb9302a93cbe0e24a4a99dd17248 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_export_next.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 "b_export_next.h"
#include "f_fail.h"
#include "s_lvars.h"
#include "s_struct.h"
#include "u_vars.h"

static char
	**b_get_var(const char arg[],
				t_msh *msh)
{
	size_t	len;
	char	**var;

	if ((var = (char**)malloc(3 * sizeof(char*))) == NULL)
		f_fail_alloc_and_destroy(msh);
	len = 0;
	while (arg[len] != '=' && arg[len] != '\0')
		len++;
	len += 1;
	if ((var[FT_VAR_NAME] = (char*)malloc((len + 1) * sizeof(char))) == NULL)
		f_fail_alloc_and_destroy(msh);
	var[FT_VAR_NAME][0] = '$';
	ft_strlcpy(var[FT_VAR_NAME] + 1, arg, len);
	if ((var[FT_VAR_VAL] = ft_strdup(arg + len)) == NULL)
		f_fail_alloc_and_destroy(msh);
	var[FT_VAR_NULL] = NULL;
	return (var);
}

static int64_t
	b_is_it_in_env(const char varname[],
				t_msh *msh)
{
	char	**p_env;

	p_env = msh->envp;
	while (*p_env != NULL)
	{
		if (ft_strncmp(varname, *p_env, ft_strclen(*p_env, '=')) == 0)
		{
			return (p_env - msh->envp);
		}
		p_env++;
	}
	return (-1);
}

static void
	b_add_to_env(const char arg[],
				t_msh *msh)
{
	size_t	i;
	char	**nenvp;

	i = 0;
	while (msh->envp[i] != NULL)
		i++;
	if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*))))
		f_fail_alloc_and_destroy(msh);
	i = 0;
	while (msh->envp[i] != NULL)
	{
		if (!(nenvp[i] = ft_strdup(msh->envp[i])))
			f_fail_alloc_and_destroy(msh);
		i++;
	}
	if (!(nenvp[i] = ft_strdup(arg)))
		f_fail_alloc_and_destroy(msh);
	nenvp[i + 1] = 0;
	ft_delwords(msh->envp);
	msh->envp = nenvp;
}

void
	b_export_with_equals(const char arg[],
						t_msh *msh)
{
	char	*varval;
	char	**var;
	int64_t	env_i;

	var = b_get_var(arg, msh);
	if ((env_i = b_is_it_in_env(var[FT_VAR_NAME] + 1, msh)) != -1)
	{
		ft_memdel((void*)&msh->envp[env_i]);
		if ((msh->envp[env_i] = ft_strdup(arg)) == NULL)
			f_fail_alloc_and_destroy(msh);
	}
	else if ((varval = u_get_cstm_vr(var[FT_VAR_NAME], msh)) != NULL)
	{
		b_add_to_env(arg, msh);
		lvars_delone(&msh->vars, var[FT_VAR_NAME] + 1);
	}
	else
	{
		b_add_to_env(arg, msh);
	}
	ft_delwords(var);
}