-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_shell.c
95 lines (86 loc) · 2.48 KB
/
clean_shell.c
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
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* clean_shell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aoizel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 09:13:19 by aoizel #+# #+# */
/* Updated: 2024/01/23 11:17:27 by aoizel ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void clean_wordlst(t_wordlst **wordlst)
{
t_wordlst *word_elem;
t_wordlst *next_word_elem;
word_elem = *wordlst;
while (word_elem)
{
free(word_elem->word);
next_word_elem = word_elem->next;
free(word_elem);
word_elem = next_word_elem;
}
*wordlst = NULL;
}
int clean_processlst(t_processlst **processlst, t_shell *shell)
{
int i;
t_processlst *process;
t_processlst *next_process;
int status;
process = *processlst;
status = shell->last_status;
while (process)
{
i = 0;
status = process->status;
if (g_last_sig == SIGINT && status == 130)
ft_putendl_fd("", g_last_sig = 0);
while (process->argv && process->argv[i])
free(process->argv[i++]);
free(process->argv);
if (process->fd_in > 0)
close(process->fd_in);
if (process->fd_out > 1)
close(process->fd_out);
next_process = process->next;
free(process);
process = next_process;
}
return (status);
}
int clean_shell(t_shell *shell)
{
char *pwd;
char *tmp_str;
clean_wordlst(&shell->wordlst);
shell->last_status = clean_processlst(&shell->processlst, shell);
shell->processlst = NULL;
if (shell->last_status == 131)
ft_putstr_fd("Quit (core dumped)\n", 2);
free_envarray(shell->envarray);
shell->envarray = NULL;
free(shell->line);
pwd = getcwd(NULL, 0);
if (!pwd)
return (0);
tmp_str = ft_strjoin(pwd, "$ ");
if (!tmp_str)
return (perror("minishell"), free(pwd), -1);
free(shell->prompt);
shell->prompt = tmp_str;
free(pwd);
return (0);
}
void free_envarray(char **envarray)
{
int i;
i = 0;
if (!envarray)
return ;
while (envarray[i])
free(envarray[i++]);
free(envarray);
}