summaryrefslogtreecommitdiffstats
path: root/src/u_vars_next.c
blob: 0b485338a7293bf2562589c6962c96ca7521f672 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   u_vars_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>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "d_enum.h"
#include "s_lvars.h"
#include "s_struct.h"
#include "u_parse.h"
#include "u_utils.h"

static long		u_get_env_var_line(const char varname[], t_msh *msh)
{
	char	**env_dup;
	long	i;

	env_dup = u_get_env_var_names(msh);
	i = 0;
	while (env_dup[i] != NULL
		&& ft_strncmp(varname, env_dup[i], ft_strlen(env_dup[i]) + 1) != 0)
	{
		i++;
	}
	if (env_dup[i] == NULL)
	{
		i = -1;
	}
	ft_delwords(env_dup);
	return (i);
}

static t_bool	u_get_custom_var_existance(const char varname[], t_msh *msh)
{
	t_lvars	*ptr;

	ptr = msh->vars;
	while (ptr != NULL
		&& ft_strncmp(varname, ptr->name, ft_strlen(varname) + 1) != 0)
	{
		ptr = ptr->next;
	}
	if (ptr != NULL)
	{
		return (TRUE);
	}
	else
	{
		return (FALSE);
	}
}

/*
** SYNOPSIS
** char
** u_subst_var_value(const char varname[], const char newval[], t_msh *msh);
**
** DESCRIPTION
** The u_subst_var_value() function changes the value of msh->envp
** variable varname[] with newval[]. If varname[] wasn't found
** in msh->envp, varname[] is searched in msh->vars. Otherwise
** varname[] is added to msh->vars.
**
** RETURN VALUES
** The u_subst_var_value() function returns 0 upon success
** and 1 in case of malloc(3) failure. When returning 1, the
** entire environment is destroyed.
*/

char			u_subst_var_value(const char varname[],
								const char newval[],
								t_msh *msh)
{
	size_t	i;
	long	en_l;
	char	new_line_fmt[ARG_MAX];

	if ((en_l = u_get_env_var_line(varname + 1, msh)) > -1)
	{
		ft_memdel((void*)&msh->envp[en_l]);
		ft_sprintf(new_line_fmt, "%s=%s", varname + 1, newval);
		if ((msh->envp[en_l] = (char*)malloc((ft_strlen(new_line_fmt) + 1) *
			sizeof(char))) == NULL)
		{
			i = en_l;
			while (msh->envp[++i] != NULL)
				ft_memdel((void*)&msh->envp[i]);
			ft_delwords(msh->envp);
			msh->envp = NULL;
			return (1);
		}
		ft_strlcpy(msh->envp[en_l], new_line_fmt, ft_strlen(new_line_fmt) + 1);
	}
	else if ((en_l = u_get_custom_var_existance(varname + 1, msh)) == TRUE)
		lvars_rebind(&msh->vars, varname + 1, newval);
	else
		lvars_add_front(&msh->vars, lvars_new(varname + 1, newval));
	return (0);
}