Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion inc/execute_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 21:39:18 by dayano #+# #+# */
/* Updated: 2025/04/25 20:46:25 by dayano ### ########.fr */
/* Updated: 2025/04/28 13:56:33 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,8 @@

# define PATH_PREFIX_LEN 5
# define EQUAL_LEN 1

# define CMD_FAILED_EXIT_STATUS 126
# define CMD_NOT_FOUND_EXIT_STATUS 127

void execute_cmd(t_cmd *cmd, t_minish *minish);
Expand All @@ -25,4 +27,6 @@ char *get_path_line(char **envp);
void free_str_array(char **str);
void print_cmd_not_found(t_cmd *cmd);

void execute_cmd(t_cmd *cmd, t_minish *minish);

#endif
4 changes: 2 additions & 2 deletions inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */
/* Updated: 2025/04/27 16:28:37 by ttsubo ### ########.fr */
/* Updated: 2025/04/28 12:00:56 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 2 additions & 2 deletions inc/pipeline_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 14:32:27 by dayano #+# #+# */
/* Updated: 2025/04/24 15:02:17 by dayano ### ########.fr */
/* Updated: 2025/04/28 14:11:40 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,7 +19,7 @@
// pipeline_helper.c
void redirect_stdout(t_cmd *cmd);
bool is_redirect(t_cmd *cmd);
bool is_head(t_cmd *cmd);
bool is_head(t_cmd *cmd, t_cmd *cmd_head);
bool is_tail(t_cmd *cmd);
bool is_builtin(t_cmd *cmd);

Expand Down
106 changes: 53 additions & 53 deletions src/invoke_cmd/exec_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 13:14:15 by dayano #+# #+# */
/* Updated: 2025/04/25 21:16:09 by dayano ### ########.fr */
/* Updated: 2025/04/28 14:12:13 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,62 +21,14 @@ void initialize_fds(int fds1[2], int fds2[2])
}

/**
* @brief // execvp(cmd->argv[0], cmd->argv);
// fprintf(stderr, "%s: command not found: %s\n", program_name,
// cmd->argv[0]);
* @brief old execute_cmd function
* execvp(cmd->argv[0], cmd->argv);
fprintf(stderr, "%s: command not found: %s\n", program_name,
cmd->argv[0]);
* @brief
* @param cmd_head
* @param minish
*/
// note : process in while loop of inside
// fds1[0] = fds2[0];
// fds1[1] = fds2[1];
// if (!is_tail(cmd))
// {
// if (pipe(fds2) < 0)
// {
// perror("pipe");
// exit(3);
// }
// }
// cmd->pid = fork();
// if (cmd->pid < 0)
// {
// perror("fork");
// exit(3);
// }
// if (cmd->pid > 0)
// {
// if (fds1[0] != -1)
// close(fds1[0]);
// if (fds1[1] != -1)
// close(fds1[1]);
// continue ;
// }
// if (!is_head(cmd))
// {
// close(STDIN_FILENO);
// dup2(fds1[0], STDIN_FILENO);
// close(fds1[0]);
// close(fds1[1]);
// }
// if (!is_tail(cmd))
// {
// close(fds2[0]);
// close(STDOUT_FILENO);
// dup2(fds2[1], STDOUT_FILENO);
// close(fds2[1]);
// }
// if ((cmd->next != NULL) && is_redirect(cmd->next))
// {
// redirect_stdout(cmd->next);
// }
// if (!is_builtin(cmd))
// {
// execute_cmd(cmd, minish);
// exit(EXIT_FAILURE);
// }
// cmd = cmd->next;
void exec_pipeline(t_cmd *cmd_head, t_minish *minish)
{
t_cmd *cmd;
Expand All @@ -88,6 +40,54 @@ void exec_pipeline(t_cmd *cmd_head, t_minish *minish)
initialize_fds(fds1, fds2);
while (cmd && !is_redirect(cmd))
{
fds1[0] = fds2[0];
fds1[1] = fds2[1];
if (!is_tail(cmd))
{
if (pipe(fds2) < 0)
{
perror("pipe");
exit(3);
}
}
cmd->pid = fork();
if (cmd->pid < 0)
{
perror("fork");
exit(3);
}
if (cmd->pid > 0)
{
if (fds1[0] != -1)
close(fds1[0]);
if (fds1[1] != -1)
close(fds1[1]);
continue ;
}
if (!is_head(cmd, cmd_head))
{
close(STDIN_FILENO);
dup2(fds1[0], STDIN_FILENO);
close(fds1[0]);
close(fds1[1]);
}
if (!is_tail(cmd))
{
close(fds2[0]);
close(STDOUT_FILENO);
dup2(fds2[1], STDOUT_FILENO);
close(fds2[1]);
}
if ((cmd->next != NULL) && is_redirect(cmd->next))
{
redirect_stdout(cmd->next);
}
if (!is_builtin(cmd))
{
execute_cmd(cmd, minish);
exit(EXIT_FAILURE);
}
cmd = cmd->next;
}
}

Expand Down
15 changes: 6 additions & 9 deletions src/invoke_cmd/execute_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 21:25:41 by dayano #+# #+# */
/* Updated: 2025/04/25 21:17:14 by dayano ### ########.fr */
/* Updated: 2025/04/28 13:56:43 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,6 +37,8 @@ char *get_cmd_path(char *cmd, char **envp)
char **path_set;
char *full_path;

if (!cmd && ft_strlen(cmd) == 0)
return (NULL);
if (access(cmd, X_OK) == 0)
return (cmd);
path_line = get_path_line(envp);
Expand Down Expand Up @@ -69,19 +71,14 @@ void execute_cmd(t_cmd *cmd, t_minish *minish)
print_cmd_not_found(cmd);
return ;
}
if (!cmd->argv[0] || ft_strlen(cmd->argv[0]) == 0)
{
ft_putstr_fd("Command '' not found\n", STDERR_FILENO);
exit(127);
}
if (!is_pathname(cmd->argv[0], envp, &fullpath))
{
ft_putstr_fd("Command '' not found\n", STDERR_FILENO);
exit(127);
print_cmd_not_found(cmd);
return ;
}
if (execve(fullpath, cmd->argv, envp) < 0)
{
free(fullpath);
perror("execve");
cmd->status = CMD_FAILED_EXIT_STATUS;
}
}
14 changes: 8 additions & 6 deletions src/invoke_cmd/pipeline_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 14:29:29 by dayano #+# #+# */
/* Updated: 2025/04/24 14:57:00 by dayano ### ########.fr */
/* Updated: 2025/04/28 14:12:08 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,16 +24,18 @@ bool is_redirect(t_cmd *cmd)
return (false);
}

bool is_head(t_cmd *cmd)
bool is_head(t_cmd *cmd, t_cmd *cmd_head)
{
(void)cmd;
return (true);
if (cmd == cmd_head)
return (true);
return (false);
}

bool is_tail(t_cmd *cmd)
{
(void)cmd;
return (true);
if (cmd->next == NULL || is_redirect(cmd->next))
return (true);
return (false);
}

bool is_builtin(t_cmd *cmd)
Expand Down
64 changes: 0 additions & 64 deletions src/invoke_cmd/pipeline_helper_2.c

This file was deleted.

6 changes: 3 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:11 by ttsubo #+# #+# */
/* Updated: 2025/04/28 11:55:41 by ttsubo ### ########.fr */
/* Updated: 2025/04/28 12:01:09 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -45,7 +45,7 @@ static bool prompt(char *program_name, t_minish *minish, int *status)
char **tokens;
t_cmd **cmds;

(void) status;
(void)status;
line = readline("minish>");
if (!line)
return (false);
Expand Down
Loading