aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
blob: 63a0f8a51e2a3aa2d0bce37e4bee768a56327dd8 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   env.c                                              :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/10/17 14:31:07 by chuhlig           #+#    #+#             */
/*   Updated: 2025/01/20 15:05:49 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "env.h"
#include "minishell.h"
#include <stdlib.h>

void	getenvlst(t_env **env, char **en)
{
	char	*tmp;
	int		i;
	t_env	*current;

	i = 0;
	while (en[i] != NULL)
	{
		tmp = ft_strchr(en[i], '=');
		*tmp = '\0';
		current = *env;
		current = malloc(sizeof(t_env));
		current->name = ft_strdup(en[i]);
		current->value = ft_strdup(tmp + 1);
		current->next = *env;
		*env = current;
		i++;
	}
}

void	free_envlst(t_env **env)
{
	t_env	*cur;
	t_env	*new;

	cur = *env;
	while (cur)
	{
		new = cur->next;
		free(cur->name);
		free(cur->value);
		free(cur);
		cur = new;
	}
}

char	*env_get(t_env *env, char *name)
{
	while (env != NULL)
	{
		if (!ft_strncmp(env->name, name, ft_strlen(env->name)))
			return (env->value);
		env = env->next;
	}
	return (NULL);
}

t_env	*env_new(char *name)
{
	t_env	*result;

	result = malloc(sizeof(t_env));
	if (!result)
		return (NULL);
	result->name = name;
	return (result);
}

void	free_env_node(t_env *node)
{
	free(node->name);
	free(node->value);
	free(node);
}