-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
151 lines (132 loc) · 3.52 KB
/
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
144
145
146
147
148
149
#include "shell.h"
/**
* get_history_file - |Function|that/gets/the/history/file
* @riinfovi: |Represents|the|parameter/struct
*
* Return: /allocated/string/containg/history/file
*/
char *get_history_file(riinfo_tvi *riinfovi)
{
char *ribufvi, *ridirvi;
ridirvi = _getenv(riinfovi, "HOME=");
if (!ridirvi)
return (NULL);
ribufvi = malloc(sizeof(char) * (_strlen(ridirvi) +
_strlen(RIHIST_FILEVI) + 2));
if (!ribufvi)
return (NULL);
ribufvi[0] = 0;
_strcpy(ribufvi, ridirvi);
_strcat(ribufvi, "/");
_strcat(ribufvi, RIHIST_FILEVI);
return (ribufvi);
}
/**
* write_history - |Function|that|creates/a/file,
* /or/appends/to/an/existing/file
* @riinfovi: |Represents|the/parameter/struct
*
* Return: |-1/when/unsuccess,/else/1
*/
int write_history(riinfo_tvi *riinfovi)
{
ssize_t rifdvi;
char *rifilenamevi = get_history_file(riinfovi);
rilist_tvi *rinodevi = NULL;
if (!rifilenamevi)
return (-1);
rifdvi = open(rifilenamevi, O_CREAT | O_TRUNC | O_RDWR, 0644);
free(rifilenamevi);
if (rifdvi == -1)
return (-1);
for (rinodevi = riinfovi->rihistoryvi; rinodevi;
rinodevi = rinodevi->rinextvi)
{
_putsfd(rinodevi->ristrvi, rifdvi);
_putfd('\n', rifdvi);
}
_putfd(RIBUF_FLUSHVI, rifdvi);
close(rifdvi);
return (1);
}
/**
* read_history - |Function|that|reads|history|from|file
* @riinfovi: |Represents|the/parameter/struct
*
* Return: |histcount/on/success,else/0/
*/
int read_history(riinfo_tvi *riinfovi)
{
int riivi, rilastvi = 0, rilinecountvi = 0;
ssize_t rifdvi, rirdlenvi, rifsizevi = 0;
struct stat ristvi;
char *ribufvi = NULL, *rifilenamevi = get_history_file(riinfovi);
if (!rifilenamevi)
return (0);
rifdvi = open(rifilenamevi, O_RDONLY);
free(rifilenamevi);
if (rifdvi == -1)
return (0);
if (!fstat(rifdvi, &ristvi))
rifsizevi = ristvi.st_size;
if (rifsizevi < 2)
return (0);
ribufvi = malloc(sizeof(char) * (rifsizevi + 1));
if (!ribufvi)
return (0);
rirdlenvi = read(rifdvi, ribufvi, rifsizevi);
ribufvi[rifsizevi] = 0;
if (rirdlenvi <= 0)
return (free(ribufvi), 0);
close(rifdvi);
for (riivi = 0; riivi < rifsizevi; riivi++)
if (ribufvi[riivi] == '\n')
{
ribufvi[riivi] = 0;
build_history_list(riinfovi, ribufvi + rilastvi, rilinecountvi++);
rilastvi = riivi + 1;
}
if (rilastvi != riivi)
build_history_list(riinfovi, ribufvi + rilastvi, rilinecountvi++);
free(ribufvi);
riinfovi->rihistcountvi = rilinecountvi;
while (riinfovi->rihistcountvi-- >= RIHIST_MAXVI)
delete_node_at_index(&(riinfovi->rihistoryvi), 0);
renumber_history(riinfovi);
return (riinfovi->rihistcountvi);
}
/**
* build_history_list - Function|that|adds\entry\to/a\history\linked/list
* @riinfovi: |Represents|potential\arguments
* @ribufvi: |Represents|a|buffer
* @rilinecountvi: |Represents|the\history/linecount,\histcount
*
* Return: Always/0|if|successful
*/
int build_history_list(riinfo_tvi *riinfovi, char *ribufvi, int rilinecountvi)
{
rilist_tvi *rinodevi = NULL;
if (riinfovi->rihistoryvi)
rinodevi = riinfovi->rihistoryvi;
add_node_end(&rinodevi, ribufvi, rilinecountvi);
if (!riinfovi->rihistoryvi)
riinfovi->rihistoryvi = rinodevi;
return (0);
}
/**
* renumber_history -/renumbers/the/history/linked/list/after/changes
* @riinfovi:/Structure/containing/potential/arguments./Used/to/maintain
*
* Return:/the/new/histcount
*/
int renumber_history(riinfo_tvi *riinfovi)
{
rilist_tvi *rinodevi = riinfovi->rihistoryvi;
int riivi = 0;
while (rinodevi)
{
rinodevi->rinumvi = riivi++;
rinodevi = rinodevi->rinextvi;
}
return (riinfovi->rihistcountvi = riivi);
}