Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
executor_redir.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* executor_redir.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/26 17:39:59 by hgenc #+# #+# */
9/* Updated: 2026/02/03 16:17:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <fcntl.h>
15#include <unistd.h>
16#include <string.h>
17#include <errno.h>
18
19/**
20 * @brief Redirection dosyasını açar
21 * @param redir Redirection yapısı
22 * @param flags open() flag'leri
23 * @param mode Dosya oluşturma izni (O_CREAT varsa)
24 * @return int başarı: fd, hata: -1
25 */
26static int open_redir_file(t_redir *redir, int flags, int mode)
27{
28 int fd;
29
30 if (!redir || !redir->file)
31 return (-1);
32 fd = open(redir->file, flags, mode);
33 if (fd < 0)
34 {
35 shell_error(redir->file, NULL, strerror(errno));
36 return (-1);
37 }
38 return (fd);
39}
40
41/**
42 * @brief Tek bir redirection uygula
43 * @param shell Shell structure
44 * @param redir Redirection yapısı
45 * @return int başarı: 1, hata: 0
46 */
47int apply_redir(t_shell *shell, t_redir *redir)
48{
49 int fd;
50
51 if (!redir)
52 return (0);
53 if (redir->type == REDIR_IN)
54 fd = open_redir_file(redir, O_RDONLY, 0);
55 else if (redir->type == REDIR_OUT)
56 fd = open_redir_file(redir, O_WRONLY | O_CREAT | O_TRUNC, 0644);
57 else if (redir->type == REDIR_APPEND)
58 fd = open_redir_file(redir, O_WRONLY | O_CREAT | O_APPEND, 0644);
59 else if (redir->type == REDIR_HEREDOC)
60 fd = redir_heredoc(shell, redir);
61 else
62 return (0);
63 if (fd < 0)
64 return (0);
65 if (redir->type == REDIR_IN || redir->type == REDIR_HEREDOC)
66 {
67 if (dup2(fd, STDIN_FILENO) < 0)
68 return (close(fd), 0);
69 }
70 else
71 {
72 if (dup2(fd, STDOUT_FILENO) < 0)
73 return (close(fd), 0);
74 }
75 close(fd);
76 return (1);
77}
78
79/**
80 * @brief Tüm redirectionları uygula
81 * @param shell Shell structure
82 * @param cmd Command yapısı
83 * @return int başarı: 1, hata: 0
84 */
85int setup_redirs(t_shell *shell, t_cmd *cmd)
86{
87 t_list *current;
88 t_redir *redir;
89
90 if (!cmd)
91 return (1);
92 current = cmd->redirections;
93 while (current)
94 {
95 redir = (t_redir *)current->content;
96 if (!apply_redir(shell, redir))
97 return (0);
98 current = current->next;
99 }
100 return (1);
101}
void shell_error(char *cmd, char *arg, char *msg)
int redir_heredoc(t_shell *shell, t_redir *redir)
Heredoc redirection uygula (fork tabanlı).
static int open_redir_file(t_redir *redir, int flags, int mode)
Redirection dosyasını açar.
int setup_redirs(t_shell *shell, t_cmd *cmd)
Tüm redirectionları uygula.
int apply_redir(t_shell *shell, t_redir *redir)
Tek bir redirection uygula.
Minishell ana header dosyası
struct s_redir t_redir
struct s_cmd t_cmd
@ REDIR_IN
Definition minishell.h:116
@ REDIR_HEREDOC
Definition minishell.h:118
@ REDIR_OUT
Definition minishell.h:117
@ REDIR_APPEND
Definition minishell.h:119
struct s_shell t_shell
t_list * redirections
Definition minishell.h:131
t_redir_type type
Definition minishell.h:123
char * file
Definition minishell.h:124