-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.c
More file actions
93 lines (92 loc) · 1.98 KB
/
history.c
File metadata and controls
93 lines (92 loc) · 1.98 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
#include "headers.h"
#include "main.h"
#include "generalcommand.h"
#include "prompt.h"
#include "history.h"
void history(int no_of_arg, char **argument)
{
// fprintf(stderr, "%d\n", no_of_arg);
char addre[2048] = "";
strcat(addre, home);
strcat(addre, "/");
strcat(addre, "history.txt");
char hists[20][256];
for (int i = 0; i < 20; i++)
{
for (int k = 0; k < 256; k++)
{
hists[i][k] = '\0';
}
}
int fd1 = open(addre, O_RDWR);
// int fd1 = open(addre, O_RDWR);
int kj = 0;
if (fd1 < 0)
{
perror("history.txt - ");
}
else
{
char buffer[1024] = "";
int chars = read(fd1, buffer, 1024);
// printf("%s\n", buffer);
for (int i = 0; i < chars; i++)
{
int k = 0;
while (buffer[i] != '\n')
{
hists[kj][k] = buffer[i];
k++;
i++;
if (buffer[i] == '\n' || i >= chars)
{
break;
}
}
hists[kj][k] = '\0';
if (k != 0)
{
kj++;
}
}
}
close(fd1);
if (no_of_arg == 1)
{
if (kj <= 10)
{
for (int i = 0; i < kj; i++)
{
printf("%s\n", hists[i]);
}
}
else
{
for (int i = kj - 10; i < kj; i++)
{
printf("%s\n", hists[i]);
}
}
}
else
{
int number = 0;
int t = 1;
for (int i = strlen(argument[1]) - 1; i >= 0; i--)
{
number += (int)(argument[1][i] - '0') * t;
t = t * 10;
}
if (number > kj)
{
printf("Invalid Operation");
}
else
{
for (int i = kj - number; i < kj; i++)
{
printf("%s\n", hists[i]);
}
}
}
}