42_minishell/env.c
Etienne Rey-bethbeder 13d412ea71 Push sur ta branche
2023-02-02 17:40:09 +01:00

140 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/02 17:39:56 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "env.h"
#include "libftx/libftx.h"
void print_export(t_list **head, int fd)
{
t_list *current;
char *ctn;
current = *head;
while (current != NULL)
{
ctn = current->content;
if (*(ft_strchr(ctn, '=') - 1) != '_')
{
write(fd, "declare -x ", 11);
ft_putstr_fd(ctn, fd);
write(fd, "\n", 1);
}
current = current->next;
}
}
void print_env(t_list **head, int fd)
{
t_list *current;
current = *head;
while (current != NULL)
{
ft_putstr_fd(current->content, fd);
write(fd, "\n", 1);
current = current->next;
}
}
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);
}
}
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])
{
if (ft_strnstr(env[i], "XMODIFIERS=@im=ibus", 200))
write(1, "", 0);
add_sort(head, env[i]);
}
return (head);
}
/*int main(int argc, char *argv[], char **env)
{
(void)argc;
(void)argv;
print_export(init_env(env));
return (0);
}*/