forked from mohamedamine456/MiniShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdwr_history.c
94 lines (84 loc) · 2.24 KB
/
rdwr_history.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rdwr_history.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mlachheb <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/01 19:35:24 by mlachheb #+# #+# */
/* Updated: 2021/06/29 13:10:14 by mlachheb ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int open_history(void)
{
int fd;
fd = open(".minishell_history",
O_RDWR | O_CREAT | O_APPEND, S_IWUSR | S_IRUSR);
return (fd);
}
void write_hist(t_general_data *data, char *line)
{
if (ft_strcmp(line, ""))
{
if (data->fd != -1)
{
write(data->fd, line, ft_strlen(line));
write(data->fd, "\n", 1);
}
free(data->hists->line_chngd);
data->hists->line_chngd = NULL;
data->hists = last_hist(data->hists);
free(data->hists->line_orig);
data->hists->line_orig = ft_strdup(line);
if (data->hists->line_chngd != NULL)
{
free(data->hists->line_chngd);
data->hists->line_chngd = NULL;
}
free(line);
line = NULL;
}
else
free(line);
}
void fill_hist(t_history **hists, char *line)
{
t_history *new;
new = new_hist();
new->line_orig = ft_strdup(line);
add_back_hist(hists, new);
free(line);
}
t_history *read_hists(int fd)
{
t_history *hists;
char *line;
hists = NULL;
if (fd != -1)
{
while (get_next_line(fd, &line) > 0)
{
fill_hist(&hists, line);
line = NULL;
}
if (ft_strcmp(line, ""))
{
fill_hist(&hists, line);
line = NULL;
}
else
free(line);
}
return (hists);
}
t_general_data init_general_data(void)
{
t_general_data data;
data.retv = 0;
data.fd = open_history();
data.hists = read_hists(data.fd);
data.hists = last_hist(data.hists);
data.command_line = NULL;
return (data);
}