fix errors. Remaining errors:

- segfault when empty command
- doesn't wait for the end of the command before prompting
This commit is contained in:
mcolonna 2024-04-25 18:15:37 +02:00
parent 6066d0529b
commit 7df3184578
7 changed files with 35 additions and 33 deletions

View file

@ -5,7 +5,7 @@ SRCS = src/
# include directory # include directory
INCLUDES = include/ libtf/ libft/ INCLUDES = include/ libtf/ libft/
# .c files in src/ without the extension # .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' # directories to 'make'
LIBRARIES = libtf libft LIBRARIES = libtf libft
# .a files to include # .a files to include

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 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. const char *program; // path of the program to call.
int argc; // number of arguments given 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) // (argc and argv must include the program name)
} t_call; } t_call;
@ -58,7 +58,7 @@ t_command parse_command(const t_memclass mc, const char *command);
///// EXECUTE COMMAND ///// ///// EXECUTE COMMAND /////
// Execute the command given. Return the exit status. // 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 ///// ///// ERROR /////
@ -73,6 +73,11 @@ void fatal_error(const char *msg);
// Get the PATH values. // Get the PATH values.
// Return a list of strings ended by NULL. // 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 #endif

BIN
minishell Executable file

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 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 works with only one call (no pipe).
// TODO is t_call.argv ended by NULL? // 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) if (command.input_fd != 0)
dup2(command.input_fd, 0); dup2(command.input_fd, 0);
if (command.output_fd != 1) if (command.output_fd != 1)
dup2(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). // 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); char **const path = get_path(envp);
pid_t pid; pid_t pid;
int program_path; const char* program_path;
program_path = search_path(path); program_path = search_path(mc, (const char **)path, command.calls[0].program);
if (!program_path) if (!program_path)
return (127); return (127);
pid = fork(); pid = fork();

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:33:45 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"); 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; t_memclass mc;
const char *command_str; const char *command_str;
t_command command; t_command command;
@ -65,10 +65,8 @@ int main(const int argc, const char *argv[], const char *envp[])
command = parse_command(mc, command_str); command = parse_command(mc, command_str);
if (command.error) if (command.error)
continue ; continue ;
/* errorstatus = execute_command(mc, command, envp);
errorstatus = execute_command(command);
mem_freeall(mc); mem_freeall(mc);
*/
} }
return (errorstatus); return (errorstatus);
} }

View file

@ -6,7 +6,7 @@
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */ /* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:47:40 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 = mem_alloc(fatal_error, args->mc, sizeof(t_call));
r->program = (char *)list_get(parse_error, &arguments, 0); r->program = (char *)list_get(parse_error, &arguments, 0);
r->argc = list_getsize(&arguments); r->argc = list_getsize(&arguments);
r->argv = (const char *const *)list_convert( r->argv = (char *const *)list_convert(
fatal_error, args->mc, &arguments); fatal_error, args->mc, &arguments);
list_add(fatal_error, &args->calls, (t_call *)r); list_add(fatal_error, &args->calls, (t_call *)r);
args->got_first_call = true; args->got_first_call = true;

View file

@ -6,14 +6,13 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */ /* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:07:26 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" #include "include.h"
// Get the path values from envp. char **get_path(char *const envp[])
char **get_path(const char **envp)
{ {
int i; int i;
char **re; char **re;
@ -25,26 +24,26 @@ char **get_path(const char **envp)
path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5); path = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5);
re = ft_split(path, ':'); re = ft_split(path, ':');
free(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? // TODO does it return the good path if there is several possibilities?
const char *search_path( const char *search_path(
const t_memclass mc, const char **path, const char *prog) 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, "/", prog);
const char *prog2 = str_join(fatal_error, mc_in, "/", prog); int i;
int i; const char *r;
i = 0; i = 0;
while (path[i] != NULL) while (path[i] != NULL)
{ {
if (access(str_join(fatal_error, mc_in, path[i], prog2), X_OK) >= 0) r = str_join(fatal_error, mc, path[i], prog2);
return (i); if (access(r, X_OK) >= 0)
return (r);
mem_free((void *)r);
i++; i++;
} }
mem_freeall(mc_in); mem_free((void *)prog2);
return (NULL); return (NULL);
} }