-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionParser.c
More file actions
100 lines (92 loc) · 3.15 KB
/
optionParser.c
File metadata and controls
100 lines (92 loc) · 3.15 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
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
//Authors: Peyton Gardipee and Tushar Verma
//CS537 Fall 2018
//File Purpose:
//Use getOpt to parse through the command line. Set the appropriate flags based on their intended default values
//and the options that were set
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "optionParser.h"
#define BASETEN 10
int process(int argc,char* argv[], flag* cmdFlags)
{
int opt; //Used by getOpt
char* end = NULL;
//Flags from passed in flag struct
cmdFlags->p = 0;
cmdFlags->pid = 0;
cmdFlags->s = 0;
cmdFlags->U = 1;
cmdFlags->S = 0;
cmdFlags->v = 0;
cmdFlags->c = 1;
//Using getOpt, parse through command line and set flags
while ((opt = getopt(argc, argv, "c::v::S::U::s::p:")) != -1) {
switch (opt) {
case 'c':
//Defaults to true
if(optarg == NULL){
cmdFlags->c = 1;
}
else if(strcmp(optarg, "-") == 0 ){
cmdFlags->c = 0;
}
break;
case 'v':
//Defaults to false
if(optarg == NULL){
cmdFlags->v = 1;
}
else if(strcmp(optarg, "-") != 0 ){
puts("Invalid input for the 'v' option");
exit(-1);
}
break;
case 'S':
//Defaults to false
if(optarg == NULL){
cmdFlags->S = 1;
}
else if(strcmp(optarg, "-") != 0 ){
puts("Invalid input for the 'S' option");
exit(-1);
}
break;
case 'U':
//Defaults to true
if(optarg == NULL){
cmdFlags->U = 1;
}
else if(strcmp(optarg, "-") == 0 ){
cmdFlags->U = 0;
}
break;
case 's':
//Defaults to false
if(optarg == NULL){
cmdFlags->s = 1;
}
else if(strcmp(optarg, "-") != 0 ){
puts("Invalid input for the 's' option");
exit(-1);
}
break;
case 'p':
if(optarg == NULL){
puts("Invalid input for the 'p' option. Valid argument required.");
exit(-1);
}
else if(strcmp(optarg, "-") == 0 ){
puts("Invalid input for the 'p' option");
exit(-1);
}
cmdFlags->p = 1;
cmdFlags->pid = strtol(optarg,&end,BASETEN);
break;
default:
break;
}
}
return 0;
}