diff --git a/Makefile b/Makefile index 52864a7..794a0f3 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ SRCS = src/ # include directory INCLUDES = include/ libtf/ libft/ # .c files in src/ without the extension -CODE = main ask_command error path parse_command +CODE = main ask_command error path parse_command exec_command # directories to 'make' LIBRARIES = libtf libft # .a files to include diff --git a/include/include.h b/include/include.h index 9205886..92e9074 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */ -/* Updated: 2024/04/25 17:47:02 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 18:04:46 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ typedef struct s_call { const char *program; // path of the program to call. int argc; // number of arguments given - const char *const *argv; // arguments given + char *const *argv; // arguments given // (argc and argv must include the program name) } t_call; @@ -58,7 +58,7 @@ t_command parse_command(const t_memclass mc, const char *command); ///// EXECUTE COMMAND ///// // Execute the command given. Return the exit status. -int execute_command(t_command command); +int execute_command(t_memclass mc, t_command command, char *const envp[]); ///// ERROR ///// @@ -73,6 +73,11 @@ void fatal_error(const char *msg); // Get the PATH values. // Return a list of strings ended by NULL. -char **get_path(const char **envp); +char **get_path(char *const envp[]); + +// Search the program in PATH. +// Returns the path of the program to use, or NULL if there is none. +const char *search_path( + const t_memclass mc, const char **path, const char *prog); #endif diff --git a/minishell b/minishell new file mode 100755 index 0000000..5638437 Binary files /dev/null and b/minishell differ diff --git a/src/exec_command.c b/src/exec_command.c index f7d609d..a9b46c7 100644 --- a/src/exec_command.c +++ b/src/exec_command.c @@ -6,7 +6,7 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */ -/* Updated: 2024/04/25 17:23:34 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 18:14:03 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,23 +28,23 @@ static void free_tab(char **tab) // TODO works with only one call (no pipe). // TODO is t_call.argv ended by NULL? -static void fork_exec(const char *path, t_command command, const char **envp) +static void fork_exec(const char *path, t_command command, char *const envp[]) { if (command.input_fd != 0) dup2(command.input_fd, 0); if (command.output_fd != 1) dup2(command.output_fd, 1); - execve(path, command.calls[0]->argv, envp); + execve(path, command.calls[0].argv, envp); } // TODO works with only one call (no pipe). -int execute_command(t_command command, const char **envp) +int execute_command(t_memclass mc, t_command command, char *const envp[]) { - const char **path = get_path(envp); - pid_t pid; - int program_path; + char **const path = get_path(envp); + pid_t pid; + const char* program_path; - program_path = search_path(path); + program_path = search_path(mc, (const char **)path, command.calls[0].program); if (!program_path) return (127); pid = fork(); diff --git a/src/main.c b/src/main.c index 7101c0c..d8f80a7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */ -/* Updated: 2024/04/25 16:54:13 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 18:14:53 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,9 +43,9 @@ static void print_hi(void) printf("|_____________________________________________|\n\n"); } -int main(const int argc, const char *argv[], const char *envp[]) +int main(const int argc, const char *argv[], char *const envp[]) { - const char **path = get_path(envp); + char **const path = get_path(envp); t_memclass mc; const char *command_str; t_command command; @@ -65,10 +65,8 @@ int main(const int argc, const char *argv[], const char *envp[]) command = parse_command(mc, command_str); if (command.error) continue ; - /* - errorstatus = execute_command(command); + errorstatus = execute_command(mc, command, envp); mem_freeall(mc); - */ } return (errorstatus); } diff --git a/src/parse_command.c b/src/parse_command.c index 0610053..0370d36 100644 --- a/src/parse_command.c +++ b/src/parse_command.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/24 13:47:40 by mcolonna #+# #+# */ -/* Updated: 2024/04/25 16:51:22 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 17:58:54 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,7 +78,7 @@ static void get_call(t_parsing_args *args, const char *stop_charset) r = mem_alloc(fatal_error, args->mc, sizeof(t_call)); r->program = (char *)list_get(parse_error, &arguments, 0); r->argc = list_getsize(&arguments); - r->argv = (const char *const *)list_convert( + r->argv = (char *const *)list_convert( fatal_error, args->mc, &arguments); list_add(fatal_error, &args->calls, (t_call *)r); args->got_first_call = true; diff --git a/src/path.c b/src/path.c index 37ea53e..fde3885 100644 --- a/src/path.c +++ b/src/path.c @@ -6,14 +6,13 @@ /* By: jschaft +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/11/16 12:07:26 by jschaft #+# #+# */ -/* Updated: 2024/04/25 17:30:05 by mcolonna ### ########.fr */ +/* Updated: 2024/04/25 18:03:49 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ #include "include.h" -// Get the path values from envp. -char **get_path(const char **envp) +char **get_path(char *const envp[]) { int i; char **re; @@ -25,26 +24,26 @@ char **get_path(const char **envp) path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5); re = ft_split(path, ':'); free(path); - return (char **re); + return (re); } -// Search the program in PATH. -// Returns the path of the program to use, or NULL if there is none. // TODO does it return the good path if there is several possibilities? const char *search_path( const t_memclass mc, const char **path, const char *prog) { - const t_memclass mc_in = mem_newclass(fatal_error); - const char *prog2 = str_join(fatal_error, mc_in, "/", prog); - int i; + const char *prog2 = str_join(fatal_error, mc, "/", prog); + int i; + const char *r; i = 0; while (path[i] != NULL) { - if (access(str_join(fatal_error, mc_in, path[i], prog2), X_OK) >= 0) - return (i); + r = str_join(fatal_error, mc, path[i], prog2); + if (access(r, X_OK) >= 0) + return (r); + mem_free((void *)r); i++; } - mem_freeall(mc_in); + mem_free((void *)prog2); return (NULL); }