-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.c
312 lines (251 loc) · 7.71 KB
/
commands.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "commands.h"
#include "tar.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* @description: is the entry point to handle all commands
* @parameter: (argumentCount) amount of command line arguments received
* @parameter: (argumentList) the list of the arguments received
* @output: exit code of the program
*/
int handleCommands(int argumentCount, char *argumentList[]) {
if (argumentCount <= 1) {
displayHelp();
return 0;
}
int flagCount;
char *flags[argumentCount];
getFlags(argumentCount, argumentList, &flagCount, flags);
char *filename = NULL;
Flags selectedMode = UNKNOWN;
// run the check for long flags like --create, --update, etc.
for (int i = 0; i < flagCount; i++) {
Flags currentMode = UNKNOWN;
if (isFlag(flags[i])) {
continue;
}
currentMode = determineFlag(flags[i]);
if (currentMode == VERBOSE) {
isGlobalVerbosed = true;
}
if (currentMode == USE_FILE) {
filename = getOutFilename(argumentCount, argumentList);
if (filename == NULL)
return 1;
}
if (currentMode == UNKNOWN) {
char errorMessage[100];
snprintf(errorMessage, 100, "unknown flag %s", flags[i]);
logError(errorMessage);
logInfo("run \"star --help\" to see the available flags");
return 1;
}
if (currentMode != VERBOSE && currentMode != USE_FILE) {
selectedMode = currentMode;
}
}
// run the check for short flags, like -cvz
for (int i = 0; i < flagCount; i++) {
if (isLongFlag(flags[i])) {
continue;
}
for (int k = 0; k < strlen(flags[i]); k++) {
if (flags[i][k] == '-')
continue;
char buffer[2];
buffer[0] = flags[i][k];
Flags currentMode = determineFlag(buffer);
if (currentMode == VERBOSE) {
isGlobalVerbosed = true;
}
if (currentMode == USE_FILE) {
filename = getOutFilename(argumentCount, argumentList);
if (filename == NULL)
return 1;
}
if (currentMode == UNKNOWN) {
char errorMessage[100];
snprintf(errorMessage, 100, "unknown flag %s", flags[i]);
logError(errorMessage);
logInfo("run \"star --help\" to see the available flags");
return 1;
}
if (currentMode != VERBOSE && currentMode != USE_FILE) {
selectedMode = currentMode;
}
}
}
int filesCount;
char *files[argumentCount];
getFiles(argumentCount, argumentList, &filesCount, files);
return callCommands(selectedMode, files, filesCount, filename);
}
/**
* @description: decide which command to send
* @parameter: (command) the command to be executed
* @parameter: (files) the files passed as parameter to the program
* @parameter: (fileCount) the amount of files passed as parameter to the
* program
* @parameter: (filename) the tar filename to be written or read
* @output: exit code of the program
*/
int callCommands(Flags command, char *files[], int fileCount, char *filename) {
if (command == HELP) {
return displayHelp();
}
if (command == EXTRACT) {
return extract(filename);
}
if (command == CREATE) {
return create(files, fileCount, filename);
}
if (command == LIST) {
return list(filename);
}
if (command == DELETE) {
return delete (files, fileCount, filename);
}
if (command == UPDATE) {
return update(files, fileCount, filename);
}
if (command == APPEND) {
return append(files, fileCount, filename);
}
if (command == PACK) {
return pack(filename);
}
return 0;
}
/**
* @description: determines which flag is received
* @parameter: (flag) the string flag, either in its long version or short
* version line
* @parameter: (argumentList) the arguments received from command line
* @output: the flag enum to be used
*/
Flags determineFlag(char *flag) {
if (strcmp(flag, "--create") == 0 || flag[0] == 'c') {
return CREATE;
}
if (strcmp(flag, "--extract") == 0 || flag[0] == 'x') {
return EXTRACT;
}
if (strcmp(flag, "--list") == 0 || flag[0] == 't') {
return LIST;
}
if (strcmp(flag, "--delete") == 0) {
return DELETE;
}
if (strcmp(flag, "--update") == 0 || flag[0] == 'u') {
return UPDATE;
}
if (strcmp(flag, "--verbose") == 0 || flag[0] == 'v') {
return VERBOSE;
}
if (strcmp(flag, "--file") == 0 || flag[0] == 'f') {
return USE_FILE;
}
if (strcmp(flag, "--append") == 0 || flag[0] == 'r') {
return APPEND;
}
if (strcmp(flag, "--help") == 0 || flag[0] == 'h') {
return HELP;
}
if (strcmp(flag, "--pack") == 0 || flag[0] == 'p') {
return PACK;
}
return UNKNOWN;
}
/**
* @description: retrives the filename to be used
* @parameter: (argumentCount) the amount of arguments received from command
* line
* @parameter: (argumentList) the arguments received from command line
* @output: the tar filename
*/
char *getOutFilename(int argumentCount, char *argumentList[]) {
// Iterate over all arguments
for (int i = 1; i < argumentCount; i++) {
// If the argument ends with ".tar", return it
if (endsWithTar(argumentList[i])) {
return argumentList[i];
}
}
// Log an error if no .tar file is found
logError("No .tar file specified in arguments");
return NULL;
}
/**
* @description: set all the flags that the user passed as parameter
* @parameter: (argumentCount) the amount of arguments received from command
* line
* @parameter: (argumentList) the arguments received from command line
* @parameter: (flagCount) a pointer to set the flag count found
* @parameter: (flags) a pointer to set the flags found
* @output: n/a
*/
void getFlags(int argumentCount, char *argumentList[], int *flagCount,
char *flags[]) {
*flagCount = 0;
for (int i = 1; i < argumentCount; i++) {
bool isValidFlag = isFlag(argumentList[i]) || isLongFlag(argumentList[i]);
if (isValidFlag) {
flags[*flagCount] = argumentList[i];
(*flagCount)++;
}
}
}
/**
* @description: set all the files that are necessary for the command
* @parameter: (argumentCount) the amount of arguments received from command
* line
* @parameter: (argumentList) the arguments received from command line
* @parameter: (fileCount) a pointer to set the files count found
* @parameter: (files) a pointer to set the files found
* @output: n/a
*/
void getFiles(int argumentCount, char *argumentList[], int *fileCount,
char *files[]) {
*fileCount = 0;
int startIndex = 2;
// just received "star" "<flag>" "<flag>" "<files>..."
if (argumentCount >= 4) {
startIndex = 3;
}
for (int i = startIndex; i < argumentCount; i++) {
bool isValidFlag = isFlag(argumentList[i]) || isLongFlag(argumentList[i]);
if (!isValidFlag && !endsWithTar(argumentList[i])) {
files[*fileCount] = argumentList[i];
(*fileCount)++;
}
}
}
/**
* @description: determines if it is a single flag
* @parameter: (flag) the string flag
* @output: true if it is a short flag
*/
bool isFlag(char *flag) { return flag[0] == '-' && flag[1] != '-'; }
/**
* @description: determines if it is a long flag
* @parameter: (flag) the string flag
* @output: true if it is a long flag
*/
bool isLongFlag(char *flag) {
return strlen(flag) > 1 && flag[0] == '-' && flag[1] == '-';
}
/**
* @description: determines if a filename in string format is a tar file
* @parameter: (filename) the string filename
* @output: true if it ends with .tar extension
*/
bool endsWithTar(const char *filename) {
const char *tarSuffix = ".tar";
size_t lenSuffix = strlen(tarSuffix);
size_t lenFilename = strlen(filename);
// Check if the filename is longer than the suffix and ends with ".tar"
return lenFilename >= lenSuffix &&
strcmp(filename + lenFilename - lenSuffix, tarSuffix) == 0;
}