-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory.c
117 lines (110 loc) · 2.61 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
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
// verified
#include "display.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h> // contains boolean realtons
#include "history.h"
#include <stdlib.h>
typedef char *stringg;
typedef struct passwd *main_struct;
typedef long long element_type;
#define MAX_LIMITS 1000
#define okay_limit 200
long long check_argu(long long arguments) // check if vald arguments are passed
{
if (arguments >= 2)
{
printf("More arguments than desired\n");
return 0;
}
}
void history(int argc, stringg argv[]) // history call
{
int count;
int flagu = check_argu(argc);
if (flagu == 0)
{
return;
}
else if (argc == 1)
{
count = 10;
}
// int haha = min(10, log_count);
int haha = 0;
if (10 > log_count)
{
haha = log_count;
}
else
{
haha = 10;
}
for (int i = haha - 1; i >= 0; i--) // prints recents 10 onto screen
{
printf("%d %s\n",(haha-i), logs[i]);
}
}
void write_history(stringg command) // updates history when new command is entered diff from prew
// recent one comes at top
{
element_type flag = 0;
long long len = strlen(command) - 1;
if (command[len] == '\n')
{
command[len] = '\0';
}
if (strcmp(logs[0], command) == 0)
{
flag = 1;
}
if (flag == 0)
{
for (int i = log_count; i > 0; i--)
{
char str[okay_limit];
strcpy(str, logs[i-1]);
strcpy(logs[i], str);
}
long long len = (strlen(command) - 1);
if (command[len] == '\n')
{
command[len] = '\0';
}
// printf("%s is the value of command\n",command);
strcpy(logs[0], command);
if (log_count < 20)
{
log_count++;
}
}
}
void read_history() // reads the histpry of past execution
{
FILE *lf;
lf = fopen("history.txt", "r");
log_count = 0;
char command[okay_limit];
while (fgets(command, okay_limit, lf))
{
long long len = (strlen(command) - 1);
if (command[len] == '\n')
{
command[len] = '\0';
}
strcpy(logs[log_count++], command);
}
}
void print_history() // updates history.txt with new history
{
FILE *lf;
lf = fopen("history.txt", "w");
for (int i = 0; i < log_count; i++)
{
chdir(home_directory);
fprintf(lf, "%s\n", logs[i]);
}
fclose(lf);
}