/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* env.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ /* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.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 **)(¤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_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_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); } int set_value_key(char *key, t_list **head, char *str) { t_list *current; char *temp; size_t len; current = *head; while (current != NULL) { if (is_start(current->content, key)) { len = ft_strlen(current->content); temp = current->content; current->content = ft_strreplace(temp, str, ft_strnchr(temp, '=') + 1, len - get_index(temp, '=') - 1); free(temp); if (current->content == NULL) return (1); break ; } 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); } int main(int argc, char *argv[], char **env) { t_list **i_env; (void)argc; (void)argv; i_env = init_env(env); ft_putstr_fd(get_value_key("PATH", i_env), 1); ft_putstr_fd("/n", 1); set_value_key("PATH", i_env, "BITE"); ft_putstr_fd(get_value_key("PATH", i_env), 1); return (0); }