fix errors. Remaining errors:
- segfault when empty command - doesn't wait for the end of the command before prompting
This commit is contained in:
parent
6066d0529b
commit
7df3184578
7 changed files with 35 additions and 33 deletions
2
Makefile
2
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
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
BIN
minishell
Executable file
BIN
minishell
Executable file
Binary file not shown.
|
@ -6,7 +6,7 @@
|
|||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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();
|
||||
|
|
10
src/main.c
10
src/main.c
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
|
23
src/path.c
23
src/path.c
|
@ -6,14 +6,13 @@
|
|||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue