-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistoryprint.c
More file actions
53 lines (52 loc) · 1.03 KB
/
historyprint.c
File metadata and controls
53 lines (52 loc) · 1.03 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
#include "headers.h"
#include "historyprint.h"
void historyprint(char *inputs[], int args, char home[])
{
char file[1000];
strcpy(file, home);
strcat(file, "/hist.txt");
int fd = open(file, O_RDWR | O_CREAT, 0777);
char buffer[1000];
int size = read(fd, buffer, 1000);
buffer[size] = '\0';
int commands = 0;
for (int i = 0; i < size; i++)
{
if (buffer[i] == '\n')
{
commands++;
}
}
int lines;
if (args == 1)
{
lines = 10;
}
else if (args == 2)
{
lines = atoi(inputs[1]);
}
else
{
fprintf(stderr, "ERROR : Invalid Number of Arguments\n");
return;
}
if (lines <= commands)
{
int diff = commands - lines;
int index = 0;
while (diff > 0)
{
if (buffer[index] == '\n')
{
diff--;
}
index++;
}
printf("%s\n", buffer + index);
}
else
{
printf("%s\n", buffer);
}
}