summaryrefslogtreecommitdiffstats
path: root/src/b_unset.c
blob: f5ab3560b4a3f8c8f0be940b71ba0af65277ab26 (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
115
116
117
118
119
120
121
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   b_unset.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 "f_fail.h"
#include "s_lvars.h"
#include "s_struct.h"
#include "u_utils.h"

static t_bool	check_valid_identifier(const char arg[])
{
	char	*ptr;
	t_bool	r;

	ptr = (char*)arg;
	r = TRUE;
	if (ft_isalpha(ptr[0]) == FALSE)
	{
		r = FALSE;
	}
	if (ptr[0] == '_')
	{
		r = TRUE;
	}
	while (*ptr != '\0')
	{
		if (*ptr == '=')
		{
			r = FALSE;
		}
		ptr++;
	}
	return (r);
}

static void		b_realloc_env(size_t skip, t_msh *msh)
{
	char	**nenvp;
	size_t	i;

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

static t_bool	b_removed_from_env(const char arg[], t_msh *msh)
{
	char	**env_dup;
	size_t	i;

	env_dup = u_get_env_var_names(msh);
	i = 0;
	while (env_dup[i] != NULL)
	{
		if (ft_strncmp(arg, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0)
		{
			b_realloc_env(i, msh);
			ft_delwords(env_dup);
			return (TRUE);
		}
		i++;
	}
	ft_delwords(env_dup);
	return (FALSE);
}

unsigned char	b_unset(char *args[], t_msh *msh)
{
	char	**ptr;
	char	r;

	if (args[0] == NULL)
		return (0);
	r = 0;
	ptr = args;
	while (*ptr != NULL)
	{
		if (check_valid_identifier(*ptr) == FALSE)
		{
			f_fail_identifier("unset", *ptr);
			r = 1;
			ptr++;
			continue ;
		}
		if (b_removed_from_env(*ptr, msh) == FALSE)
		{
			lvars_delone(&msh->vars, *ptr);
		}
		ptr++;
	}
	return (r);
}