diff --git a/Makefile b/Makefile index c35c260..31444cc 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c execution.c spacer.c env_fill.c echo.c pwd.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c OBJS = ${SRCS:.c=.o} diff --git a/echo.c b/builtins/echo.c similarity index 94% rename from echo.c rename to builtins/echo.c index 0b98c2c..06d4701 100644 --- a/echo.c +++ b/builtins/echo.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 13:09:08 by erey-bet #+# #+# */ -/* Updated: 2023/02/17 16:53:04 by erey-bet ### ########.fr */ +/* Updated: 2023/02/17 17:07:50 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "../minishell.h" int is_space(char c) { diff --git a/builtin/env.c b/builtins/env.c similarity index 80% rename from builtin/env.c rename to builtins/env.c index f97a271..87a2b34 100644 --- a/builtin/env.c +++ b/builtins/env.c @@ -12,14 +12,17 @@ #include "../minishell.h" -int main(char **env) +int print_env(t_list **head, int fd) { - t_list **head; t_list *current; - while (current != NULL) + current = *head; + while (current->next != NULL) { - ft_putendl_fd(1, current->content); + 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; } return (0); diff --git a/builtin/export.c b/builtins/export.c similarity index 68% rename from builtin/export.c rename to builtins/export.c index ef2ba11..bcf7955 100644 --- a/builtin/export.c +++ b/builtins/export.c @@ -12,29 +12,20 @@ #include "../minishell.h" -int main(char **env) +int print_export(t_list **head, int fd) { - t_list **env_lst; t_list *current; - char *key; - char *value; - env_lst = init_env(env); - current = *env_lst; - while (current != NULL) + current = *head; + while (current->next != NULL) { - value = ft_strchr(current->content, '=') + 1; - key = ft_strndup(current->content, - ft_strlen(current->content) - ft_strlen(value)); - if (key == NULL) - { - ft_lstclear(env_lst, env_del); - return (1); - } - ft_printf("declare -x %s=\"%s\"\n", key, value); - free(key); + 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; } - ft_lstclear(env_lst, env_del); return (0); } diff --git a/pwd.c b/builtins/pwd.c similarity index 91% rename from pwd.c rename to builtins/pwd.c index 0905d00..e978d21 100644 --- a/pwd.c +++ b/builtins/pwd.c @@ -6,11 +6,11 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ -/* Updated: 2023/02/17 16:55:42 by erey-bet ### ########.fr */ +/* Updated: 2023/02/17 17:08:00 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "../minishell.h" int pwd(t_list **env, int fd) { diff --git a/env.c b/env.c index ca51da5..2d4d8d4 100644 --- a/env.c +++ b/env.c @@ -6,154 +6,20 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ -/* Updated: 2023/02/17 13:05:29 by erey-bet ### ########.fr */ +/* Updated: 2023/02/17 17:11:03 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; - - 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); - free(content); -} - -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) + while (current->next != NULL + && ft_strcmp(var->key, ((t_env *)(current->content))->key) != 0) current = current->next; if (current->content == NULL) current->content = var; @@ -182,7 +48,7 @@ char *get_value_by_key(char *key, t_list **head) return (((t_env *)current->content)->value); current = current->next; } - return (NULL); + return (""); } int set_value_by_key(char *key, char *value, t_list **head) @@ -218,25 +84,6 @@ int create_value_by_key(char *key, char *value, t_list **head) return (0); } -char **env_to_strs(t_list **head) -{ - t_list *current; - t_env *content; - char **env; - int i; - - current = *head; - env = ft_calloc(ft_lstsize(*head), sizeof(char *)); - i = 0; - while (current->content) - { - content = current->content; - env[i++] = ft_strmerger(3, content->key, "=", content->value); - current = current->next; - } - return (env); -} - t_list **init_env(char **env) { t_list **head; @@ -261,12 +108,3 @@ t_list **init_env(char **env) } return (head); } - -/*int main(int argc, char *argv[], char **env) -{ - t_list **new; - - new = init_env(env); - ft_printf("%S", env_to_strs(new)); - return (0); -}*/ diff --git a/env2.c b/env2.c new file mode 100644 index 0000000..0a93049 --- /dev/null +++ b/env2.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/17 17:22:01 by erey-bet #+# #+# */ +/* Updated: 2023/02/17 17:24:57 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 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 env_del(void *ptr) +{ + t_env *content; + + content = ptr; + free(content->key); + free(content->value); + free(content); +} + +char **env_to_strs(t_list **head) +{ + t_list *current; + t_env *content; + char **env; + int i; + + current = *head; + env = ft_calloc(ft_lstsize(*head), sizeof(char *)); + i = 0; + while (current->content) + { + content = current->content; + env[i++] = ft_strmerger(3, content->key, "=", content->value); + current = current->next; + } + return (env); +} diff --git a/env3.c b/env3.c new file mode 100644 index 0000000..3be208a --- /dev/null +++ b/env3.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env3.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: erey-bet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/02/17 17:25:09 by erey-bet #+# #+# */ +/* Updated: 2023/02/17 17:31:31 by erey-bet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +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); +} diff --git a/minishell.h b/minishell.h index 7c67f58..6c2ef78 100644 --- a/minishell.h +++ b/minishell.h @@ -47,6 +47,13 @@ char **env_to_strs(t_list **head); int create_value_by_key(char *key, char *value, t_list **head); int set_value_by_key(char *key, char *value, t_list **head); char *get_value_by_key(char *key, t_list **head); +int get_index(char *s, char c); +void swap_env_3(void **a, void **b, void **c); +void swap_env(void **a, void **b); +void env_del(void *ptr); +char **env_to_strs(t_list **head); +char *get_value(char *str); +char *get_key(char *str); /* Echo */ int echo(int fd, char *str);