-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsh_history.c
111 lines (104 loc) · 2.11 KB
/
hsh_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
#include "shell.h"
/**
* hsh_history - builtin command hsh_history, mimics builtin history
* @arg_list: argrument list of input
* @envp: environemental variable list
* @mode: mode to direct which function to execute
* Return: Always 0
*/
int hsh_history(char **arg_list, env_t *envp, int mode)
{
static hist_t history = {NULL, NULL};
if (mode == 0)
create_history(&history, envp);
else if (mode == 1)
add_cmdhist(&history, arg_list[0]);
else if (mode == 2)
write_history(envp, &history);
else
{
if (arg_list[1] != NULL)
{
_write("Error: no such command\n");
return (2);
}
print_history_2(&history);
}
return (0);
}
/**
* print_history_2 - prints out the history with index
* @history: history linked list
*/
void print_history_2(hist_t *history)
{
int i, count;
char *num_str;
hist_t *temp, *temp2;
history = history->next;
for (count = 0, temp = history; temp != NULL; temp = temp->next, count++)
;
history = history->next;
for (i = 0, temp2 = history; temp2 != NULL; temp2 = temp2->next, i++)
{
num_str = _itoa(i, 2);
_write(" ");
_write(num_str);
_write(" ");
_write(temp2->cmd);
_write("\n");
}
}
/**
* hsh_history_help - builtin help printout for history
* Return: Always 0
*/
int hsh_history_help(void)
{
_write("history usage: history\n ");
_write("Display the history list with line numbers.\n");
return (0);
}
/**
* _itoa - interger to string converter
* @num: number to convert
* @mode: mode to determine how to deal with 0
* Return: a string for the number
*/
char *_itoa(int num, int mode)
{
char *num_str;
int index, exp, i, temp_exp;
num_str = safe_malloc(sizeof(char) * BUFSIZE);
_memset(num_str, '\0', BUFSIZE);
exp = 1000000000;
index = 0;
if (num != 0)
{
while ((num / exp) == 0)
exp /= 10;
temp_exp = exp;
while (temp_exp < 1000 && mode == 2)
{
num_str[index++] = 0 + '0';
temp_exp *= 10;
}
while (exp >= 1)
{
num_str[index++] = (num / exp) + '0';
num %= exp;
exp /= 10;
}
}
else
{
if (mode == 2)
{
for (i = 0; i < 4; i++)
num_str[i] = 0 + '0';
}
else
num_str[0] = 0 + '0';
}
return (num_str);
}