-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
96 lines (88 loc) · 2.48 KB
/
parse.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
#include "parse.h"
#include "help.h"
#include <string.h>
#include <stdio.h>
void init_choices(options* choices){
choices->c = 0;
choices->a = 0;
choices->x = 0;
choices->j = 0;
choices->d = 0;
choices->m = 0;
choices->q = 0;
choices->p = 0;
}
int parse(int argc, char** argv, options* choices, char** archive,list_t** filelist){
list_t *arglist;
list_create(&arglist,sizeof(flag_argument),free);
flag_argument *oargument, *temp_argument;
int number_of_flags = 0;
//Searching for flags validity, count them, enqueue them
int i;
for(i=1; i<argc; i++){
if(argv[i][0] == '-'){
//This is flag
if(strcmp(argv[i],"-c") && strcmp(argv[i],"-a") && strcmp(argv[i],"-x") && strcmp(argv[i],"-m") && strcmp(argv[i],"-q") && strcmp(argv[i],"-p") && strcmp(argv[i],"-j")){
perror("Wrong argument flag type\n");
printhelp();
return -1;
}
oargument = (flag_argument*)malloc(sizeof(flag_argument));
oargument->type = argv[i];
list_enqueue(arglist,oargument);
}
else{
number_of_flags = --i;
break;
}
}
//If more flags than the standard
if(number_of_flags > NUM_OF_FLAGS){
perror("Wrong number of flag arguments\n");
printhelp();
return -2;
}
//Recognize the flag list and initialize the options struct to store them
init_choices(choices);
list_iter_t* iter;
list_iter_create(&iter);
list_iter_init(iter,arglist,FORWARD);
while((temp_argument = list_iter_next(iter)) != NULL){
if(!strcmp(temp_argument->type,"-c")){
choices->c = 1;
}
else if(!strcmp(temp_argument->type,"-a")){
choices->a = 1;
}
else if(!strcmp(temp_argument->type,"-x")){
choices->x = 1;
}
else if(!strcmp(temp_argument->type,"-m")){
choices->m = 1;
}
else if(!strcmp(temp_argument->type,"-q")){
choices->q = 1;
}
else if(!strcmp(temp_argument->type,"-p")){
choices->p = 1;
}
else if(!strcmp(temp_argument->type,"-j")){
choices->j = 1;
}
}
list_iter_destroy(&iter);
list_destroy(&arglist);
//Store the destination archive file
*archive = argv[number_of_flags + 1];
//Store in a filelist the source files or dirs names to be archived
file_argument *fargument;
//Creates a list of files for the calling function and returns it in the filelist argument
//destroy should locate on the outter process
list_create(filelist,sizeof(file_argument),free);
for(i = number_of_flags + 2; i<argc; i++){
fargument = (file_argument*)malloc(sizeof(file_argument));
fargument->filename = argv[i];
list_enqueue(*filelist,fargument);
}
return 0;
}