-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
52 lines (51 loc) · 1.24 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
#include <stdio.h>
#include <stdlib.h>
#include <readline/history.h>
#include <readline/readline.h>
int history(char *arguments)
{
if (arguments == NULL)
{
HISTORY_STATE *hist = history_get_history_state();
HIST_ENTRY **list = history_list();
int hist_value = hist->length;
if (hist->length == 0)
{
return 0;
}
if (hist->length > 10)
{
hist_value = 10;
}
for (int i = hist->length - hist_value, j = 0; j < hist_value; i++, j++)
{
if (!list[i]->line)
{
return 0;
}
printf("%d\t%s\n", i + 1, list[i]->line);
}
}
else
{
int req_length = atoi(arguments);
HISTORY_STATE *hist = history_get_history_state();
HIST_ENTRY **list = history_list();
if (hist->length == 0)
{
return 0;
}
if (hist->length > 10)
{
req_length = 10;
}
for (int i = hist->length - req_length, j = 0; j < req_length; i++, j++)
{
if (!list[i]->line)
{
return 0;
}
printf("%d\t%s\n", i + 1, list[i]->line);
}
}
}