-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhistory.c
226 lines (179 loc) · 4.29 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "headers.h"
int builtin_history(char **args, int arg_count)
{
if (arg_count > 2)
{
fprintf(stderr, "Too many arguments\n");
return EXIT_FAILURE;
}
FILE *fp = fopen(PATH_TO_HISTORY_FILE, "r");
if (fp == NULL)
{
perror("fopen() error");
return EXIT_FAILURE;
}
char *content = (char *)malloc(sizeof(char) * MAX_HISTORY_FILE_LEN);
if (content == NULL)
{
fprintf(stderr, "Error allocating memory");
fclose(fp);
return EXIT_FAILURE;
}
fseek(fp, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(fp);
rewind(fp);
fread(content, len, 1, fp);
content[len] = '\0';
char *token = strtok(content, "\n");
long num = 0;
char *end = NULL;
num = strtol(token, &end, 10);
if (end == token)
{
fprintf(stderr, "Not an integer argument\n");
return EXIT_FAILURE;
}
if ((num > INT_MAX) || (num < 0))
{
fprintf(stderr, "Number out of range\n");
return EXIT_FAILURE;
}
int cnt = (int)num;
int iter = cnt;
if (arg_count == 2)
{
int temp = 0;
num = strtol(args[1], &end, 10);
if (end == args[1])
{
fprintf(stderr, "Not an integer argument\n");
return EXIT_FAILURE;
}
if ((num > INT_MAX) || (num < 0))
{
fprintf(stderr, "Number out of range\n");
return EXIT_FAILURE;
}
temp = (int)num;
if (cnt > temp)
{
cnt = temp;
}
}
token = strtok(NULL, "\n");
while (token != NULL && iter != 0)
{
if (iter <= cnt)
{
printf("%s\n", token);
}
token = strtok(NULL, "\n");
--iter;
}
free(content);
fclose(fp);
return EXIT_SUCCESS;
}
int add_to_history(char *cmd)
{
FILE *fp = fopen(PATH_TO_HISTORY_FILE, "r");
if (fp == NULL)
{
perror("fopen() error");
return EXIT_FAILURE;
}
char *content = (char *)malloc(sizeof(char) * MAX_HISTORY_FILE_LEN);
if (content == NULL)
{
fprintf(stderr, "Error allocating memory");
fclose(fp);
return EXIT_FAILURE;
}
char *num = (char *)malloc(sizeof(char) * 5);
if (num == NULL)
{
fprintf(stderr, "Error allocating memory");
fclose(fp);
return EXIT_FAILURE;
}
char *first_cmd = (char *)malloc(sizeof(char) * BUFFER_SIZE);
if (first_cmd == NULL)
{
fprintf(stderr, "Error allocating memory");
fclose(fp);
return EXIT_FAILURE;
}
char *rest_cmds = (char *)malloc(sizeof(char) * MAX_HISTORY_FILE_LEN);
if (rest_cmds == NULL)
{
fprintf(stderr, "Error allocating memory");
fclose(fp);
return EXIT_FAILURE;
}
fseek(fp, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(fp);
rewind(fp);
if (len > 0)
{
fread(content, len, 1, fp);
content[len] = '\0';
}
else
{
strcpy(content, "0\n");
}
char *token = strtok(content, "\n");
strcpy(num, token);
token = strtok(NULL, "\n");
if (token != NULL)
{
strcpy(first_cmd, token);
token = strtok(NULL, "\0");
if (token != NULL)
{
strcpy(rest_cmds, token);
token = strtok(NULL, "\0");
}
}
fp = freopen(NULL, "w", fp);
long n = 0;
char *end = NULL;
n = strtol(num, &end, 10);
if (end == num)
{
fprintf(stderr, "Not an integer argument\n");
return EXIT_FAILURE;
}
if ((n > INT_MAX) || (n < 0))
{
fprintf(stderr, "Number out of range\n");
return EXIT_FAILURE;
}
int no = (int)n;
if (no == MAX_COMMANDS_STORED)
{
fprintf(fp, "%s\n%s\n%s", num, rest_cmds, cmd);
}
else
{
no++;
if (no == 1)
{
fprintf(fp, "%d\n%s", no, cmd);
}
else if (no == 2)
{
fprintf(fp, "%d\n%s\n%s", no, first_cmd, cmd);
}
else
{
printf("%s\n", cmd);
fprintf(fp, "%d\n%s\n%s\n%s", no, first_cmd, rest_cmds, cmd);
}
}
free(num);
free(first_cmd);
free(rest_cmds);
fclose(fp);
return EXIT_SUCCESS;
}