Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
executor_pipe.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* executor_pipe.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/26 17:39:50 by hgenc #+# #+# */
9/* Updated: 2026/02/16 17:00:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <stdlib.h>
15#include <sys/wait.h>
16#include <unistd.h>
17
18/**
19 * @brief Pipe fd'lerini kapat
20 */
21void close_pipe(int *pipefd)
22{
23 if (!pipefd)
24 return ;
25 close(pipefd[0]);
26 close(pipefd[1]);
27}
28
29static int fork_process(pid_t *pid, int *pipefd)
30{
31 *pid = fork();
32 if (*pid < 0)
33 {
34 close_pipe(pipefd);
35 shell_error(NULL, NULL, "fork failed");
36 return (0);
37 }
38 return (1);
39}
40
41/**
42 * @brief Pipe node'unu çalıştır
43 * Her iki taraf da child process'te çalışır.
44 */
45int execute_pipe(t_shell *shell, t_ast *node)
46{
47 int pipefd[2];
48 pid_t left_pid;
49 pid_t right_pid;
50
51 if (pipe(pipefd) < 0)
52 {
53 shell_error(NULL, NULL, "pipe failed");
54 return (1);
55 }
56 if (!fork_process(&left_pid, pipefd))
57 return (1);
58 if (left_pid == 0)
59 pipe_left_child(shell, node, pipefd);
60 if (!fork_process(&right_pid, pipefd))
61 {
62 waitpid(left_pid, NULL, 0);
63 return (1);
64 }
65 if (right_pid == 0)
66 pipe_right_child(shell, node, pipefd);
67 close_pipe(pipefd);
68 return (wait_pipe(left_pid, right_pid));
69}
void shell_error(char *cmd, char *arg, char *msg)
int execute_pipe(t_shell *shell, t_ast *node)
Pipe node'unu çalıştır Her iki taraf da child process'te çalışır.
void close_pipe(int *pipefd)
Pipe fd'lerini kapat.
static int fork_process(pid_t *pid, int *pipefd)
void pipe_left_child(t_shell *shell, t_ast *node, int *pipefd)
Pipe'ın sol tarafını çalıştır.
int wait_pipe(pid_t left_pid, pid_t right_pid)
İki child'ı bekle, sağ tarafın exit status'unu döndür.
void pipe_right_child(t_shell *shell, t_ast *node, int *pipefd)
Pipe'ın sağ tarafını çalıştır.
Minishell ana header dosyası
struct s_ast t_ast
struct s_shell t_shell