/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Updated: 2023/03/09 15:06:44 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" void add_sort(t_list **head, t_env *var) { t_list *current; t_env *last; current = *head; while (current->next != NULL && ft_strcmp(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 (""); } 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; content->original = 0; 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]); var->original = 1; add_sort(head, var); } return (head); } /*int main(int argc, char *argv[], char **env) { t_list **n_env; n_env = init_env(env); delete_by_key("SSH_AUTH_SOCK", n_env); print_env(n_env, 1); return (0); }*/