-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss-manage_history.c
143 lines (129 loc) · 2.78 KB
/
ss-manage_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "shell.h"
/**
* read_hist - gets the history file
* @info: parameter struct
*
* Return: allocated string containg history file
*/
char *read_hist(info_s *info)
{
char *buf, *dir;
dir = _getenv(info, "HOME=");
if (!dir)
return (NULL);
buf = malloc(sizeof(char) * (_strlen(dir) + _strlen(HIST_FILE_NAME) + 2));
if (!buf)
return (NULL);
buf[0] = 0;
_strcpy(buf, dir);
_strcat(buf, "/");
_strcat(buf, HIST_FILE_NAME);
return (buf);
}
/**
* create_history - creates a file, or appends to an existing file
* @info: the parameter struct
*
* Return: 1 on success, else -1
*/
int create_history(info_s *info)
{
ssize_t fd;
char *filename = read_hist(info);
list_s *node = NULL;
if (!filename)
return (-1);
fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0644);
free(filename);
if (fd == -1)
return (-1);
for (node = info->history; node; node = node->next)
{
write_chars(node->str, fd);
write_char('\n', fd);
}
write_char(NEG_ONE, fd);
close(fd);
return (1);
}
/**
* read_history - reads history from file
* @info: the parameter struct
*
* Return: hist_lines on success, 0 otherwise
*/
int read_history(info_s *info)
{
int i, last = 0, linecount = 0;
ssize_t fd, rdlen, fsize = 0;
struct stat st;
char *buf = NULL, *filename = read_hist(info);
if (!filename)
return (0);
fd = open(filename, O_RDONLY);
free(filename);
if (fd == -1)
return (0);
if (!fstat(fd, &st))
fsize = st.st_size;
if (fsize < 2)
return (0);
buf = malloc(sizeof(char) * (fsize + 1));
if (!buf)
return (0);
rdlen = read(fd, buf, fsize);
buf[fsize] = 0;
if (rdlen <= 0)
return (free(buf), 0);
close(fd);
for (i = 0; i < fsize; i++)
if (buf[i] == '\n')
{
buf[i] = 0;
update_history(info, buf + last, linecount++);
last = i + 1;
}
if (last != i)
update_history(info, buf + last, linecount++);
free(buf);
info->hist_lines = linecount;
while (info->hist_lines-- >= HIST_SIZE_MAX)
delete_node_at_index(&(info->history), 0);
renumber_history(info);
return (info->hist_lines);
}
/**
* update_history - adds entry to a history linked list
* @info: contains simulated arguments for a function pointer,
* @buf: buffer
* @linecount: the history linecount, hist_lines
*
* Return: Always 0
*/
int update_history(info_s *info, char *buf, int linecount)
{
list_s *node = NULL;
if (info->history)
node = info->history;
add_node_end(&node, buf, linecount);
if (!info->history)
info->history = node;
return (0);
}
/**
* renumber_history - renumbers the history linked list after changes
* @info: contains simulated arguments for a function pointer,
*
* Return: the new hist_lines
*/
int renumber_history(info_s *info)
{
list_s *node = info->history;
int i = 0;
while (node)
{
node->num = i++;
node = node->next;
}
return (info->hist_lines = i);
}