-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
54 lines (43 loc) · 1.15 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
#include <stdio.h>
#include <string.h>
#include "./include/history.h"
int loadHistory(char history[HISTORYSIZE][PATHANDNAMESIZE])
{
FILE *ptr = fopen(HISTORYPATH, "r");
if (ptr == NULL)
{
printf("\033[091;49mUnable to Load File\033[0m");
return -1;
}
char input[1000];
int i = 0;
while(fgets(input, 1000, ptr)!= NULL)
{
strcpy(history[i++],input);
// printf("%s",history[i]);
// i++;
}
fclose(ptr);
return i;
}
void printHistory(char history[HISTORYSIZE][PATHANDNAMESIZE], int noOfCommandsInHistory)
{
for (int i = ((noOfCommandsInHistory - 10) > 0 ? (noOfCommandsInHistory - 10) : 0); i < noOfCommandsInHistory; i++)
printf("%2d %s", i + 1, history[i]);
printf("\n");
}
void writeHistory(char history[HISTORYSIZE][PATHANDNAMESIZE], int noOfCommandsInHistory)
{
FILE *ptr = fopen(HISTORYPATH, "w");
if (ptr == NULL)
{
printf("\033[0;91;49mUnable to write on file\033[0m");
return;
}
for (int i = 0; i< noOfCommandsInHistory; i++)
{
fputs(history[i], ptr);
// printf("%s",history[i]);
}
fclose(ptr);
}