-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistory_helper.c
More file actions
132 lines (122 loc) · 2.47 KB
/
history_helper.c
File metadata and controls
132 lines (122 loc) · 2.47 KB
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
#include "shell.h"
/**
* delete_first_cmd - delete the first cmd node in linked list
* essentially 'popping the head off'
* @file_str: beginning of the history linked list
* Return: beginning of history linked list
*/
node_t **delete_first_cmd(node_t **file_str)
{
node_t *temp;
if (!file_str)
return (NULL);
temp = *file_str;
*file_str = temp->next;
free(temp->name);
if (temp->value != NULL)
free(temp->value);
free(temp);
return (file_str);
}
/**
* _getcmd - get the cmd from the user input
* @file_strm: file strm, STDIN_FILENO or an opened file
* Return: string from cmd
*/
char *_getcmd(int file_strm)
{
ssize_t nline;
size_t length;
char *line;
length = LINE_LENGTH;
line = malloc(sizeof(char) * length);
if (line == NULL)
return (NULL);
nline = _getline(&line, &length, file_strm);
if (nline == -1)
{
free(line);
return (NULL);
}
line[nline - 1] = '\0';
return (line);
}
/**
* history_init - initializes the history list based on .simple_shell_history
* @file_str: beginning of history linked list
* Return: beginning of history linked list
*/
node_t **history_init(node_t **file_str)
{
int fp, nr, i;
size_t length;
char *cmd;
mode_t modes;
modes = S_IRUSR | S_IWUSR;
fp = open(".simple_shell_history", O_RDWR | O_CREAT, modes);
if (fp == -1)
return (NULL);
length = LINE_LENGTH;
cmd = malloc(sizeof(char) * length);
do {
nr = _getline(&cmd, &length, fp);
if (*cmd != '\n' && *cmd != '\0')
{
i = _strlen(cmd);
cmd[i] = '\0';
add_node_end(file_str, cmd, NULL);
}
} while (nr > 0);
free(cmd);
if (nr == -1)
return (NULL);
close(fp);
return (file_str);
}
/**
* node_count - count and return number of nodes in a list
* @file_str: beginning of linked list to be counted
* Return: number of nodes
*/
int node_count(node_t **file_str)
{
int i;
node_t *tmp;
for (i = 0, tmp = *file_str; tmp != NULL; i++, tmp = tmp->next)
;
return (i);
}
/**
* num_to_str - convert a number to a string
* @i: number to convert
* Return: a string
*/
char *num_to_str(int i)
{
int dig;
int num;
char *num_str;
if (i == 0)
{
num_str = malloc(sizeof(char) * 2);
num_str[0] = '0';
num_str[1] = '\0';
return (num_str);
}
for (dig = 1, num = 0; i / dig > 0; dig *= 10, num++)
;
num_str = malloc(sizeof(char) * (num + 1));
if (num_str == NULL)
return (NULL);
dig /= 10;
num = 0;
while (dig >= 1)
{
num_str[num] = (i / dig) + '0';
i = i % dig;
dig = dig / 10;
num++;
}
num_str[num] = '\0';
return (num_str);
}