42_minishell/env.c

236 lines
4.5 KiB
C
Raw Normal View History

2023-02-02 11:40:09 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
/* Updated: 2023/02/16 14:30:12 by erey-bet ### ########.fr */
2023-02-02 11:40:09 -05:00
/* */
/* ************************************************************************** */
2023-02-10 07:19:28 -05:00
#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);
}
2023-02-01 11:08:02 -05:00
2023-02-02 11:40:09 -05:00
void print_export(t_list **head, int fd)
{
t_list *current;
char *ctn;
2023-02-10 07:19:28 -05:00
int v;
2023-02-02 11:40:09 -05:00
current = *head;
while (current->next != NULL)
2023-02-02 11:40:09 -05:00
{
write(fd, "declare -x ", 11);
ft_putstr_fd(((t_env*)(current->content))->key, fd);
ft_putstr_fd("=", fd);
write(fd, "\"", 1);
ft_putstr_fd(((t_env*)(current->content))->value, fd);
write(fd, "\"\n", 2);
2023-02-02 11:40:09 -05:00
current = current->next;
}
}
void print_env(t_list **head, int fd)
{
t_list *current;
current = *head;
while (current->next != NULL)
2023-02-02 11:40:09 -05:00
{
ft_putstr_fd(((t_env*)(current->content))->key, fd);
ft_putstr_fd("=", fd);
ft_putstr_fd(((t_env*)(current->content))->value, fd);
2023-02-02 11:40:09 -05:00
write(fd, "\n", 1);
current = current->next;
}
}
2023-02-01 11:08:02 -05:00
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(void *a, void *b)
2023-02-01 11:08:02 -05:00
{
void *c;
c = a;
a = b;
b = c;
}
void exchange(void *a, void *b, void *c)
{
void *d;
d = a;
a = b;
b = c;
c = d;
}
char *get_value(char *str)
{
char *s;
int i;
int start;
s = ft_calloc(ft_strlen(str), sizeof(char));
start = get_index(str, '=');
i = start;
while (str[++i])
s[i - start - 1] = str[i];
return (s);
}
char *get_key(char *str)
{
char *s;
int i;
s = ft_calloc(ft_strlen(str), sizeof(char));
i = -1;
while (str[++i] != '=')
s[i] = str[i];
return (s);
2023-02-01 11:08:02 -05:00
}
void swap_env_3(void **a, void **b, void **c)
2023-02-02 11:40:09 -05:00
{
void *d;
d = *a;
*a = *b;
*b = *c;
*c = d;
}
void swap_env(void **a, void **b)
2023-02-01 11:08:02 -05:00
{
void *c;
2023-02-01 11:08:02 -05:00
c = *a;
*a = *b;
*b = c;
2023-02-01 11:08:02 -05:00
}
void add_sort(t_list **head, t_env *var)
2023-02-10 07:19:28 -05:00
{
t_list *current;
t_env *last;
2023-02-10 07:19:28 -05:00
current = *head;
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
2023-02-10 07:19:28 -05:00
current = current->next;
if (current->content == NULL)
current->content = var;
else
2023-02-10 07:19:28 -05:00
{
last = NULL;
swap_env_3((void **)&last, &current->content, (void **)&var);
while (current->next != NULL)
{
current = current->next;
swap_env(&current->content, (void **)&last);
}
2023-02-10 07:19:28 -05:00
}
if (current->next == NULL)
current->next = ft_calloc(1, sizeof(t_list));
2023-02-10 07:19:28 -05:00
}
char *get_value_by_key(char *key, t_list **head)
2023-02-10 07:19:28 -05:00
{
t_list *current;
current = *head;
while (current->next != NULL)
2023-02-10 07:19:28 -05:00
{
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
return (((t_env *)current->content)->value);
2023-02-10 07:19:28 -05:00
current = current->next;
}
return (NULL);
}
int set_value_by_key(char *key, char *value, t_list **head)
2023-02-14 07:54:41 -05:00
{
t_list *current;
current = *head;
while (current->next != NULL)
2023-02-14 07:54:41 -05:00
{
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
2023-02-14 07:54:41 -05:00
{
free(((t_env *)current->content)->value);
((t_env *)current->content)->value = value;
return (0);
2023-02-14 07:54:41 -05:00
}
current = current->next;
}
return (1);
}
int create_value_by_key(char *key, char *value, t_list **head)
{
t_env *content;
if (set_value_by_key(key, value, head) == 0)
return (0);
content = ft_calloc(1, sizeof(t_env));
if (content == NULL)
return (1);
content->key = key;
content->value = value;
add_sort(head, content);
2023-02-14 07:54:41 -05:00
return (0);
}
2023-02-01 11:08:02 -05:00
t_list **init_env(char **env)
{
t_list **head;
int i;
t_env *var;
2023-02-01 11:08:02 -05:00
2023-02-02 11:40:09 -05:00
head = ft_calloc(1, sizeof(t_list *));
if (head == NULL)
return (NULL);
2023-02-01 11:08:02 -05:00
*head = ft_calloc(1, sizeof(t_list));
if (*head == NULL)
return (NULL);
i = -1;
while (env[++i])
{
var = ft_calloc(1, sizeof(t_env));
if (var == NULL)
return (NULL);
var->key = get_key(env[i]);
var->value = get_value(env[i]);
add_sort(head, var);
}
2023-02-02 11:40:09 -05:00
return (head);
2023-02-01 11:08:02 -05:00
}