-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.c
More file actions
94 lines (86 loc) · 2.24 KB
/
init.c
File metadata and controls
94 lines (86 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ferenc <ferenc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 23:36:05 by elavrich #+# #+# */
/* Updated: 2025/07/24 16:54:22 by ferenc ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
void init_pipex(t_shell *px, t_shell *shell)
{
*px = *shell;
px->env_var = shell->env_var;
px->pipe_fd[0] = -1;
px->pipe_fd[1] = -1;
px->prev_fd[0] = -1;
px->prev_fd[1] = -1;
}
void init_cmd(t_cmd *cmd)
{
cmd->redir_in = -1;
cmd->redir_out = -1;
cmd->infile = NULL;
cmd->outfile = NULL;
cmd->args = NULL;
cmd->redir_error = 0;
}
void init_shell(t_shell *shell, char **envp)
{
shell->exit = 0;
shell->exit_stat = 0;
shell->infile = NULL;
shell->outfile = NULL;
shell->env_idx = 0;
shell->var_len = 0;
shell->env_var = copy_envp(envp, NULL);
if (!shell->env_var)
{
perror("Failed to copy envp");
exit(EXIT_FAILURE);
}
shell->pwd = set_pwd(shell);
}
void take_comm(t_token **tokens, t_shell *shell)
{
char *command;
while (!shell->exit)
{
command = readline("prompt> ");
if (!command)
{
close_free(tokens, shell);
write(1, "exit\n", 5);
exit(EXIT_SUCCESS);
}
if (ft_strlen(command) > 0)
add_history(command);
if (input(command, tokens, shell) < 0)
{
free(command);
deallocate(tokens);
continue ;
}
if (syntax_error(tokens, shell) == 0)
process_commands(tokens, shell);
deallocate(tokens);
free(command);
}
rl_clear_history();
}
char *set_pwd(t_shell *shell)
{
char *cwd;
(void)shell;
cwd = getcwd(NULL, 0);
if (cwd == NULL)
{
perror("getcwd");
free(cwd);
return (NULL);
}
return (cwd);
}