-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_history.c
More file actions
40 lines (28 loc) · 858 Bytes
/
shell_history.c
File metadata and controls
40 lines (28 loc) · 858 Bytes
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
#include "shell_history.h"
int shell_history(char** args, char* root) {
char* hist_file_path = (char*)malloc(1000);
strcpy(hist_file_path, root);
strcat(hist_file_path, "/.history");
FILE* hist_file = fopen(hist_file_path, "r");
if (hist_file == NULL) {
fprintf(stderr, "History File Couldn't be opened\n");
return 1;
}
char** commands = (char**)malloc(1000*sizeof(char*));
int i = 0;
int length = 0;
commands[i] = (char*)malloc(1000);
while (fgets(commands[i], 1000, hist_file) != NULL) {
i++;
commands[i] = (char*)malloc(1000);
}
int j = i - 1;
int n = 10;
if (args[1] != NULL && strcmp(args[1], "-n") == 0) {
n = atoi(args[2]);
}
for (int k = 0; k < n && j >= 0; k++) {
printf("%s", commands[j--]);
}
free(commands);
}