-
Notifications
You must be signed in to change notification settings - Fork 0
/
history.c
180 lines (159 loc) · 3.77 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "headers.h"
#include "functions.h"
void createHistory()
{
strcpy(hispath, HomeDirectory);
strcat(hispath, "/.ish_history");
if (debug)
printf("about to create history\n");
FILE *temp = fopen(hispath, "w");
if (debug)
if (temp == NULL)
{
printf("could not create history\n");
return;
}
fprintf(temp, "0 \n");
fclose(temp);
if (debug)
perror("while creating history : ");
if (debug)
printf("c h failed\n");
}
void history()
{
strcpy(hispath, HomeDirectory);
strcat(hispath, "/.ish_history");
FILE *f = fopen(hispath, "r");
if (f == NULL)
{
createHistory();
}
else
{
fclose(f);
}
f = fopen(hispath, "r");
int size;
// fscanf(f, "%d", &size);
char buf[MY_LEN] = "";
fgets(buf, MY_LEN, f);
size = atoi(buf);
char *commands[size + 1];
if (debug)
{
printf("commands array created\n");
}
for (int i = 0; i <= size; i++)
{
commands[i] = (char *)malloc(MY_LEN * sizeof(char));
}
for (int i = 0; i < size; i++)
{
// fscanf(f, "%s", commands[i]);
fgets(commands[i], MY_LEN, f);
if (debug)
printf("scanning commands[%d] = %s\n", i, commands[i]);
// DeDashify(commands[i]);
}
for (int i = max(size - 10 ,0); i < size; i++)
{
printf("%s", commands[i]);
fputs(commands[i], f);
if (debug)
printf("printing commands[%d] = %s\n", i, commands[i]);
}
fclose(f);
}
void AddHistory(char *command)
{
// Dashify(command);
char entry[MY_LEN];
strcpy(entry, command);
strcat(entry, "\n");
if (debug)
printf("\nadding to history %s\n", entry);
strcpy(hispath, HomeDirectory);
strcat(hispath, "/.ish_history");
int createhistory = 0;
FILE *f = fopen(hispath, "r");
if (f == NULL)
{
createhistory = 1;
}
else
{
fclose(f);
}
if (debug)
printf("calling createHistory()\n");
if (createhistory)
createHistory();
f = fopen(hispath, "r");
if (debug)
{
printf("hispath = %s\n",hispath);
// perror("opening if created");
}
int size;
// fscanf(f, "%d", &size);
char buf[MY_LEN] = "";
fgets(buf, MY_LEN, f);
size = atoi(buf);
if (debug)
printf("size read = %d\n", size);
char *commands[size + 1];
if (debug)
{
printf("commands array created\n");
}
for (int i = 0; i <= size; i++)
{
commands[i] = (char *)malloc(MY_LEN * sizeof(char));
}
for (int i = 0; i < size; i++)
{
if (debug)
printf("commands[%d] getting it \n", i);
// fscanf(f, "%[^\n]%*c", commands[i]);
fgets(commands[i], MY_LEN, f);
if (debug)
printf("commands[%d] = %s\n", i, commands[i]);
}
fclose(f);
if (size > 0)
{
if (debug)
printf("command = %s\n", command); // USE ENTRY HERE
if (debug)
printf("commands[size] = %s\n", commands[size - 1]);
if (!strcmp(entry, commands[size - 1]))
{
return;
}
}
f = fopen(hispath, "w");
strcpy(commands[size], entry);
if (debug)
printf("after copying = %s\n", commands[size]);
if ((size + 1) > 20)
{
assert(size == 20);
fprintf(f, "%d \n", 20);
for (int i = 1; i < 21; i++)
{
// fprintf(f, "%s ", commands[i]);
fputs(commands[i], f);
}
}
else
{
fprintf(f, "%d \n", size + 1);
for (int i = 0; i < (size + 1); i++)
{
// fprintf(f, "%s ", commands[i]);
fputs(commands[i], f);
}
}
fclose(f);
}