42_minishell/env.c

156 lines
3.3 KiB
C
Raw Normal View History

2023-02-14 01:21:24 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
2023-02-14 11:11:39 -05:00
/* Updated: 2023/02/14 15:21:28 by cchauvet ### ########.fr */
2023-02-14 01:21:24 -05:00
/* */
/* ************************************************************************** */
2023-02-14 11:11:39 -05:00
#include "libftx/libftx.h"
2023-02-14 01:21:24 -05:00
#include "minishell.h"
2023-02-14 11:11:39 -05:00
#include "utils/utils.h"
2023-02-14 01:21:24 -05:00
2023-02-14 11:11:39 -05:00
void env_del(void *content)
2023-02-14 01:21:24 -05:00
{
2023-02-14 11:11:39 -05:00
(void) content;
2023-02-14 01:21:24 -05:00
}
int strcmp_alphabet(char *s1, char *s2)
{
int i;
if (!s1 || !s2)
return (-2);
i = 0;
while (s1[i] && s2[i])
{
if (s1[i] < s2[i])
return (0);
else if (s1[i] > s2[i])
return (1);
i++;
}
return (-1);
}
void ft_double_swap(char **a, char **b)
{
void *c;
c = *a;
*a = *b;
*b = c;
}
void exchange(char **a, char **b, char **c)
{
void *d;
d = *a;
*a = *b;
*b = *c;
*c = d;
}
void add_sort(t_list **head, char *str)
{
t_list *current;
char *last;
current = *head;
while (current->next != NULL && strcmp_alphabet(str, current->content) != 0)
current = current->next;
if (strcmp_alphabet(str, current->content) == 1)
last = str;
else
exchange(&last, (char **)(&current->content), &str);
while (current != NULL)
{
if (current->next == NULL)
current->next = ft_calloc(1, sizeof(t_list));
if (current->next == NULL)
return ;
current = current->next;
if (current->content == NULL)
{
current->content = last;
return ;
}
else
ft_double_swap((char **)(&current->content), &last);
}
}
2023-02-14 01:33:02 -05:00
char *get_value_by_index(int index, t_list **head)
2023-02-14 01:21:24 -05:00
{
t_list *current;
int i;
current = *head;
i = -1;
while (current != NULL && ++i != index)
current = current->next;
if (i == index)
return (ft_strchr(current->content, '=') + 1);
return (NULL);
}
2023-02-14 11:11:39 -05:00
char *get_value_by_key(char *key, t_list **head)
2023-02-14 01:21:24 -05:00
{
2023-02-14 11:11:39 -05:00
t_list *current;
2023-02-14 01:21:24 -05:00
2023-02-14 11:11:39 -05:00
current = *head;
while (current != NULL)
2023-02-14 01:21:24 -05:00
{
2023-02-14 11:11:39 -05:00
if (ft_strncmp(current->content, key, ft_strlen(key)))
return (ft_strchr(current->content, '=') + 1);
current = current->next;
2023-02-14 01:21:24 -05:00
}
2023-02-14 11:11:39 -05:00
return (NULL);
2023-02-14 01:21:24 -05:00
}
2023-02-14 11:11:39 -05:00
int set_value_by_key(t_list **env, char *key, char *value)
2023-02-14 01:21:24 -05:00
{
t_list *current;
2023-02-14 11:11:39 -05:00
char *temp;
2023-02-14 01:21:24 -05:00
2023-02-14 11:11:39 -05:00
current = *env;
2023-02-14 01:21:24 -05:00
while (current != NULL)
{
2023-02-14 11:11:39 -05:00
if (!ft_strncmp(key, current->content, ft_strlen(current->content)))
{
temp = current->content;
current->content = ft_strreplace(temp, value,
ft_strnchr(temp, '=') + 1,
ft_strlen(temp) - (ft_strnchr(temp, '=') - 1));
free(temp);
if (current->content == NULL)
{
ft_eprintf("minishell: malloc failed");
return (1);
}
}
2023-02-14 01:21:24 -05:00
current = current->next;
}
2023-02-14 11:11:39 -05:00
return (0);
2023-02-14 01:21:24 -05:00
}
t_list **init_env(char **env)
{
t_list **head;
int i;
head = ft_calloc(1, sizeof(t_list *));
*head = ft_calloc(1, sizeof(t_list));
if (*head == NULL)
return (NULL);
i = -1;
while (env[++i])
add_sort(head, env[i]);
return (head);
}