42_minishell/env.c

100 lines
1.5 KiB
C
Raw Normal View History

2023-02-01 11:08:02 -05:00
#include "env.h"
#include "libftx/libftx.h"
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 add_sort(t_list **head, char *str)
{
t_list *current;
char *last;
current = *head;
while (current->next != NULL)
{
if (strcmp_alphabet(str, current->content) == 0)
break ;
current = current->next;
}
last = current->content;
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], "XDG_SESSION_CLASS=user", 200))
write(1, "", 0);
add_sort(head, env[i]);
}
//current->next = NULL;
return(head);
}
int main(int argc, char *argv[], char **env)
{
t_list **new_env;
t_list *current;
(void)argc;
(void)argv;
new_env = init_env(env);
current = *new_env;
while (current != NULL)
{
ft_printf("%s\n", current->content);
current = current->next;
}
return (0);
}