-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory_func.c
155 lines (140 loc) · 3.1 KB
/
history_func.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
#include "shell.h"
/**
* create_history - creates a linked list for user
* command history, size is 4096
* @history: history linked list to pass
* @envp: environmental variable linked list
* Return: a pointer to the history linked list
*/
void create_history(hist_t *history, env_t *envp)
{
char *str, *buf;
int i, j, n;
/* create a buf of what is in the file */
buf = safe_malloc(sizeof(char) * BUFSIZE);
_memset(buf, '\0', BUFSIZE);
n = read_file(envp, &buf);
if (n > 0)
{
str = safe_malloc(sizeof(char) * _strlen(buf));
if (*buf == '\0')
{
add_history(history, "");
}
/* create linked list and fill in with what is in file*/
for (i = 0, j = 0; buf[i] != '\0'; i++)
{
if (buf[i] == '\n')
{
str[j] = '\0';
add_history(history, str);
j = 0;
}
else
str[j++] = buf[i];
}
}
}
/**
* print_history - prints the history
* @head: the head of the linked list
*/
void print_history(hist_t *head)
{
char *str;
while (head != NULL)
{
str = _strdup(head->cmd);
_write(str);
_write("\n");
head = head->next;
}
}
/**
* add_history - creates another history node to the linked list
* @head: head of the history linked list
* @cmd: command to store in list
* Description: this function will add node to the end of the list
* Return: a pointer to the new node
*/
hist_t *add_history(hist_t *head, char *cmd)
{
hist_t *new_node;
hist_t *temp;
new_node = safe_malloc(sizeof(hist_t));
new_node->cmd = _strdup(cmd);
new_node->next = NULL;
if (head == NULL)
head = new_node;
else
{
temp = head;
while (temp->next != NULL)
temp = temp->next;
temp->next = new_node;
}
return (new_node);
}
/**
* read_file - reads the simple_shell_history file and put it in a buffer
* @envp: environemental variable linked list to get the HOME directory
* @buf: buffer to input what is read
* Return: 1 if success and 0 if failed
*/
int read_file(env_t *envp, char **buf)
{
static int b_size = BUFSIZE;
int offset, n, fd;
char *path, *new_buf;
path = rm_vname(envp, "HOME", BUFSIZE);
if (path == NULL)
{
_write("Error: Cannot find Home\n");
_write("Cannot find history file\n");
return (1);
}
_strcat(path, "/.simple_shell_history");
fd = open(path, O_RDWR | 0600);
if (fd > 0)
{
offset = 0;
while ((n = read(fd, *buf + offset, b_size - offset)) > 0)
{
if (n < b_size - offset)
{
(*buf)[n + offset] = '\0';
}
b_size *= 2;
new_buf = safe_malloc((b_size) * sizeof(char));
offset += n;
_memcpy(new_buf, *buf, b_size / 2);
*buf = new_buf;
}
close(fd);
return (1);
}
return (0);
}
/**
* make_path - makes the path
* @path: path to create
* @filename: filename to open
* @key: key to create
* @envp: environemental linked list
* Return: the path str
*/
char *make_path(char **path, char *filename, char *key, env_t *envp)
{
char *value;
if (_strstr(key, "/"))
{
*path = filename;
return (filename);
}
value = get_env_value(envp, key);
*path = safe_malloc(sizeof(char) * (_strlen(value) + _strlen(filename) + 2));
_memcpy(*path, value, _strlen(value) + 1);
strcat(*path, "/");
strcat(*path, filename);
return (*path);
}