-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.c
More file actions
161 lines (153 loc) · 4.15 KB
/
history.c
File metadata and controls
161 lines (153 loc) · 4.15 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
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
#include "headers.h"
#include "history.h"
void history(int number_of_commands, char *history_file)
{
FILE *read_file = fopen(history_file, "r");
if (read_file == NULL && errno != 2)
{
perror("Error opening history file. Command not added to history.\n");
return;
}
char line[10000];
int command_count = 0;
char last_command[10000];
char history_commands[20][1000];
while (fgets(line, sizeof(line), read_file))
{
strcpy(history_commands[command_count], line);
command_count++;
}
if (fclose(read_file) != 0)
{
perror("Error closing file");
}
if (command_count < number_of_commands)
{
for (int x = 0; x < command_count; x++)
printf("%s", history_commands[x]);
printf("\n");
return;
}
for (int x = command_count - number_of_commands; x < command_count; x++)
printf("%s", history_commands[x]);
printf("\n");
}
void add_history(char *command, char *history_file)
{
if (strlen(command) == 0)
return;
FILE *read_file = fopen(history_file, "r");
char line[10000];
int fputs_error = 10;
int command_count = 0;
char last_command[10000];
char history_commands[20][10000];
if (read_file == NULL && errno != 2)
{
perror("Error opening history file. Command not added to history.\n");
return;
}
if (read_file != NULL)
while (fgets(line, sizeof(line), read_file))
{
strcpy(history_commands[command_count], line);
command_count++;
}
if (read_file != NULL)
{
if (fclose(read_file) != 0)
{
perror("Error closing file");
}
}
if (strcmp(history_commands[command_count - 1], command) == 0)
{
return;
}
FILE *clear_file = fopen(history_file, "w");
if (clear_file == NULL && errno != 2)
{
perror("Error opening history file. Command not added to history.\n");
return;
}
fputs_error = fputs("", clear_file);
if (fputs_error < 0)
{
perror("Error writing command to history file.\n");
return;
}
if (fclose(clear_file) != 0)
{
perror("Error closing file");
}
FILE *append_file = fopen(history_file, "a");
if (append_file == NULL && errno != 2)
{
perror("Error opening history file. Command not added to history.\n");
return;
}
int starting_val = 0;
if (command_count == 20)
starting_val = 1;
for (int x = starting_val; x < command_count; x++)
{
fputs_error = fputs(history_commands[x], append_file);
if (fputs_error < 0)
{
perror("Error writing command to history file.\n");
return;
}
}
if (command_count != 0)
{
fputs_error = fputs("\n", append_file);
if (fputs_error < 0)
{
perror("Error writing command to history file.\n");
return;
}
}
fputs_error = fputs(command, append_file);
if (fputs_error < 0)
{
perror("Error writing command to history file.\n");
return;
}
if (fclose(append_file) != 0)
{
perror("Error closing file");
}
}
char *get_nth_history(int n, char *history_file)
{
FILE *read_file = fopen(history_file, "r");
char *return_str = calloc(10000, sizeof(char));
if (read_file == NULL)
{
if (errno != 2)
perror("Error opening history file. Command not added to history.\n");
strcpy(return_str, "");
return return_str;
}
char line[10000];
int command_count = 0;
char last_command[10000];
char history_commands[20][10000];
while (fgets(line, sizeof(line), read_file))
{
strcpy(history_commands[command_count], line);
command_count++;
}
if (fclose(read_file) != 0)
{
perror("Error closing file");
}
int a = command_count - n;
if (a < 0)
strcpy(return_str, history_commands[0]);
else
strcpy(return_str, history_commands[a]);
if (return_str[strlen(return_str) - 1] == '\n')
return_str[strlen(return_str) - 1] = '\0';
return return_str;
}