merge
This commit is contained in:
parent
82a0af80ad
commit
de8dfc41d4
242
env.c
Normal file
242
env.c
Normal file
@ -0,0 +1,242 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/02/16 15:13:35 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;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
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);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
|
||||
void print_env(t_list **head, int fd)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
ft_putstr_fd(((t_env*)(current->content))->key, fd);
|
||||
ft_putstr_fd("=", fd);
|
||||
ft_putstr_fd(((t_env*)(current->content))->value, 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(void *a, void *b)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
void env_del(void *ptr)
|
||||
{
|
||||
t_env *content;
|
||||
|
||||
content = ptr;
|
||||
free(content->key);
|
||||
free(content->value);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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 add_sort(t_list **head, t_env *var)
|
||||
{
|
||||
t_list *current;
|
||||
t_env *last;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0)
|
||||
current = current->next;
|
||||
if (current->content == NULL)
|
||||
current->content = var;
|
||||
else
|
||||
{
|
||||
last = NULL;
|
||||
swap_env_3((void **)&last, ¤t->content, (void **)&var);
|
||||
while (current->next != NULL)
|
||||
{
|
||||
current = current->next;
|
||||
swap_env(¤t->content, (void **)&last);
|
||||
}
|
||||
}
|
||||
if (current->next == NULL)
|
||||
current->next = ft_calloc(1, sizeof(t_list));
|
||||
}
|
||||
|
||||
char *get_value_by_key(char *key, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
return (((t_env *)current->content)->value);
|
||||
current = current->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int set_value_by_key(char *key, char *value, t_list **head)
|
||||
{
|
||||
t_list *current;
|
||||
|
||||
current = *head;
|
||||
while (current->next != NULL)
|
||||
{
|
||||
if (ft_strcmp(((t_env *)current->content)->key, key) == 0)
|
||||
{
|
||||
free(((t_env *)current->content)->value);
|
||||
((t_env *)current->content)->value = value;
|
||||
return (0);
|
||||
}
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_list **init_env(char **env)
|
||||
{
|
||||
t_list **head;
|
||||
int i;
|
||||
t_env *var;
|
||||
|
||||
head = ft_calloc(1, sizeof(t_list *));
|
||||
if (head == NULL)
|
||||
return (NULL);
|
||||
*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);
|
||||
}
|
||||
return (head);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
|
||||
/* Updated: 2023/02/15 18:03:06 by cchauvet ### ########.fr */
|
||||
/* Updated: 2023/02/16 15:11:53 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -57,4 +57,10 @@ typedef struct s_data
|
||||
int heredoc;
|
||||
} t_data;
|
||||
|
||||
typedef struct s_env
|
||||
{
|
||||
void *key;
|
||||
void *value;
|
||||
} t_env;
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user