-
Notifications
You must be signed in to change notification settings - Fork 1
/
history.c
149 lines (133 loc) · 2.65 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"
static char **history;
/**
* read_history - reads from the history file and save to an array
* none
* Return: 0 on success, -1 otherwise
*/
int read_history(void)
{
FILE *history_file;
size_t n, len;
char *path, *home, *line, **hist;
hist = malloc(sizeof(char *));
hist[0] = NULL;
home = getenv("HOME");
path = malloc(sizeof(char) * (str_len(home) + 23));
if (path)
{
mem_cpy(path, home, str_len(home));
mem_cpy(path + str_len(home), "/.simple_shell_history", 23);
history_file = fopen(path, "r");
if (history_file == NULL)
{
free(path);
return (-1);
}
n = 0;
line = NULL;
while (getline(&line, &n, history_file) > 0)
{
len = len_array(hist);
hist = realloc(hist, sizeof(char *) * (len + 2));
hist[len] = str_dup(line);
hist[len + 1] = NULL;
}
fclose(history_file);
free(path);
free(line);
}
history = hist;
return (0);
}
/**
* display_history - displays the shell history
* @args: the arg vector
* Return: 0 on success, -1 otherwise
*/
int display_history(char **args __attribute__((unused)))
{
size_t i;
i = 0;
while (history[i])
{
printf("%lu %s", i, history[i]);
i++;
}
return (0);
}
/**
* add_to_history - adds a new command to the history array
* @command: the command entered
* Return: 0 on success, -1 otherwise
*/
int add_to_history(char *command)
{
char *comm;
unsigned int len_command;
size_t len;
if (command)
{
len_command = str_len(command);
comm = malloc(sizeof(char) * (len_command + 2));
if (comm)
{
mem_cpy(comm, command, len_command);
mem_cpy(comm + len_command, "\n", 1);
mem_cpy(comm + len_command + 1, "\0", 1);
len = len_array(history);
history = realloc(history, sizeof(char *) * (len + 2));
if (history == NULL)
{
free(comm);
return (-1);
}
history[len] = comm;
history[len + 1] = NULL;
}
else
return (-1);
}
return (0);
}
/**
* write_history - writes the history array to a file in $HOME
* none
* Return: 0 on success, -1 otherwise
*/
int write_history(void)
{
int fd, i;
char *path, *home;
home = getenv("HOME");
path = malloc(sizeof(char) * (str_len(home) + 23));
if (path)
{
mem_cpy(path, home, str_len(home));
mem_cpy(path + str_len(home), "/.simple_shell_history", 23);
fd = open(path, O_WRONLY | O_CREAT, 0666);
if (fd == -1)
{
free(path);
return (-1);
}
i = 0;
while (history[i])
{
write(fd, history[i], str_len(history[i]));
i++;
}
close(fd);
free(path);
}
return (0);
}
/**
* get_history - return the history array outside this file
* none
* Return: pointer to the history array
*/
char **get_history(void)
{
return (history);
}