/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Updated: 2023/02/15 19:58:19 by cchauvet ### ########.fr */ /* */ /* ************************************************************************** */ #include "libftx/libftx.h" #include "minishell.h" #include "utils/utils.h" void env_del(void *content) { (void) content; } 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 **)(¤t->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 **)(¤t->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); } char *get_value_by_key(char *key, t_list **head) { t_list *current; current = *head; while (current != NULL) { if (ft_strncmp(current->content, key, ft_strlen(key))) return (ft_strchr(current->content, '=') + 1); current = current->next; } return (NULL); } int set_value_key(t_list **env, char *key, char *value) { t_list *current; char *temp; current = *env; while (current != NULL) { if (!ft_strncmp(key, current->content, ft_strlen(current->content))) { temp = current->content; current->content = ft_strreplace(temp, value, ft_strnchr(temp, '=') + 1, ft_strlen(temp) - (ft_strnchr(temp, '=') - 1)); free(temp); if (current->content == NULL) { ft_eprintf("minishell: malloc failed"); return (1); } } current = current->next; } return (0); } 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); }