change execute_call() and fork_exec() prototypes
This commit is contained in:
parent
7df3184578
commit
d90fc62be5
5 changed files with 31 additions and 23 deletions
|
@ -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 18:04:46 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/26 12:27:17 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -58,7 +58,8 @@ 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_memclass mc, t_command command, char *const envp[]);
|
int execute_command(t_memclass mc, t_command command,
|
||||||
|
char *const envp[]);
|
||||||
|
|
||||||
///// ERROR /////
|
///// ERROR /////
|
||||||
|
|
||||||
|
@ -78,6 +79,6 @@ char **get_path(char *const envp[]);
|
||||||
// Search the program in PATH.
|
// Search the program in PATH.
|
||||||
// Returns the path of the program to use, or NULL if there is none.
|
// Returns the path of the program to use, or NULL if there is none.
|
||||||
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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
BIN
minishell
BIN
minishell
Binary file not shown.
|
@ -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 18:14:03 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/26 12:26:56 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -28,30 +28,37 @@ 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, char *const envp[])
|
static void fork_exec(const char *path, t_call call, const int inout[2],
|
||||||
|
char *const envp[])
|
||||||
{
|
{
|
||||||
if (command.input_fd != 0)
|
dup2(inout[0], 0);
|
||||||
dup2(command.input_fd, 0);
|
dup2(inout[1], 1);
|
||||||
if (command.output_fd != 1)
|
execve(path, call.argv, envp);
|
||||||
dup2(command.output_fd, 1);
|
|
||||||
execve(path, command.calls[0].argv, envp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO works with only one call (no pipe).
|
static int execute_call(t_memclass mc, t_call call, const int inout[2],
|
||||||
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
char *const envp[])
|
||||||
{
|
{
|
||||||
char **const path = get_path(envp);
|
char **const path = get_path(envp);
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
const char* program_path;
|
const char *program_path;
|
||||||
|
|
||||||
program_path = search_path(mc, (const char **)path, command.calls[0].program);
|
program_path = search_path(mc, (const char **)path, call.program);
|
||||||
if (!program_path)
|
if (!program_path)
|
||||||
return (127);
|
return (127);
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0)
|
if (pid < 0)
|
||||||
return (1);
|
return (1);
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
fork_exec(program_path, command, envp);
|
fork_exec(program_path, call, inout, envp);
|
||||||
free_tab(path);
|
free_tab(path);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO works with only one call (no pipe).
|
||||||
|
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
||||||
|
{
|
||||||
|
const int inout[] = {0, 1};
|
||||||
|
|
||||||
|
return (execute_call(mc, command.calls[0], inout, envp));
|
||||||
|
}
|
||||||
|
|
10
src/main.c
10
src/main.c
|
@ -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 18:14:53 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/26 12:24:58 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -46,10 +46,10 @@ static void print_hi(void)
|
||||||
int main(const int argc, const char *argv[], char *const envp[])
|
int main(const int argc, const char *argv[], char *const envp[])
|
||||||
{
|
{
|
||||||
char **const 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;
|
||||||
int errorstatus;
|
int errorstatus;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* 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 18:03:49 by mcolonna ### ########.fr */
|
/* Updated: 2024/04/26 12:24:41 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -15,8 +15,8 @@
|
||||||
char **get_path(char *const envp[])
|
char **get_path(char *const envp[])
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char **re;
|
char **re;
|
||||||
char *path;
|
char *path;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
|
while (ft_strncmp(envp[i], "PATH=", 5) != 0)
|
||||||
|
|
Loading…
Add table
Reference in a new issue