42_minishell/env/env2.c

78 lines
1.7 KiB
C
Raw Normal View History

/* ************************************************************************** */
2023-02-17 11:46:12 -05:00
/* */
/* ::: :::::::: */
/* 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 */
2023-02-17 11:46:12 -05:00
/* */
/* ************************************************************************** */
#include "env_private.h"
2023-02-17 11:46:12 -05:00
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;
2023-02-21 08:34:49 -05:00
if (ptr == NULL)
return ;
2023-02-17 11:46:12 -05:00
content = ptr;
2023-02-21 08:34:49 -05:00
if (content->key != NULL)
free(content->key);
if (content->key != NULL)
free(content->value);
if (content != NULL)
free(content);
2023-02-17 11:46:12 -05:00
}
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);
}