-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhistory.c
170 lines (153 loc) · 3.18 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "header.h"
/**
* history_list - initializes linked list of history from history file in $HOME
* @arginv: arguments inventory
*
* Return: head (pointer to first node of linked list of environ variables)
*/
history_t *history_list(arg_inventory_t *arginv)
{
history_t *head;
char *file, *buffer;
int fd;
size_t iterations = 1, charcount = 0, i = 0;
head = NULL;
file = arginv->history_file;
fd = open(file, O_RDONLY);
buffer = safe_malloc(sizeof(char) * BUFSIZE);
if (fd == -1 || buffer == NULL)
{
free(buffer);
return (head);
}
while (TRUE)
{
i = read(fd, (buffer + charcount), BUFSIZE);
if (i <= 0)
break;
charcount += i;
if (i % BUFSIZE == 0)
{
iterations++;
buffer = _realloc(buffer, charcount, (BUFSIZE * iterations));
}
else
break;
}
close(fd);
if (charcount == 0)
{
free(buffer);
return (head);
}
head = init_history(head, buffer);
return (head);
}
/**
* init_history - initializes history using history file converted to buffer
* @head: head of linked list of history commands
* @buffer: old history backup converted to string
*
* Return: head (pointer to first node of linked list of environ variables)
*/
history_t *init_history(history_t *head, char *buffer)
{
int marker, sublen, j, i = 0;
char *temp;
while (buffer[i] != '\0')
{
marker = i;
sublen = 0;
while (buffer[i] != '\n')
sublen++, i++;
temp = safe_malloc(sizeof(char) * (sublen + 2));
j = 0;
while (j <= sublen)
{
temp[j] = buffer[marker];
j++, marker++;
}
add_node_history(&head, temp);
i++;
free(temp);
}
free(buffer);
return (head);
}
/**
* add_node_history - adds new node to end of linked list of input history
* @head: head of history linked list
* @command: new command
*
* Return: pointer to new node
*/
history_t *add_node_history(history_t **head, char *command)
{
history_t *new_node, *temp;
unsigned int i = 1;
new_node = malloc(sizeof(history_t));
if (new_node == NULL)
return (NULL);
new_node->command = _strdup(command);
new_node->next = NULL;
new_node->number = 0;
if (!*head)
*head = new_node;
else
{
temp = *head;
while (temp->next)
{
temp = temp->next;
i++;
}
if (i > 4095)
{
free_history(*head);
*head = new_node;
return (new_node);
}
new_node->number = i;
temp->next = new_node;
}
return (new_node);
}
/**
* file_history - converts history to string to file in history file
* @arginv: arguments inventory
*
* Return: 0 success, 1 failure
*/
int file_history(arg_inventory_t *arginv)
{
char *history_text;
history_text = history_to_string(arginv->history);
trunc_text_to_file(arginv->history_file, history_text);
free(history_text);
return (0);
}
/**
* history_to_string - converts history linked list to string
* @head: head of linked list of history
*
* Return: linked list converted to string
*/
char *history_to_string(history_t *head)
{
history_t *temp = head;
char *string;
unsigned int size = 0;
while (temp)
{
size += _strlen(temp->command);
temp = temp->next;
}
string = safe_malloc(sizeof(char) * (size + 1));
temp = head;
while (temp)
{
_strncat(string, temp->command, _strlen(temp->command));
temp = temp->next;
}
return (string);
}