-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.c
More file actions
65 lines (58 loc) · 1.38 KB
/
options.c
File metadata and controls
65 lines (58 loc) · 1.38 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "options.h"
void init_options(opts *o){
o -> opts_c = 0;
o -> opts_t = 0;
o -> opts_x = 0;
o -> opts_d = 0;
// initialising all options to 0 i.e. false
}
char *getOptions(int argc, char *argv[]){
char *optionsstring;
int total = 0; //Total length of all arguments
int i;
int n = 0;
for(i=1; i < argc; i++){
total += strlen(argv[i]);
}
//Mallocating size for options string
optionsstring = (char *) malloc(total * sizeof(char));
for(i = 1; i < argc; i++){
if(argv[i][0] == '-'){
int j;
for(j = 0; j < strlen(argv[i]); j++){
optionsstring[n] = argv[i][j];
n++;
}
}
}
if(!optionsstring[0]){
return NULL;
}
optionsstring[n] = '\0';
return optionsstring;
}
opts get_options(char *o){
opts options;
init_options(&options);
int i;
for(i = 0; i < strlen(o); i ++){
switch(o[i]){
case 'c':
options.opts_c = 1;
break;
case 't':
options.opts_t = 1;
break;
case 'x':
options.opts_x = 1;
break;
case 'd':
options.opts_d = 1;
break;
}
}
return options;
}