42_minishell/env2.c
2023-03-09 20:13:38 +01:00

79 lines
1.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */
/* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftx/libftx.h"
#include "minishell.h"
int get_index(char *s, char c)
{
int i;
i = -1;
while (s[++i])
if (s[i] == c)
return (i);
return (-1);
}
void swap_env_3(void **a, void **b, void **c)
{
void *d;
d = *a;
*a = *b;
*b = *c;
*c = d;
}
void swap_env(void **a, void **b)
{
void *c;
c = *a;
*a = *b;
*b = c;
}
void env_del(void *ptr)
{
t_env *content;
if (ptr == NULL)
return ;
content = ptr;
if (content->key != NULL)
free(content->key);
if (content->key != NULL)
free(content->value);
if (content != NULL)
free(content);
}
char **env_to_strs(t_list **head)
{
t_list *current;
t_env *content;
char **env;
int i;
current = *head;
env = ft_calloc(ft_lstsize(*head), sizeof(char *));
i = 0;
while (current->content)
{
content = current->content;
env[i++] = ft_strmerger(3, content->key, "=", content->value);
current = current->next;
}
return (env);
}