diff --git a/.gitignore b/.gitignore index 7f7880e..3eb33e5 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ compile_commands.json .cache libft/**/*.o libft/**/*.a +src/main.c diff --git a/src/env/env_cmd.c b/src/env/env_cmd.c index 6d2d220..569b5c0 100644 --- a/src/env/env_cmd.c +++ b/src/env/env_cmd.c @@ -5,109 +5,13 @@ /* +:+ +:+ +:+ */ /* By: mmoussou name) - + ft_strlen(env->content) + 2); - if (!exec_env[i]) - { - ft_free("a", exec_env); - return (NULL); - } - ft_strlcpy(exec_env[i], env->name, ft_strlen(env->name) + 1); - exec_env[i][ft_strlen(env->name)] = '='; - ft_strlcpy(exec_env[i] + ft_strlen(env->name) + 1, - env->content, ft_strlen(env->content) + 1); - env = env->next; - i++; - } - return (exec_env); -} - -int env_create_first_el(char *env_line, t_env *env) -{ - char *name; - char *content; - - content = ft_strdup(ft_strchr(env_line, '=') + 1); - if (!content) - return (-1); - ft_strchr(env_line, '=')[0] = 0; - name = ft_strdup(env_line); - if (!name) - return (-1); - env->name = name; - env->content = content; - env->next = NULL; - return (0); -} - -t_env *env_create_el(char *env_line) -{ - t_env *new; - char *name; - char *content; - - content = ft_strdup(ft_strchr(env_line, '=') + 1); - if (!content) - return (NULL); - ft_strchr(env_line, '=')[0] = 0; - name = ft_strdup(env_line); - if (!name) - return (NULL); - new = ft_envnew(name, content); - return (new); -} - -int env_init(char **env_d, t_env *env) -{ - t_env *new; - int i; - char bool_first_el; - - i = 0; - bool_first_el = true; - while (env_d[i]) - { - if (bool_first_el) - i = env_create_first_el(env_d[i], env); - if (bool_first_el && i < 0) - return (1); - if (bool_first_el) - bool_first_el = false; - else - { - new = env_create_el(env_d[i]); - if (!new) - { - ft_envclear(&env, free); - return (1); - } - ft_envadd_back(&env, new); - } - i++; - } - return (0); -} - int env_append(char *name, char *content, t_env *env) { char *new_content; @@ -117,6 +21,8 @@ int env_append(char *name, char *content, t_env *env) if (!env) return (-1); new_content = ft_calloc(1, ft_strlen(env->content) + ft_strlen(content)); + if (!new_content) + return (-1); ft_strlcpy(new_content, env->content, ft_strlen(env->content) + 1); ft_strlcpy(new_content + ft_strlen(env->content), content, ft_strlen(content) + 1); diff --git a/src/env/env_setters.c b/src/env/env_setters.c new file mode 100644 index 0000000..05c1c18 --- /dev/null +++ b/src/env/env_setters.c @@ -0,0 +1,112 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_setters.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mmoussou name = name; + env->content = content; + env->next = NULL; + return (0); +} + +t_env *env_create_el(char *env_line) +{ + t_env *new; + char *name; + char *content; + + content = ft_strdup(ft_strchr(env_line, '=') + 1); + if (!content) + return (NULL); + ft_strchr(env_line, '=')[0] = 0; + name = ft_strdup(env_line); + if (!name) + { + free(content); + return (NULL); + } + new = ft_envnew(name, content); + return (new); +} + +int env_init(char **env_d, t_env *env) +{ + t_env *new; + int i; + char bool_first_el; + + i = -1; + bool_first_el = true; + while (env_d[++i]) + { + if (bool_first_el) + i = env_create_first_el(env_d[i], env); + if (bool_first_el && i < 0) + return (1); + else + { + new = env_create_el(env_d[i]); + if (!new) + { + ft_envclear(&env, free); + return (1); + } + ft_envadd_back(&env, new); + } + bool_first_el = false; + } + return (0); +} + +char **env_get(t_env *env) +{ + char **exec_env; + int i; + + exec_env = malloc(sizeof(char *) * (ft_envsize(env) + 1)); + if (!exec_env) + return (exec_env); + exec_env[ft_envsize(env)] = NULL; + i = 0; + while (env) + { + exec_env[i] = malloc(ft_strlen(env->name) + + ft_strlen(env->content) + 2); + if (!exec_env[i]) + { + ft_free("a", exec_env); + return (NULL); + } + ft_strlcpy(exec_env[i], env->name, ft_strlen(env->name) + 1); + exec_env[i][ft_strlen(env->name)] = '='; + ft_strlcpy(exec_env[i] + ft_strlen(env->name) + 1, + env->content, ft_strlen(env->content) + 1); + env = env->next; + i++; + } + return (exec_env); +} diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index b207e86..33bcb36 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,47 +6,101 @@ /* By: mmoussou : close ce fd + -1: pour signifier qu'il faut decrémenter la static + */ +int fd_manager(int mode) +{ + static int index = 0; + char *index_itoa; + char *path; + int fd; + + if (mode > 0) + close(mode); + if (mode) + { + index--; + return (0); + } + path = ft_calloc(sizeof(char), 24 + 3); + if (!path) + return (-1); + index_itoa = ft_itoa(index); + if (!index_itoa) + free(path); + if (!index_itoa) + return (-1); + index++; + strcat(path, "/tmp/.minishell-heredoc-"); + strcat(path, index_itoa); + fd = open(path, O_RDONLY | O_WRONLY | O_TRUNC | O_CREAT, 0644); + return (fd); +} + +static int get_input(char *delimiter, int fd) +{ + char *line; + int status; + + printf("%s | ", delimiter); + line = readline("heredoc> "); + while (ft_strcmp(line, delimiter)) + { + status = write(fd, line, ft_strlen(line)); + if (status == -1) + fd_manager(fd); + if (status == -1) + return (-1); + status = write(fd, "\n", 1); + if (status == -1) + fd_manager(fd); + if (status == -1) + return (-1); + free(line); + line = readline("heredoc> "); + } + free(line); + status = write(fd, "\0", 1); + if (status == -1) + fd_manager(fd); + if (status == -1) + return (-1); + return (0); +} + int ft_heredoc(char *delimiter) { int fork_pid; - int tmp_fd; - int status; - char *line; + int fd; - tmp_fd = open("/tmp/.minishell-heredoc", O_RDONLY | O_WRONLY | O_TRUNC | O_CREAT, 0644); - if (tmp_fd == -1) + fd = fd_manager(0); + if (fd == -1) + { + fd_manager(-1); return (-1); + } fork_pid = fork(); if (fork_pid == -1) { - close(tmp_fd); + fd_manager(fd); return (-1); } if (!fork_pid) { - line = readline("heredoc> "); - while (ft_strcmp(line, delimiter)) - { - status = write(tmp_fd, line, ft_strlen(line)); - status = write(tmp_fd, "\n", 1); - free(line); - line = readline("heredoc> "); - } - free(line); - status = write(tmp_fd, "\0", 1); - if (status == -1) - { - close(tmp_fd); - return (-1); - } + get_input(delimiter, fd); + exit(0); } else waitpid(fork_pid, NULL, 0); - return (tmp_fd); + return (fd); } diff --git a/src/main.c b/src/main.c index 77f5902..2b247b1 100644 --- a/src/main.c +++ b/src/main.c @@ -3,7 +3,7 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: adjoly +#+ +:+ +#+ */ +/* By: mmoussou