42_minishell/env.c
2023-02-14 07:33:02 +01:00

195 lines
3.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/14 07:32:43 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#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 print_export(t_list **head, int fd)
{
t_list *current;
char *ctn;
int v;
current = *head;
while (current != NULL)
{
ctn = current->content;
if (*(ft_strchr(ctn, '=') - 1) != '_')
{
v = get_index(ctn, '=');
write(fd, "declare -x ", 11);
write(fd, ctn, v + 1);
write(fd, "\"", 1);
ft_putstr_fd(ctn + v + 1, fd);
write(fd, "\"\n", 2);
}
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);
}
}
char *get_value_by_index(int index, t_list **head)
{
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);
}
int is_start(char *big, char *little)
{
int i;
i = 0;
while (big[i])
{
if (little[i] != big[i])
return (0);
i++;
if (!little[i])
return (1);
}
return (0);
}
char *get_value_by_key(char *key, t_list **head)
{
t_list *current;
current = *head;
while (current != NULL)
{
if (is_start(current->content, key))
return (ft_strchr(current->content, '=') + 1);
current = current->next;
}
return (NULL);
}
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);
}
/*int main(int argc, char *argv[], char **env)
{
(void)argc;
(void)argv;
ft_putstr_fd(get_value_index(10, init_env(env)), 1);
//print_env(init_env(env), 1);
return (0);
}*/