-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhstry.c
190 lines (172 loc) · 3.41 KB
/
hstry.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "shell.h"
/**
* addHistory - adds user input to history linked list and
* pops head if list is bigger than HSTRYLIMIT
* @input: user input
* @nodeCount: pointer to history count
* Return: pointer to newly added history node
**/
hstory *addHistory(hstory **head, char *input, int *nodeCount)
{
hstory *new, *temp;
new = _malloc(sizeof(hstory));
if (new == NULL)
return (NULL);
new->input = _strcpy(input, _strlen(input));
if (new->input == NULL)
{
_free(new);
return (NULL);
}
new->next = NULL;
if (*head == NULL)
{
*head = new;
*nodeCount += 1;
}
else
{
temp = *head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new;
*nodeCount += 1;
if (*nodeCount > HSTRYLIMIT)
popHead(head, nodeCount);
}
return (new);
}
/**
* popHead - removes head of the history list and subtract node count
* @nodeCount: pointer to history count
* Return: pointer to new head
**/
hstory *popHead(hstory **head, int *nodeCount)
{
hstory *temp;
temp = *head;
*head = (*head)->next;
_free(temp->input);
_free(temp);
temp = NULL;
*nodeCount -= 1;
return (*head);
}
/**
* readFromFile - reads history from file and populate the linkedlist
* @file: pointer to name of file
* @nodeCount: pointer to history count
* Return: number of added added to list or -1 on error
**/
int readFromFile(char *file, hstory **head, int *nodeCount)
{
int fd, numNodes;
char *input, **tokns;
ssize_t count;
struct stat st;
off_t filsiz;
numNodes = 0;
if (stat(file, &st) == 0)
{
filsiz = st.st_size;
input = _malloc(sizeof(char) * filsiz + 1);
if (input == NULL)
return (-1);
fd = open(file, O_RDONLY);
if (fd == -1)
{
_puts("could not open to read ");
_puts(file);
_puts("\n");
_free(input);
return (-1);
}
count = read(fd, input, filsiz);
if (count == -1 || count != filsiz)
{
close(fd);
_free(input);
return (-1);
}
tokns = tokenize(input, '\n');
if (tokns == NULL)
return (numNodes);
while (*tokns)
{
addHistory(head, *tokns, nodeCount);
numNodes++, tokns++;
}
}
close(fd);
return (numNodes);
}
/**
* writeHstorytofile - writes history to histro file from history list
* @file: pointer to file name
* Return: 0 on success, -1 on error
**/
int writeHstorytofile(char *file)
{
int fd, count, inptLen;
hstory **head, *temp;
head = getHistoryHead();
fd = open(file, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1)
{
_puts("could not open to write ");
_puts(file);
_puts("\n");
return (-1);
}
if (head != NULL)
{
temp = *head;
while (temp)
{
inptLen = _strlen(temp->input);
count = write(fd, temp->input, inptLen);
if (count == -1 || count != inptLen)
return (-1);
if (inptLen > 0 && temp->input[inptLen - 1] != '\n')
count = write(fd, "\n", 1);
if (count == -1)
return (-1);
temp = temp->next;
}
close(fd);
return (0);
}
close(fd);
return (-1);
}
/**
* printHistory - prints history list
* @str: pointer to input as part of builtin prototype
* Return: nothing
**/
void printHistory(char **str)
{
int i, len;
hstory *start, **head;
UNUSED(str);
head = getHistoryHead();
if (head == NULL)
{
_puts("No history\n");
return;
}
i = 0;
start = *head;
while (start)
{
_puts("[");
_puts_num(i);
_puts("] ");
_puts(start->input);
len = _strlen(start->input);
if (len > 0 && start->input[len - 1] != '\n')
_puts("\n");
start = start->next;
i++;
}
}