-
Notifications
You must be signed in to change notification settings - Fork 1
/
history.c
85 lines (77 loc) · 1.66 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
#include "shell.h"
/**
* write_history - write history of commands
* @history: command to be stored in the history list
* Return: 0 on success and 1 in faliure
*/
int write_history(char *history)
{
char *filename, *h = _getenv("HOME");
char *tmp = NULL;
ssize_t w, fd;
int len = _strlen(history);
filename = _malloc(_strlen(h) + _strlen(".shell.history") + 4);
_strcpy(filename, h), _strcat(filename, "/.shell.history");
fd = open(filename, O_RDWR | O_CREAT | O_APPEND, 0640);
free(filename);
if (fd < 0)
return (1);
if (len)
{
tmp = _malloc(len + 4);
_strcpy(tmp, history), _strcat(tmp, "\n");
w = write(fd, tmp, len + 1);
if (w < 0)
{
close(fd);
free(tmp);
return (1);
}
free(tmp);
}
close(fd);
return (0);
}
/**
* print_history - print history
* Return: 0 on success and 1 in faliure
*/
int print_history(void)
{
char *filename, *h = _getenv("HOME");
char *history = _malloc(4);
int fd, w = 1;
int counter = 0;
char *istr = NULL;
filename = _malloc(_strlen(h) + _strlen(".shell.history") + 4);
_strcpy(filename, h);
_strcat(filename, "/.shell.history");
fd = open(filename, O_RDONLY);
if (fd < 0)
return (1);
while (w > 0)
{
counter++;
w = read(fd, history, 1);
if (w > 0)
{
_write(-1, NULL, 0), _write(1, " ", 3);
istr = itoa(counter);
_write(1, istr, _strlen(istr)), _write(1, " ", 2);
_write(1, NULL, 0), free(istr);
}
_write(-1, NULL, 0);
while (w > 0 && *history != '\n')
{
_write(1, history, _strlen(history));
w = read(fd, history, 1);
}
if (w > 0 && *history == '\n')
_write(1, "\n", 1);
_write(1, NULL, 0);
}
free(filename);
free(history);
close(fd);
return (1);
}