diff --git a/Makefile b/Makefile index a5c2e5d..937333d 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ NAME = minishell CC = gcc -CFLAGS = -Wall -Werror -Wextra -g +CFLAGS = -Werror -Wextra -g LIBS = libftx/libftx.a diff --git a/env.c b/env.c index 58309b5..052a97f 100644 --- a/env.c +++ b/env.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ -/* Updated: 2023/02/03 16:04:16 by erey-bet ### ########.fr */ +/* Updated: 2023/02/16 14:30:12 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,18 +30,14 @@ void print_export(t_list **head, int fd) int v; current = *head; - while (current != NULL) + while (current->next != 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); - } + 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; } } @@ -51,9 +47,11 @@ void print_env(t_list **head, int fd) t_list *current; current = *head; - while (current != NULL) + while (current->next != NULL) { - ft_putstr_fd(current->content, fd); + 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; } @@ -77,16 +75,52 @@ int strcmp_alphabet(char *s1, char *s2) return (-1); } -void ft_double_swap(char **a, char **b) +void ft_double_swap(void *a, void *b) { void *c; - c = *a; - *a = *b; - *b = c; + c = a; + a = b; + b = c; } -void exchange(char **a, char **b, char **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); +} + +void swap_env_3(void **a, void **b, void **c) { void *d; @@ -96,100 +130,82 @@ void exchange(char **a, char **b, char **c) *c = d; } -void add_sort(t_list **head, char *str) +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; - char *last; + t_env *last; current = *head; - while (current->next != NULL && strcmp_alphabet(str, current->content) != 0) + while (current->next != NULL && strcmp_alphabet(var->key, ((t_env*)(current->content))->key) != 0) current = current->next; - if (strcmp_alphabet(str, current->content) == 1) - last = str; + if (current->content == NULL) + current->content = var; 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) + last = NULL; + swap_env_3((void **)&last, ¤t->content, (void **)&var); + while (current->next != NULL) { - current->content = last; - return ; + current = current->next; + swap_env(¤t->content, (void **)&last); } - else - ft_double_swap((char **)(¤t->content), &last); } + if (current->next == NULL) + current->next = ft_calloc(1, sizeof(t_list)); } -char *get_value_index(int index, t_list **head) +char *get_value_by_key(char *key, t_list **head) { t_list *current; - int i; current = *head; - i = -1; - while (current != NULL && ++i != index) + while (current->next != NULL) + { + if (ft_strcmp(((t_env *)current->content)->key, key) == 0) + return (((t_env *)current->content)->value); current = current->next; - if (i == index) - return (ft_strchr(current->content, '=') + 1); + } return (NULL); } -int is_start(char *big, char *little) +int set_value_by_key(char *key, char *value, t_list **head) { - int i; + t_list *current; - i = 0; - while (big[i]) + current = *head; + while (current->next != NULL) { - if (little[i] != big[i]) + if (ft_strcmp(((t_env *)current->content)->key, key) == 0) + { + free(((t_env *)current->content)->value); + ((t_env *)current->content)->value = value; 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 (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); } @@ -197,27 +213,23 @@ 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]) - add_sort(head, 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); } - -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); -} diff --git a/env.o b/env.o index dc97556..b0d6e9c 100644 Binary files a/env.o and b/env.o differ diff --git a/file.o b/file.o new file mode 100644 index 0000000..eb1d89e Binary files /dev/null and b/file.o differ diff --git a/ft_split_quoted.o b/ft_split_quoted.o new file mode 100644 index 0000000..31efd61 Binary files /dev/null and b/ft_split_quoted.o differ diff --git a/heredoc.o b/heredoc.o new file mode 100644 index 0000000..75d71cb Binary files /dev/null and b/heredoc.o differ diff --git a/infile.o b/infile.o new file mode 100644 index 0000000..a2fcd31 Binary files /dev/null and b/infile.o differ diff --git a/libftx/extra/extra.a b/libftx/extra/extra.a index caeb9df..c1c0387 100644 Binary files a/libftx/extra/extra.a and b/libftx/extra/extra.a differ diff --git a/libftx/extra/ft_strndup.o b/libftx/extra/ft_strndup.o index 1866899..4d63558 100644 Binary files a/libftx/extra/ft_strndup.o and b/libftx/extra/ft_strndup.o differ diff --git a/libftx/extra/ft_swap.o b/libftx/extra/ft_swap.o new file mode 100644 index 0000000..6284db1 Binary files /dev/null and b/libftx/extra/ft_swap.o differ diff --git a/libftx/libft/ft_lstadd_back.o b/libftx/libft/ft_lstadd_back.o new file mode 100644 index 0000000..3c154e2 Binary files /dev/null and b/libftx/libft/ft_lstadd_back.o differ diff --git a/libftx/libft/ft_lstadd_front.o b/libftx/libft/ft_lstadd_front.o new file mode 100644 index 0000000..891e90e Binary files /dev/null and b/libftx/libft/ft_lstadd_front.o differ diff --git a/libftx/libft/ft_lstclear.o b/libftx/libft/ft_lstclear.o new file mode 100644 index 0000000..7a150d6 Binary files /dev/null and b/libftx/libft/ft_lstclear.o differ diff --git a/libftx/libft/ft_lstdelone.o b/libftx/libft/ft_lstdelone.o new file mode 100644 index 0000000..d7ffe7a Binary files /dev/null and b/libftx/libft/ft_lstdelone.o differ diff --git a/libftx/libft/ft_lstiter.o b/libftx/libft/ft_lstiter.o new file mode 100644 index 0000000..285e7ba Binary files /dev/null and b/libftx/libft/ft_lstiter.o differ diff --git a/libftx/libft/ft_lstlast.o b/libftx/libft/ft_lstlast.o new file mode 100644 index 0000000..a3c0874 Binary files /dev/null and b/libftx/libft/ft_lstlast.o differ diff --git a/libftx/libft/ft_lstmap.o b/libftx/libft/ft_lstmap.o new file mode 100644 index 0000000..da2f69a Binary files /dev/null and b/libftx/libft/ft_lstmap.o differ diff --git a/libftx/libft/ft_lstnew.o b/libftx/libft/ft_lstnew.o new file mode 100644 index 0000000..1c4d55f Binary files /dev/null and b/libftx/libft/ft_lstnew.o differ diff --git a/libftx/libft/ft_lstsize.o b/libftx/libft/ft_lstsize.o new file mode 100644 index 0000000..4031bad Binary files /dev/null and b/libftx/libft/ft_lstsize.o differ diff --git a/libftx/libft/ft_split.o b/libftx/libft/ft_split.o index 4194ea3..8ef5b89 100644 Binary files a/libftx/libft/ft_split.o and b/libftx/libft/ft_split.o differ diff --git a/libftx/libft/libft.a b/libftx/libft/libft.a index 64fb516..2239fec 100644 Binary files a/libftx/libft/libft.a and b/libftx/libft/libft.a differ diff --git a/libftx/libftx.a b/libftx/libftx.a index 6919e75..59da582 100644 Binary files a/libftx/libftx.a and b/libftx/libftx.a differ diff --git a/libftx/printf/ft_dprintarg.o b/libftx/printf/ft_dprintarg.o index b3e6c91..8c59e74 100644 Binary files a/libftx/printf/ft_dprintarg.o and b/libftx/printf/ft_dprintarg.o differ diff --git a/libftx/printf/ft_dprintptr.o b/libftx/printf/ft_dprintptr.o index 720b848..99eb28d 100644 Binary files a/libftx/printf/ft_dprintptr.o and b/libftx/printf/ft_dprintptr.o differ diff --git a/libftx/printf/ft_dprintstrtab.o b/libftx/printf/ft_dprintstrtab.o index d3ce17f..eca97f5 100644 Binary files a/libftx/printf/ft_dprintstrtab.o and b/libftx/printf/ft_dprintstrtab.o differ diff --git a/libftx/printf/ft_eprintf.o b/libftx/printf/ft_eprintf.o index 6e841d1..d9ee39f 100644 Binary files a/libftx/printf/ft_eprintf.o and b/libftx/printf/ft_eprintf.o differ diff --git a/libftx/printf/ft_printf.a b/libftx/printf/ft_printf.a index 13f9344..c3b3151 100644 Binary files a/libftx/printf/ft_printf.a and b/libftx/printf/ft_printf.a differ diff --git a/libftx/printf/ft_putstr_fd.o b/libftx/printf/ft_putstr_fd.o index c1d1ae9..a54b23b 100644 Binary files a/libftx/printf/ft_putstr_fd.o and b/libftx/printf/ft_putstr_fd.o differ diff --git a/main.c b/main.c index 31d45e4..274ccfb 100644 --- a/main.c +++ b/main.c @@ -208,12 +208,20 @@ int ft_cmds_excutor(t_list **cmds) return (0); } -int main(int ac, char **av) +/*int main(int ac, char **av, char **env) { t_list **cmds; + t_data data; if (ac == 1) return (1); + data.env = init_env(env); + if (data.env == NULL) + return (1); + ft_putstr_fd(get_value_key("PATH", data.env), 1); + ft_putstr_fd("/n", 1); + set_value_key("PATH", i_env, "BITE"); + ft_putstr_fd(get_value_key("PATH", data.env), 1); cmds = ft_parse_cmds(av[1]); if (cmds == NULL) return (1); @@ -225,4 +233,4 @@ int main(int ac, char **av) ft_lstclear(cmds, ft_lstdel); free(cmds); return (1); -} +}*/ diff --git a/main.o b/main.o new file mode 100644 index 0000000..3619299 Binary files /dev/null and b/main.o differ diff --git a/minishell b/minishell index c1bc176..4929cbe 100755 Binary files a/minishell and b/minishell differ diff --git a/minishell.h b/minishell.h index 86edcfd..b4b69c2 100644 --- a/minishell.h +++ b/minishell.h @@ -37,4 +37,9 @@ typedef struct s_data t_list **env; } t_data; +typedef struct s_env +{ + void *key; + void *value; +} t_env; #endif diff --git a/outfile.o b/outfile.o new file mode 100644 index 0000000..439614a Binary files /dev/null and b/outfile.o differ diff --git a/syntatics.o b/syntatics.o new file mode 100644 index 0000000..239bcc1 Binary files /dev/null and b/syntatics.o differ diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o index ca97c05..2ffa519 100644 Binary files a/utils/ft_getstr.o and b/utils/ft_getstr.o differ diff --git a/utils/ft_is_in_quote.o b/utils/ft_is_in_quote.o index 3c3b6ac..cd3bfd1 100644 Binary files a/utils/ft_is_in_quote.o and b/utils/ft_is_in_quote.o differ diff --git a/utils/ft_strnchr.o b/utils/ft_strnchr.o index 8b0ac81..5b7b1f8 100644 Binary files a/utils/ft_strnchr.o and b/utils/ft_strnchr.o differ diff --git a/utils/ft_strncpy.o b/utils/ft_strncpy.o index c5a7006..e5ec8f8 100644 Binary files a/utils/ft_strncpy.o and b/utils/ft_strncpy.o differ diff --git a/utils/ft_strreplace.c b/utils/ft_strreplace.c index dbfb6a5..6879893 100644 --- a/utils/ft_strreplace.c +++ b/utils/ft_strreplace.c @@ -1,24 +1,17 @@ #include "utils.h" -<<<<<<< HEAD -int ft_strreplace(char **str, char *fill, size_t start, size_t stop) -======= -char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop) ->>>>>>> 67fb6d0533f56a0d63ffeebd9fde7cdc75919eb2 +char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop) { char *out; size_t sum; - out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 - * sizeof(char))); + out = malloc((ft_strlen(str) + ft_strlen(fill) - (stop - start) + 1 * sizeof(char))); if (out == NULL) - return (1); + return (NULL); ft_strncpy(out, str, start); ft_strncpy(out + start, fill, ft_strlen(fill)); sum = start + ft_strlen(fill); ft_strncpy(out + sum, str + stop, ft_strlen(str) - stop); out[sum + ft_strlen(str) - stop] = '\0'; - free(*str); - *str = out; - return (0); + return (out); } diff --git a/utils/ft_strreplace.o b/utils/ft_strreplace.o index 1422e58..e799f33 100644 Binary files a/utils/ft_strreplace.o and b/utils/ft_strreplace.o differ diff --git a/utils/utils.h b/utils/utils.h index 23df973..0e3aa71 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -5,7 +5,7 @@ size_t ft_strncpy(char *dst, const char *src, size_t n); int ft_is_in_quote(const char *str, size_t n); -char *ft_strreplace(char *str, const char *fill, size_t start, size_t stop); +char *ft_strreplace(const char *str, const char *fill, size_t start, size_t stop); ssize_t ft_strnchr(const char *str, char c); char *ft_getstr(const char *str, size_t n);