diff --git a/Makefile b/Makefile index e8aba2a..a970ad0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,30 @@ -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 utils/ft_atoi_check.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 builtins/export.c builtins/env.c builtins/cd.c builtins/exit.c builtins/unset.c +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 utils/ft_atoi_check.c ./utils/ft_get_executable.c ./utils/fd.c + +BUILTINS_SRC = builtins/pwd.c \ + builtins/export.c \ + builtins/env.c \ + builtins/cd.c \ + builtins/exit.c \ + builtins/unset.c \ + builtins/echo.c + +SRCS = ${BUILTINS_SRC} \ + ${UTILS_SRC} \ + main.c \ + ./cmd/cmd.c \ + ./env/env1.c \ + ./env/env2.c \ + ./env/env3.c \ + ./env/env_fill.c \ + ./data/data.c \ + ./execution/execution.c \ + ./syntax/syntax.c \ + ./format/format.c \ + ./redirection/heredoc.c \ + ./redirection/infile.c \ + ./redirection/outfile.c \ + ./redirection/file.c \ + ./parse/cmds_parse.c OBJS = ${SRCS:.c=.o} diff --git a/builtins/:wq b/builtins/:wq new file mode 100644 index 0000000..a29128c --- /dev/null +++ b/builtins/:wq @@ -0,0 +1,8 @@ +#ifndef BUILTINS_PRIVATE_H +# define BUILTINS_PRIVATE_H +# include +# include "../libftx/libftx.h" +# include "../env/env.h" +# include "../utils/utils.h" +char *get_pwd(int fd); +#endif diff --git a/builtins/builtins.h b/builtins/builtins.h new file mode 100644 index 0000000..09ba4d9 --- /dev/null +++ b/builtins/builtins.h @@ -0,0 +1,14 @@ +#ifndef BUILTINS_H +# define BUILTINS_H +# include "../libftx/libft/list.h" + +int echo(int fd, char **strs); +int pwd(int fd); +char *get_pwd(int fd); +int print_env(t_list **env, int fd); +int ft_export(t_list **env, char **args, int fd); +int move_folder(char **args, int fd); +int unset(t_list **env, char **args, int fd); +int ft_exit(char **args); + +#endif diff --git a/builtins/builtins_private.h b/builtins/builtins_private.h new file mode 100644 index 0000000..a29128c --- /dev/null +++ b/builtins/builtins_private.h @@ -0,0 +1,8 @@ +#ifndef BUILTINS_PRIVATE_H +# define BUILTINS_PRIVATE_H +# include +# include "../libftx/libftx.h" +# include "../env/env.h" +# include "../utils/utils.h" +char *get_pwd(int fd); +#endif diff --git a/builtins/cd.c b/builtins/cd.c index 78791c5..92d4291 100644 --- a/builtins/cd.c +++ b/builtins/cd.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int move_folder(char **args, int fd) { @@ -44,21 +44,3 @@ int move_folder(char **args, int fd) return (1); } } - -/*int main(int argc, char *argv[]) { - char cwd[PATH_MAX]; - if (getcwd(cwd, sizeof(cwd)) != NULL) { - printf("%s\n", cwd); - } else { - perror("getcwd() error"); - return 1; - } - move_folder(argv[1], 1); - if (getcwd(cwd, sizeof(cwd)) != NULL) { - printf("%s\n", cwd); - } else { - perror("getcwd() error"); - return 1; - } - return 0; -}*/ diff --git a/builtins/echo.c b/builtins/echo.c index 641313d..ddd3d0d 100644 --- a/builtins/echo.c +++ b/builtins/echo.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int is_space(char c) { diff --git a/builtins/env.c b/builtins/env.c index 3557710..da00299 100644 --- a/builtins/env.c +++ b/builtins/env.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int print_env(t_list **env, int fd) { diff --git a/builtins/exit.c b/builtins/exit.c index c276925..a059e50 100644 --- a/builtins/exit.c +++ b/builtins/exit.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" static int error(int err, char *reason, char *problem) { @@ -39,9 +39,3 @@ int ft_exit(char **args) return(error(err, "numeric argument required", args[0])); return ((ft_atoi(args[0]) % 256 + 256) % 256); } - -/*int main(int argc, char *argv[]) -{ - (void)argc; - return(ft_exit(argv + 1, 1)); -}*/ diff --git a/builtins/export.c b/builtins/export.c index c4d27ee..81db1ab 100644 --- a/builtins/export.c +++ b/builtins/export.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" static int error(char *str, int fd) { @@ -65,7 +65,7 @@ int add_export(t_list **env, char *args, int fd, int *err) return (0); } -int export(t_list **env, char **args, int fd) +int ft_export(t_list **env, char **args, int fd) { int err; int i; @@ -82,13 +82,3 @@ int export(t_list **env, char **args, int fd) } return (err); } -/* -int main(int argc, char *argv[], char **env) -{ - t_list **n_env; - - (void)argc; - n_env = init_env(env); - export(n_env, argv + 1, 1); - return (0); -}*/ diff --git a/builtins/pwd.c b/builtins/pwd.c index aa7f39c..3f3fd8f 100644 --- a/builtins/pwd.c +++ b/builtins/pwd.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int pwd(int fd) { diff --git a/builtins/unset.c b/builtins/unset.c index 8504b61..d6d28aa 100644 --- a/builtins/unset.c +++ b/builtins/unset.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "../minishell.h" +#include "./builtins_private.h" int error(char *str, int fd) { diff --git a/cmd.c b/cmd/cmd.c similarity index 62% rename from cmd.c rename to cmd/cmd.c index 1bdea0f..59d9639 100644 --- a/cmd.c +++ b/cmd/cmd.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include +#include "cmd_private.h" void ft_cmddel(void *ptr) { @@ -21,37 +19,26 @@ void ft_cmddel(void *ptr) content = (t_cmd *) ptr; if (content->args != NULL) ft_freer_tab_ultimate(1, content->args); - if (content->executable != NULL) + if (content->own_cmd == false && content->executable != NULL) free(content->executable); - if (content->fd_in > 2) - close(content->fd_in); - if (content->fd_out > 2) - close(content->fd_out); + if (content->fd_in[0] > 2) + close(content->fd_in[0]); + if (content->fd_out[0] > 2) + close(content->fd_out[0]); + if (content->fd_in[1] > 2) + close(content->fd_in[1]); + if (content->fd_out[1] > 2) + close(content->fd_out[1]); free(content); } -int ft_cmd_filler(t_data *data, t_list *element, char **args) +void ft_cmd_waiter(void *ptr) { - t_cmd *content; - char *temp; - size_t i; + t_cmd *cmd; - if (args == NULL) - return (1); - content = (t_cmd *)element->content; - i = 0; - while (args[i] != NULL) + cmd = ptr; + if (cmd->pid != -1) { - temp = ft_env_filler(data, args[i]); - if (temp == NULL) - return (1); - free(args[i]); - args[i] = temp; - ft_quote_remover(temp); - i++; + waitpid(cmd->pid, ft_get_exit_code(), 0); } - content->args = args; - content->executable = args[0]; - content->pid = -1; - return (0); } diff --git a/cmd/cmd.h b/cmd/cmd.h new file mode 100644 index 0000000..4c34143 --- /dev/null +++ b/cmd/cmd.h @@ -0,0 +1,19 @@ +#ifndef CMD_H +# define CMD_H +# include +# include "../data/data.h" + +typedef struct s_cmd +{ + int fd_in[2]; + int fd_out[2]; + int pid; + char *executable; + char **args; + bool own_cmd; +} t_cmd; + +void ft_cmddel(void *content); +void ft_cmd_waiter(void *content); + +#endif diff --git a/cmd/cmd_private.h b/cmd/cmd_private.h new file mode 100644 index 0000000..d27c80f --- /dev/null +++ b/cmd/cmd_private.h @@ -0,0 +1,8 @@ +#ifndef CMD_PRIVATE_H +# define CMD_PRIVATE_H +# include +# include +# include "./cmd.h" +# include "../libftx/libftx.h" +# include "../data/data.h" +#endif diff --git a/cmds.c b/cmds.c deleted file mode 100644 index 21ac594..0000000 --- a/cmds.c +++ /dev/null @@ -1,112 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* cmds.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet 0) - *cmds = malloc(sizeof(t_list)); - current = *cmds; - i = 0; - while (i < len) - { - content = malloc(sizeof(t_cmd)); - if (content == NULL) - { - ft_lstclear(cmds, ft_cmddel); - return (1); - } - content->args = NULL; - content->executable = NULL; - content->fd_in = -1; - content->fd_out = -1; - current->content = content; - if (!((i + 1) < len)) - { - current->next = NULL; - return (0); - } - current->next = malloc(sizeof(t_list)); - if (current->next == NULL) - ft_lstclear(cmds, ft_cmddel); - current = current->next; - i++; - } - return (0); -} - -static int ft_cmds_prep(t_list **cmds, const char *line, int infile, int outfile) -{ - t_cmd *cmd; - t_list *current; - - if (ft_cmds_init(cmds, ft_seglen_quoted(line, '|'))) - { - free(cmds); - return (1); - } - if (*cmds == NULL) - return (0); - current = *cmds; - cmd = current->content; - cmd->fd_in = infile; - cmd = ft_lstlast(*cmds)->content; - cmd->fd_out = outfile; - return (0); -} - -static int ft_cmds_fill(t_data *data, t_list **cmds, const char *line) -{ - char **tab; - char **args; - t_list *current; - size_t i; - - tab = ft_split_quoted(line, '|'); - if (tab == NULL) - return (1); - i = 0; - current = *cmds; - while (tab[i] != NULL) - { - args = ft_split_quoted(tab[i], ' '); - if (ft_cmd_filler(data, current, args) == 1) - { - ft_lstclear(cmds, ft_cmddel); - ft_freer_tab_ultimate(2, args, tab); - return (1); - } - current = current->next; - i++; - } - ft_freer_tab_ultimate(1, tab); - return (0); -} - -t_list **ft_parse_cmds(t_data *data, char *line, int infile, int outfile) -{ - t_list **cmds; - - cmds = malloc(sizeof(t_list *)); - if (ft_cmds_prep(cmds, line, infile, outfile) == 1) - return (NULL); - if (ft_cmds_fill(data, cmds, line) == 1) - return (NULL); - return (cmds); -} diff --git a/data/data.c b/data/data.c new file mode 100644 index 0000000..0b47650 --- /dev/null +++ b/data/data.c @@ -0,0 +1,25 @@ +#include "./data_private.h" + +int *ft_get_exit_code(void) +{ + static int exit_code = 0; + + return (&exit_code); +} + +int ft_gen_exit_code(t_data *data) +{ + char *str; + + if (data->exit_code_str != NULL) + free(data->exit_code_str); + str = ft_itoa(data->exit_code); + if (str == NULL) + { + ft_printf("minishell: malloc failed"); + return (1); + } + data->exit_code_str = str; + return (0); +} + diff --git a/data/data.h b/data/data.h new file mode 100644 index 0000000..8afc656 --- /dev/null +++ b/data/data.h @@ -0,0 +1,16 @@ +#ifndef DATA_H +# define DATA_H +# include "../libftx/libft/list.h" + +typedef struct s_data +{ + t_list **env; + t_list **cmds; + int exit_code; + char *exit_code_str; +} t_data; + +int *ft_get_exit_code(); +int ft_gen_exit_code(t_data *data); + +#endif diff --git a/data/data_private.h b/data/data_private.h new file mode 100644 index 0000000..e01f9c4 --- /dev/null +++ b/data/data_private.h @@ -0,0 +1,6 @@ +#ifndef DATA_PRIVATE_H +# define DATA_PRIVATE_H +# include "../libftx/libftx.h" +# include "./data.h" + +#endif diff --git a/env/env.h b/env/env.h new file mode 100644 index 0000000..c7da96e --- /dev/null +++ b/env/env.h @@ -0,0 +1,24 @@ +#ifndef ENV_H +# define ENV_H +# include "../libftx/libft/list.h" +# include "../data/data.h" + +typedef struct s_env +{ + char *key; + char *value; +} t_env; + +char *ft_env_filler(t_data *data, const char *str); +void env_del(void *content); +t_list **init_env(char **env); +char **env_to_strs(t_list **head); +int create_value_by_key(char *key, char *value, t_list **head); +int create_value_by_key_dup(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); +void env_del(void *ptr); +int delete_by_key(char *key, t_list **head); +int possible_key(char *key); + +#endif diff --git a/env.c b/env/env1.c similarity index 93% rename from env.c rename to env/env1.c index 4e29c04..239df46 100644 --- a/env.c +++ b/env/env1.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "env_private.h" void add_sort(t_list **head, t_env *var) { @@ -108,14 +108,3 @@ t_list **init_env(char **env) } 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); -}*/ diff --git a/env2.c b/env/env2.c similarity index 97% rename from env2.c rename to env/env2.c index 76f0008..59ad638 100644 --- a/env2.c +++ b/env/env2.c @@ -9,8 +9,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "env_private.h" int get_index(char *s, char c) { diff --git a/env3.c b/env/env3.c similarity index 97% rename from env3.c rename to env/env3.c index 9f6f56b..3635228 100644 --- a/env3.c +++ b/env/env3.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "env_private.h" char *get_value(char *str) { diff --git a/env_fill.c b/env/env_fill.c similarity index 98% rename from env_fill.c rename to env/env_fill.c index 72fdda4..504573b 100644 --- a/env_fill.c +++ b/env/env_fill.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" +#include "./env_private.h" int ft_gen_exit_code_var(t_data *data) { diff --git a/env/env_private.h b/env/env_private.h new file mode 100644 index 0000000..d431242 --- /dev/null +++ b/env/env_private.h @@ -0,0 +1,14 @@ +#ifndef ENV_PRIVATE_H +# define ENV_PRIVATE_H +# include "./env.h" +# include "../libftx/libftx.h" +# include "../utils/utils.h" + +void swap_env_3(void **a, void **b, void **c); +void swap_env(void **a, void **b); +char *get_value(char *str); +char *get_key(char *str); +int get_index(char *str, char c); +char *ft_env_filler(t_data *data, const char *str); + +#endif diff --git a/execution.c b/execution.c deleted file mode 100644 index 1600952..0000000 --- a/execution.c +++ /dev/null @@ -1,188 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* execution.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cchauvet -#include - -static char *ft_get_executable_path(t_data *data, char *executable_name) -{ - char *path; - char *temp; - char **tab; - size_t i; - - if (executable_name == NULL) - return (NULL); - path = NULL; - if (executable_name[0] == '.' || executable_name[0] == '/') - { - path = ft_strdup(executable_name); - if (path == NULL) - { - ft_eprintf("minishell: malloc failed\n"); - return (NULL); - } - if (access(path, X_OK) == 0) - { - ft_eprintf("minishell: %s: permission denied\n", path); - return (NULL); - } - } - else - { - tab = ft_split(get_value_by_key("PATH", data->env), ':'); - if (tab == NULL) - return (NULL); - i = 0; - while (tab[i] != NULL) - { - temp = ft_strmerger(3, tab[i], "/", executable_name); - if (temp == NULL) - { - ft_freer_tab_ultimate(1, tab); - free(executable_name); - ft_eprintf("minishell: malloc failed\n"); - } - if (access(temp, X_OK) == 0) - { - path = temp; - break ; - } - free(temp); - i++; - } - if (path == NULL) - { - ft_eprintf("%s: command not found\n", executable_name); - data->exit_code = 127; - } - ft_freer_tab_ultimate(1, tab); - } - return (path); -} - -static void ft_executor(t_data *data, t_cmd *cmd) -{ - int pid; - char **tab; - - if (cmd->fd_in == -1 || cmd->fd_out == -1) - return ; - pid = fork(); - if (pid == -1) - return ; - if (pid == 0) - { - tab = env_to_strs(data->env); - if (tab == NULL) - return ; - dup2(cmd->fd_out, 1); - dup2(cmd->fd_in, 0); - if (cmd->fd_out > 2) - close(cmd->fd_out); - if (cmd->fd_in > 2) - close(cmd->fd_in); - execve(cmd->executable, cmd->args, tab); - } - cmd->pid = pid; -} - -static int ft_own_cmd(t_data *data, t_cmd *cmd) -{ - int return_code; - int exit_code; - - return_code = -1; - if (ft_strcmp(cmd->executable, "pwd") == 0) - return_code = pwd(cmd->fd_out); - else if (ft_strcmp(cmd->executable, "env") == 0) - return_code = print_env(data->env, cmd->fd_out); - else if (ft_strcmp(cmd->executable, "export") == 0) - return_code = (export(data->env,cmd->args + 1, cmd->fd_out)); - else if (ft_strcmp(cmd->executable, "cd") == 0) - return_code = (move_folder(cmd->args + 1, cmd->fd_out)); - if (ft_strcmp(cmd->executable, "unset") == 0) - return_code = (unset(data->env, cmd->args, cmd->fd_out)); - else if (ft_strcmp(cmd->executable, "echo") == 0) - return_code = (echo(cmd->fd_out, cmd->args + 1)); - else if (ft_strcmp(cmd->executable, "exit") == 0) - { - exit_code = ft_exit(cmd->args + 1); - if (exit_code > -1) - { - data->exit_code = exit_code; - return_code = -2; - } - else - { - data->exit_code = 1; - return_code = -3; - } - } - if (return_code >= 0) - data->exit_code = return_code; - if (return_code != -1) - cmd->executable = NULL; - return (return_code); -} - -int ft_cmds_executor(t_data *data, t_list **cmds) -{ - t_cmd *content; - t_list *current; - int exit_code; - int fds[2]; - - current = *cmds; - while (current != NULL) - { - content = current->content; - if (current->next != NULL) - { - if (pipe(fds) == -1) - { - ft_eprintf("minishell: pipe failed\n"); - return (1); - } - content->fd_out = fds[1]; - ((t_cmd *) current->next->content)->fd_in = fds[0]; - } - exit_code = ft_own_cmd(data, content); - if (exit_code == -1) - { - content->executable = ft_get_executable_path(data, - content->executable); - if (content->executable != NULL) - ft_executor(data, content); - } - else if (exit_code == -2) - return (1); - if (ft_gen_exit_code_var(data)) - return (1); - if (content->fd_in > 2) - close(content->fd_in); - if (content->fd_out > 2) - close(content->fd_out); - current = current->next; - } - current = *cmds; - while (current != NULL) - { - content = current->content; - if (content->pid != -1) - waitpid(content->pid, &(data->exit_code), 0); - current = current->next; - } - return (0); -} diff --git a/execution/execution.c b/execution/execution.c new file mode 100644 index 0000000..9cc79fd --- /dev/null +++ b/execution/execution.c @@ -0,0 +1,115 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execution.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cchauvet executable, "pwd") == 0) + return_code = pwd(cmd->fd_out[0]); + else if (ft_strcmp(cmd->executable, "env") == 0) + return_code = print_env(data->env, cmd->fd_out[0]); + else if (ft_strcmp(cmd->executable, "export") == 0) + return_code = (ft_export(data->env,cmd->args + 1, cmd->fd_out[0])); + else if (ft_strcmp(cmd->executable, "cd") == 0) + return_code = (move_folder(cmd->args + 1, cmd->fd_out[0])); + else if (ft_strcmp(cmd->executable, "unset") == 0) + return_code = (unset(data->env, cmd->args, cmd->fd_out[0])); + else if (ft_strcmp(cmd->executable, "echo") == 0) + return_code = (echo(cmd->fd_out[0], cmd->args + 1)); + else if (ft_strcmp(cmd->executable, "exit") == 0) + { + return_code = ft_exit(cmd->args + 1); + if (return_code > 0) + { + data->exit_code = return_code; + return (-1); + } + } + return (return_code); +} + +static int ft_executor(t_cmd *cmd, char **env) +{ + if (cmd->fd_in[0] == -1 || cmd->fd_out[0] == -1 || cmd->executable == NULL) + return (0); + cmd->pid = fork(); + if (cmd->pid == -1) + return (1); + if (cmd->pid == 0) + { + dup2(cmd->fd_in[0], 0); + dup2(cmd->fd_out[0], 1); + if (cmd->fd_in[0] > 2) + close(cmd->fd_in[0]); + if (cmd->fd_out[0] > 2) + close(cmd->fd_out[0]); + execve(cmd->executable, cmd->args, env); + } + return (0); +} + +static int ft_cmd_executor(t_data *data, t_cmd *cmd) +{ + int exit_code; + char **env; + + if (cmd->own_cmd == 1) + { + exit_code = ft_own_cmd(data, cmd); + if (exit_code == -1) + return (1); + } + else + { + env = env_to_strs(data->env); + if (env == NULL) + return (1); + exit_code = ft_executor(cmd, env); + if (exit_code == -1) + return (1); + ft_freer_tab_ultimate(1, env); + } + if (ft_gen_exit_code(data)) + return (1); + return (0); +} + +int ft_cmds_executor(t_data *data) +{ + int fds[2]; + t_list *current; + t_cmd *content; + + current = *data->cmds; + while (current != NULL) + { + content = current->content; + if (current->next != NULL) + { + if (pipe(fds) == -1) + return (1); + ft_add_fd(content->fd_out, fds[1]); + ft_add_fd(((t_cmd *) (current->next->content))->fd_in, fds[0]); + } + if (ft_cmd_executor(data, content)) + return (1); + ft_closer(content->fd_in); + ft_closer(content->fd_out); + current = current->next; + } + ft_lstiter(*data->cmds, ft_cmd_waiter); + return (0); +} diff --git a/execution/execution.h b/execution/execution.h new file mode 100644 index 0000000..a322533 --- /dev/null +++ b/execution/execution.h @@ -0,0 +1,7 @@ +#ifndef EXECUTION_H +# define EXECUTION_H +# include "../data/data.h" + +int ft_cmds_executor(t_data *data); + +#endif diff --git a/execution/execution_private.h b/execution/execution_private.h new file mode 100644 index 0000000..148c986 --- /dev/null +++ b/execution/execution_private.h @@ -0,0 +1,11 @@ +#ifndef EXECUTION_PRIVATE_H +# define EXECUTION_PRIVATE_H +# include "../data/data.h" +# include "../libftx/libftx.h" +# include "../cmd/cmd.h" +# include "../env/env.h" +# include "../builtins/builtins.h" + +void ft_closer(int fds[2]); +void ft_add_fd(int fds[2], int fd); +#endif diff --git a/spacer.c b/format/format.c similarity index 95% rename from spacer.c rename to format/format.c index ebda46d..de82726 100644 --- a/spacer.c +++ b/format/format.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include "utils/utils.h" +#include "./format_private.h" static char *ft_spacer_after(const char *str) { @@ -97,7 +95,7 @@ static void ft_space_simplifier(char *str) } } -char *ft_normalizer(char *str) +char *ft_formater(char *str) { char *out; char *temp; diff --git a/format/format.h b/format/format.h new file mode 100644 index 0000000..fe79e26 --- /dev/null +++ b/format/format.h @@ -0,0 +1,6 @@ +#ifndef FORMAT_H +# define FORMAT_H + +char *ft_formater(char *str); + +#endif diff --git a/format/format_private.h b/format/format_private.h new file mode 100644 index 0000000..3622b59 --- /dev/null +++ b/format/format_private.h @@ -0,0 +1,6 @@ +#ifndef FORMAT_PRIVATE_H +# define FORMAT_PRIVATE_H +# include "../libftx/libftx.h" +# include "../utils/utils.h" + +#endif diff --git a/libftx/extra/ft_ultoa.o b/libftx/extra/ft_ultoa.o deleted file mode 100644 index 6786878..0000000 Binary files a/libftx/extra/ft_ultoa.o and /dev/null differ diff --git a/libftx/libft/libft.h b/libftx/libft/libft.h index 5cba6cf..be70f59 100644 --- a/libftx/libft/libft.h +++ b/libftx/libft/libft.h @@ -14,6 +14,7 @@ # define LIBFT_H # include # include +# include "./list.h" void *ft_cancel(void **tab, size_t len); int ft_atoi(const char *nptr); @@ -52,20 +53,4 @@ void ft_putstr_fd(char *s, int fd); void ft_putendl_fd(char *s, int fd); void ft_putnbr_fd(int n, int fd); -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; - -t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **lst, t_list *nouveau); -int ft_lstsize(t_list *lst); -t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **lst, t_list *nouveau); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); - #endif diff --git a/libftx/libft/list.h b/libftx/libft/list.h new file mode 100644 index 0000000..5ae8ff2 --- /dev/null +++ b/libftx/libft/list.h @@ -0,0 +1,20 @@ +#ifndef LIST_H +# define LIST_H + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *nouveau); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *nouveau); +void ft_lstdelone(t_list *lst, void (*del)(void *)); +void ft_lstclear(t_list **lst, void (*del)(void *)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +#endif diff --git a/libftx/libftx.h b/libftx/libftx.h index 2e24db4..1585365 100644 --- a/libftx/libftx.h +++ b/libftx/libftx.h @@ -15,6 +15,7 @@ # include # include # include +# include "./libft/libft.h" char *ft_ultoa_base(unsigned long long n, char *base); int ft_printf(const char *format, ...); @@ -39,57 +40,41 @@ void ft_swap(void *a, void *b); void ft_swap_int(int *a, int *b); void ft_swap_char(char *a, char *b); -void *ft_cancel(void **tab, size_t len); -int ft_atoi(const char *nptr); -void ft_bzero(void *s, size_t n); -void *ft_calloc(size_t nmemb, size_t size); -int ft_isalnum(int c); -int ft_isalpha(int c); -int ft_isascii(int c); -int ft_isdigit(int c); -int ft_isprint(int c); -void *ft_memchr(const void *s, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -void *ft_memcpy(void *dest, const void *src, size_t n); -void *ft_memmove(void *dest, const void *src, size_t n); -void *ft_memset(void *s, int c, size_t n); -char *ft_strchr(const char *s, int c); -char *ft_strdup(const char *s); -size_t ft_strlcat(char *dst, const char *src, size_t size); -size_t ft_strlcpy(char *dst, const char *src, size_t size); -size_t ft_strlen(const char *s); -int ft_strncmp(const char *s1, const char *s2, size_t n); -char *ft_strnstr(const char *big, const char *little, size_t len); -char *ft_strrchr(const char *s, int c); -int ft_tolower(int c); -int ft_toupper(int c); +/* void *ft_cancel(void **tab, size_t len); */ +/* int ft_atoi(const char *nptr); */ +/* void ft_bzero(void *s, size_t n); */ +/* void *ft_calloc(size_t nmemb, size_t size); */ +/* int ft_isalnum(int c); */ +/* int ft_isalpha(int c); */ +/* int ft_isascii(int c); */ +/* int ft_isdigit(int c); */ +/* int ft_isprint(int c); */ +/* void *ft_memchr(const void *s, int c, size_t n); */ +/* int ft_memcmp(const void *s1, const void *s2, size_t n); */ +/* void *ft_memcpy(void *dest, const void *src, size_t n); */ +/* void *ft_memmove(void *dest, const void *src, size_t n); */ +/* void *ft_memset(void *s, int c, size_t n); */ +/* char *ft_strchr(const char *s, int c); */ +/* char *ft_strdup(const char *s); */ +/* size_t ft_strlcat(char *dst, const char *src, size_t size); */ +/* size_t ft_strlcpy(char *dst, const char *src, size_t size); */ +/* size_t ft_strlen(const char *s); */ +/* int ft_strncmp(const char *s1, const char *s2, size_t n); */ +/* char *ft_strnstr(const char *big, const char *little, size_t len); */ +/* char *ft_strrchr(const char *s, int c); */ +/* int ft_tolower(int c); */ +/* int ft_toupper(int c); */ -char *ft_substr(char const *s, unsigned int start, size_t len); -char *ft_strjoin(char const *s1, char const *s2); -char *ft_strtrim(char const *s1, char const *set); -char **ft_split(char const *s, char c); -char *ft_itoa(int n); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -void ft_striteri(char *s, void (*f)(unsigned int, char*)); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); -void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); - -typedef struct s_list -{ - void *content; - struct s_list *next; -} t_list; - -t_list *ft_lstnew(void *content); -void ft_lstadd_front(t_list **lst, t_list *nouveau); -int ft_lstsize(t_list *lst); -t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **lst, t_list *nouveau); -void ft_lstdelone(t_list *lst, void (*del)(void *)); -void ft_lstclear(t_list **lst, void (*del)(void *)); -void ft_lstiter(t_list *lst, void (*f)(void *)); -t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); +/* char *ft_substr(char const *s, unsigned int start, size_t len); */ +/* char *ft_strjoin(char const *s1, char const *s2); */ +/* char *ft_strtrim(char const *s1, char const *set); */ +/* char **ft_split(char const *s, char c); */ +/* char *ft_itoa(int n); */ +/* char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); */ +/* void ft_striteri(char *s, void (*f)(unsigned int, char*)); */ +/* void ft_putchar_fd(char c, int fd); */ +/* void ft_putstr_fd(char *s, int fd); */ +/* void ft_putendl_fd(char *s, int fd); */ +/* void ft_putnbr_fd(int n, int fd); */ #endif diff --git a/main.c b/main.c index fb6bac8..a4e8002 100644 --- a/main.c +++ b/main.c @@ -10,9 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" #include "minishell.h" -#include static char *ft_get_user_input() { @@ -39,64 +37,23 @@ static char *ft_get_user_input() static int ft_minishell(t_data *data, char *line) { - t_list **cmds; char *line_clean; - int infile; - int outfile; - if (ft_syntatic_verif(data, line)) + if (ft_syntax_verif(data, line)) return (0); - line_clean = ft_normalizer(line); + line_clean = ft_formater(line); if (line_clean == NULL) return (0); - outfile = ft_outfile(data, line_clean); - if (outfile == -2) + if (ft_cmds_parser(data, line_clean)) { free(line_clean); return (0); } - infile = ft_infile(data, line_clean); - if (infile == -2) + if (ft_cmds_executor(data) == 1) { - if (outfile > 2) - close(outfile); - free(line_clean); - return (0); - } - if (ft_gen_exit_code_var(data)) - { - if (outfile > 2) - close(outfile); - if (infile > 2) - close(infile); free(line_clean); return (1); } - cmds = ft_parse_cmds(data, line_clean, infile, outfile); - if (cmds == NULL) - { - if (outfile > 2) - close(outfile); - if (infile > 2) - close(infile); - ft_lstclear(cmds, ft_cmddel); - free(cmds); - free(line_clean); - return (1); - } - if (ft_cmds_executor(data, cmds)) - { - if (outfile > 2) - close(outfile); - if (infile > 2) - close(infile); - ft_lstclear(cmds, ft_cmddel); - free(cmds); - free(line_clean); - return (1); - } - ft_lstclear(cmds, ft_cmddel); - free(cmds); free(line_clean); return (0); } @@ -122,12 +79,8 @@ void ft_ctrlc(int num) void ft_quit(int num) { (void) num; - if (*ft_get_heredoc()) - { - ft_printf("pb"); - } - else - ft_printf("bozoman"); + + ft_printf("%c%c %c%c", 0, 0, 0, 0); } int main(int ac, char **av, char **env) @@ -141,7 +94,11 @@ int main(int ac, char **av, char **env) signal(SIGQUIT, ft_quit); data.exit_code = 0; data.exit_code_str = NULL; - ft_gen_exit_code_var(&data); + ft_gen_exit_code(&data); + data.cmds = malloc(sizeof(t_cmd *)); + if (data.cmds == NULL) + return (1); + *data.cmds = NULL; data.env = init_env(env); if (data.env == NULL) return (1); @@ -155,7 +112,10 @@ int main(int ac, char **av, char **env) if (line == NULL) break ; } + free(data.exit_code_str); + ft_lstclear(data.cmds, ft_cmddel); + free(data.cmds); ft_lstclear(data.env, env_del); free(data.env); return (data.exit_code); diff --git a/minishell.h b/minishell.h index 111b044..8cc4505 100644 --- a/minishell.h +++ b/minishell.h @@ -12,94 +12,19 @@ #ifndef MINISHELL_H # define MINISHELL_H -# include "libftx/libftx.h" -# include "utils/utils.h" -# include -# include -# include -# include -# include -# include -# include -# include +# include "./env/env.h" +# include "./cmd/cmd.h" +# include "./parse/parse.h" +# include "./syntax/syntax.h" +# include "./execution/execution.h" +# include "./builtins/builtins.h" +# include "./format/format.h" +# include "./redirection/redirection.h" +# include "./libftx/libftx.h" +# include "./utils/utils.h" +# include "./data/data.h" # include # include -# define DEBUG 0 - -typedef struct s_data -{ - t_list **env; - int exit_code; - char *exit_code_str; -} t_data; - -int ft_syntatic_verif(t_data *data, const char *str); -int ft_file_is_readable(const char *path); -int ft_file_is_writable(const char *path); -int ft_file_is_appendable(const char *path); -char *ft_get_file_path(const char *infile); -int ft_infile(t_data *data, char *line); -int ft_outfile(t_data *data, char *line); -int ft_heredoc(t_data *data, char *stop); -int *ft_get_heredoc(); -size_t ft_seglen_quoted(const char *str, char c); -int ft_cmds_executor(t_data *data, t_list **cmds); -char **ft_split_quoted(const char *s, char c); -void ft_cmddel(void *content); -void env_del(void *content); -t_list **ft_parse_cmds(t_data *data, char *line, int infile, int outfile); -int ft_gen_exit_code_var(t_data *data); -int ft_cmd_filler(t_data *data, t_list *current, char **args); -char *ft_normalizer(char *str); -char *ft_env_filler(t_data *data, const char *str); -char **env_to_strs(t_list **head); - -/* Environnement */ -t_list **init_env(char **env); -char **env_to_strs(t_list **head); -int create_value_by_key(char *key, char *value, t_list **head); -int create_value_by_key_dup(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); -int delete_by_key(char *key, t_list **head); -int possible_key(char *key); - -/* Echo */ -int echo(int fd, char **strs); -/* PWD */ -int pwd(int fd); -char *get_pwd(int fd); -/* Env */ -int print_env(t_list **env, int fd); -/* Export */ -int export(t_list **env, char **args, int fd); -/* CD */ -int move_folder(char **args, int fd); -/* Unset */ -int unset(t_list **env, char **args, int fd); -/* Exit */ -int ft_exit(char **args); - -typedef struct s_cmd -{ - int fd_in; - int fd_out; - int pid; - char *executable; - char **args; -} t_cmd; - -typedef struct s_env -{ - char *key; - char *value; -} t_env; +# include #endif diff --git a/minishell_tester b/minishell_tester deleted file mode 160000 index 1c6111b..0000000 --- a/minishell_tester +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 1c6111b2fd281937d38ebfa7e8d87b38baef0802 diff --git a/parse/cmds_parse.c b/parse/cmds_parse.c new file mode 100644 index 0000000..476ed06 --- /dev/null +++ b/parse/cmds_parse.c @@ -0,0 +1,128 @@ +#include "./parse_private.h" + +static int ft_redirection_parse(t_data *data, char *cmd_str, t_cmd *cmd) +{ + int fd_in; + int fd_out; + + fd_in = ft_infile(data, cmd_str); + if (fd_in == -2) + return (1); + fd_out = ft_outfile(data, cmd_str); + if (fd_out == -2) + { + close(fd_in); + return (1); + } + cmd->fd_in[0] = fd_in; + cmd->fd_in[1] = -1; + cmd->fd_out[0] = fd_out; + cmd->fd_out[1] = -1; + return (0); +} + +int ft_args_parse(t_data *data, char *cmd_str, t_cmd *cmd) +{ + char **tab; + char *str; + size_t i; + + tab = ft_split_quoted(cmd_str, ' '); + if (tab == NULL) + return (1); + i = 0; + while (tab[i] == NULL) + { + str = ft_env_filler(data, tab[i]); + if (str == NULL) + { + ft_freer_tab_ultimate(1, tab); + return (1); + } + free(tab[i]); + tab[i] = str; + i++; + } + cmd->args = tab; + return (0); +} + +int ft_executable_parse(t_data *data, t_cmd *cmd) +{ + bool own; + char *path; + + path = cmd->args[0]; + own = 0; + if (ft_strcmp(cmd->args[0], "env") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "export") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "echo") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "unset") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "exit") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "pwd") == 0) + own = 1; + else if (ft_strcmp(cmd->args[0], "cmd") == 0) + own = 1; + if (own == 0) + path = ft_get_executable(data->env, cmd->args[0]); + cmd->own_cmd = own; + cmd->executable = path; + return (own); +} + +int ft_cmd_parser(t_data *data, char *cmd_str) +{ + t_cmd *cmd; + t_list *element; + + cmd = ft_calloc(sizeof(t_cmd), 1); + if (cmd == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (1); + } + if (ft_redirection_parse(data, cmd_str, cmd)) + return (1); + if (ft_args_parse(data, cmd_str, cmd)) + { + ft_cmddel(cmd); + return (1); + } + ft_executable_parse(data, cmd); + element = ft_lstnew(cmd); + if (element == NULL) + { + ft_cmddel(cmd); + return (1); + } + ft_lstadd_back(data->cmds, element); + return (0); +} + +int ft_cmds_parser(t_data *data, const char *line) +{ + char **tab; + size_t i; + + tab = ft_split_quoted(line, '|'); + if (tab == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (1); + } + i = 0; + while (tab[i] != NULL) + { + ft_cmd_parser(data, tab[i]); + i++; + } + ft_add_fd(((t_cmd *) (*data->cmds)->content)->fd_in, 0); + ft_add_fd(((t_cmd *) ((ft_lstlast(*data->cmds))->content))->fd_out, 1); + ft_freer_tab_ultimate(1, tab); + return (0); +} diff --git a/parse/parse.h b/parse/parse.h new file mode 100644 index 0000000..64d5e9b --- /dev/null +++ b/parse/parse.h @@ -0,0 +1,7 @@ +#ifndef PARSE_H +# define PARSE_H +# include "../data/data.h" + +int ft_cmds_parser(t_data *data, const char *line); + +#endif diff --git a/parse/parse_private.h b/parse/parse_private.h new file mode 100644 index 0000000..435294a --- /dev/null +++ b/parse/parse_private.h @@ -0,0 +1,9 @@ +#ifndef PARSE_PRIVATE_H +# define PARSE_PRIVATE_H +# include "../redirection/redirection.h" +# include "../libftx/libftx.h" +# include "../utils/utils.h" +# include "../env/env.h" +# include "../data/data.h" +# include "../cmd/cmd.h" +#endif diff --git a/file.c b/redirection/file.c similarity index 97% rename from file.c rename to redirection/file.c index d0bc35c..7789403 100644 --- a/file.c +++ b/redirection/file.c @@ -10,8 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" -#include +#include "./redirection_private.h" int ft_file_is_readable(const char *path) { diff --git a/heredoc.c b/redirection/heredoc.c similarity index 94% rename from heredoc.c rename to redirection/heredoc.c index 3c58229..2ea9b01 100644 --- a/heredoc.c +++ b/redirection/heredoc.c @@ -10,10 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include -#include +#include "./redirection_private.h" int *ft_get_heredoc() { diff --git a/infile.c b/redirection/infile.c similarity index 98% rename from infile.c rename to redirection/infile.c index a0a10dc..773bd45 100644 --- a/infile.c +++ b/redirection/infile.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "./redirection_private.h" static int ft_infile_is_valid(t_data *data, const char *line) { @@ -70,7 +70,7 @@ static int ft_get_infile(t_data *data, const char *line) ft_eprintf("minishell: malloc failed\n"); return (-2); } - fd = 0; + fd = -1; i = 0; while (tab[i + 1] != NULL) { diff --git a/outfile.c b/redirection/outfile.c similarity index 98% rename from outfile.c rename to redirection/outfile.c index 5bf8c83..adaf83f 100644 --- a/outfile.c +++ b/redirection/outfile.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "minishell.h" +#include "./redirection_private.h" static int ft_outfile_is_valid(t_data *data, const char *line) { @@ -69,7 +69,7 @@ static int ft_get_outfile(t_data *data, const char *line) ft_eprintf("minishell: malloc failed\n"); return (-2); } - fd = 1; + fd = -1; i = 0; while (tab[i + 1] != NULL) { diff --git a/redirection/redirection.h b/redirection/redirection.h new file mode 100644 index 0000000..baf4b34 --- /dev/null +++ b/redirection/redirection.h @@ -0,0 +1,8 @@ +#ifndef REDIRECTION_H +# define REDIRECTION_H +# include "../data/data.h" + +int ft_infile(t_data *data, char *line); +int ft_outfile(t_data *data, char *line); +int *ft_get_heredoc(); +#endif diff --git a/redirection/redirection_private.h b/redirection/redirection_private.h new file mode 100644 index 0000000..e22e87f --- /dev/null +++ b/redirection/redirection_private.h @@ -0,0 +1,16 @@ +#ifndef REDIRECTIONS_PRIVATE +# define REDIRECTIONS_PRIVATE +# include +# include +# include +# include +# include "../libftx/libftx.h" +# include "../data/data.h" +# include "../env/env.h" +# include "../utils/utils.h" + +int ft_file_is_readable(const char *path); +int ft_file_is_writable(const char *path); +int ft_heredoc(t_data *data, char *stop); + +#endif diff --git a/syntatics.c b/syntax/syntax.c similarity index 93% rename from syntatics.c rename to syntax/syntax.c index e12fbfd..07afe9f 100644 --- a/syntatics.c +++ b/syntax/syntax.c @@ -10,9 +10,8 @@ /* */ /* ************************************************************************** */ -#include "libftx/libftx.h" -#include "minishell.h" -#include "utils/utils.h" +#include "../libftx/libftx.h" +#include "../utils/utils.h" static int ft_quote_verif(const char *str) { @@ -91,7 +90,7 @@ static int ft_special_char_dub(const char *str) return (0); } -int ft_empty_verif(const char *str) +static int ft_empty_verif(const char *str) { size_t i; @@ -101,7 +100,7 @@ int ft_empty_verif(const char *str) return (str[i] == '\0'); } -int ft_syntatic_verif(t_data *data, const char *str) +int ft_syntax_verif(t_data *data, const char *str) { if (ft_quote_verif(str) || ft_empty_verif(str) diff --git a/syntax/syntax.h b/syntax/syntax.h new file mode 100644 index 0000000..9a87c39 --- /dev/null +++ b/syntax/syntax.h @@ -0,0 +1,7 @@ +#ifndef SYNTAX_H +# define SYNTAX_H +# include "../data/data.h" + +int ft_syntax_verif(t_data *data, const char *str); + +#endif diff --git a/utils/fd.c b/utils/fd.c new file mode 100644 index 0000000..55d23b6 --- /dev/null +++ b/utils/fd.c @@ -0,0 +1,17 @@ +#include "./utils.h" + +void ft_closer(int fds[2]) +{ + if (fds[0] > 2) + close(fds[0]); + if (fds[1] > 2) + close(fds[1]); +} + +void ft_add_fd(int fds[2], int fd) +{ + if (fds[0] == -1) + fds[0] = fd; + else + fds[1] = fd; +} diff --git a/utils/ft_get_executable.c b/utils/ft_get_executable.c new file mode 100644 index 0000000..4c99a6d --- /dev/null +++ b/utils/ft_get_executable.c @@ -0,0 +1,74 @@ +#include "./utils.h" +#include + +char *ft_get_executable_with_path(const char *name) +{ + char *path; + + if (access(name, F_OK) == 0) + { + ft_eprintf("minishell: %s bash: No such file or directery\n"); + return (NULL); + } + if (access(name, X_OK) != 0) + { + ft_eprintf("minishell: %s: permission denied\n", name); + return (NULL); + } + path = ft_strdup(name); + if (path == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (NULL); + } + return (path); +} + +char *ft_get_executable_without_path(t_list **env, const char *name) +{ + char **tab; + char *paths; + char *path; + size_t i; + + path = NULL; + paths = get_value_by_key("PATH", env); + if (paths == NULL) + path = ""; + tab = ft_split(paths, ':'); + if (tab == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + return (NULL); + } + i = 0; + while (tab[i] != NULL) + { + path = ft_strmerger(3, tab[i], "/", name); + if (path == NULL) + { + ft_eprintf("minishell: malloc failed\n"); + break; + } + if (access(path, X_OK)) + break; + free(path); + path = NULL; + i++; + } + ft_freer_tab_ultimate(1, tab); + if (path == NULL) + ft_eprintf("minishell: %s: command not found\n", name); + return (path); +} + +char *ft_get_executable(t_list **env, const char *name) +{ + char *path; + + if (name[0] == '.' || name[0] == '/') + path = ft_get_executable_with_path(name); + else + path = ft_get_executable_without_path(env, name); + return (path); +} diff --git a/utils/ft_getstr.o b/utils/ft_getstr.o deleted file mode 100644 index b76c43a..0000000 Binary files a/utils/ft_getstr.o and /dev/null differ diff --git a/utils/ft_printn.o b/utils/ft_printn.o deleted file mode 100644 index e48dceb..0000000 Binary files a/utils/ft_printn.o and /dev/null differ diff --git a/utils/utils.h b/utils/utils.h index 2a12d81..d54cce4 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -14,6 +14,8 @@ # define UTILS_H # include # include "../libftx/libftx.h" +# include "../data/data.h" +# include "../env/env.h" size_t ft_strncpy(char *dst, const char *src, size_t n); int ft_is_in_quote(const char *str, size_t n); @@ -26,5 +28,9 @@ char **ft_split_quoted(const char *s, char c); void ft_strshift(char *str, int shift); char *ft_quote_remover(char *str); int ft_atoi_check(const char *nptr); +char *ft_get_executable(t_list **env, const char *name); +void ft_closer(int fds[2]); +void ft_add_fd(int fds[2], int fd); + #endif