Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
executor_utils.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* executor_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/26 17:40:01 by hgenc #+# #+# */
9/* Updated: 2026/02/03 16:15:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <errno.h>
15#include <stdlib.h>
16#include <unistd.h>
17
18/**
19 * @brief Child process'i bekle ve exit status al
20 * EINTR: sinyal gelirse waitpid'i tekrar dene (zombie bırakma)
21 */
22int wait_for_child(pid_t pid)
23{
24 int status;
25 pid_t ret;
26
27 if (pid < 0)
28 return (1);
29 while (1)
30 {
31 ret = waitpid(pid, &status, 0);
32 if (ret > 0)
33 break ;
34 if (ret < 0 && errno != EINTR)
35 return (1);
36 }
37 if (WIFEXITED(status))
38 return (WEXITSTATUS(status));
39 if (WIFSIGNALED(status))
40 return (128 + WTERMSIG(status));
41 return (1);
42}
43
44/**
45 * @brief stdin ve stdout'u kaydet (builtin için)
46 * @param saved_fds 2 elemanlı array
47 * @return int başarı: 1, hata: 0
48 */
49int save_std_fds(int *saved_fds)
50{
51 if (!saved_fds)
52 return (0);
53 saved_fds[0] = dup(STDIN_FILENO);
54 if (saved_fds[0] < 0)
55 return (0);
56 saved_fds[1] = dup(STDOUT_FILENO);
57 if (saved_fds[1] < 0)
58 {
59 close(saved_fds[0]);
60 return (0);
61 }
62 return (1);
63}
64
65/**
66 * @brief Kaydedilen fd'leri geri yükle
67 * @param saved_fds 2 elemanlı array
68 * @return int başarı: 1, hata: 0
69 */
70int restore_std_fds(int *saved_fds)
71{
72 int ret;
73
74 ret = 1;
75 if (!saved_fds)
76 return (0);
77 if (saved_fds[0] >= 0)
78 {
79 if (dup2(saved_fds[0], STDIN_FILENO) < 0)
80 ret = 0;
81 close(saved_fds[0]);
82 }
83 if (saved_fds[1] >= 0)
84 {
85 if (dup2(saved_fds[1], STDOUT_FILENO) < 0)
86 ret = 0;
87 close(saved_fds[1]);
88 }
89 return (ret);
90}
int wait_for_child(pid_t pid)
Child process'i bekle ve exit status al EINTR: sinyal gelirse waitpid'i tekrar dene (zombie bırakma).
int save_std_fds(int *saved_fds)
stdin ve stdout'u kaydet (builtin için)
int restore_std_fds(int *saved_fds)
Kaydedilen fd'leri geri yükle.
Minishell ana header dosyası