-
Notifications
You must be signed in to change notification settings - Fork 0
/
hist.c
82 lines (72 loc) · 1.32 KB
/
hist.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
#include "shell.h"
static int no_init_hist;
static list_t *hist_head;
static list_t *cmd_ls;
/**
* get_hist_addr - Return the address oof history head
* Return: Address of history head
*/
list_t **get_hist_addr()
{
if (no_init_hist == 1)
{
hist_head = NULL;
cmd_ls = NULL;
no_init_hist = 0;
}
return (&hist_head);
}
/**
* last_cmd_addr - Return the address oof history head
*
* Return: Address of last entered command
*/
list_t **last_cmd_addr()
{
if (no_init_hist == 1)
{
hist_head = NULL;
cmd_ls = NULL;
no_init_hist = 0;
}
return (&cmd_ls);
}
/**
* _hist - Prints the all the commands entered
*
* Return: 1 on success
*/
int _hist(void)
{
list_t *curr_;
char *num_str;
int i, len;
int count = *hist_count() % 4096;
for (curr_ = *get_hist_addr(); curr_ != NULL; curr_ = curr_->next)
{
num_str = str_num(count++);
len = _puts(num_str);
for (i = len; i < 7; i++)
_puts(" ");
_puts(curr_->str);
_puts("\n");
free(num_str);
}
return (1);
}
/**
* hist_handler - Adds a command to the history
* @buff: User's input
*/
void hist_handler(char *buff)
{
if (cmd_ls == NULL || buff[0] != ' ' || strcmp(cmd_ls->str, buff) != 0)
cmd_ls = end_node_add(get_hist_addr(), buff);
}
/**
* free_hist - Frees the memory used by history lists
*/
void free_hist(void)
{
free_list(*get_hist_addr());
}