forked from AngelDevil1223/PROC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.c
89 lines (85 loc) · 1.95 KB
/
options.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "options.h"
//Global variables
const int ON = 1;
const int OFF = 0;
const int ERROR = -1;
const int maxPid = 32768;
//Initializes all of the options then sets them according to command line input
int initOptions(int argc, char *argv[], struct options *flags)
{
flags->p = OFF;
flags->s = OFF;
flags->U = OFF;
flags->S = OFF;
flags->v = OFF;
flags->c = OFF;
return argsCheck(argc, argv, flags);
}
//argsCheck uses getopt to check the command line arguments and stores them
//into the passed structure. Returns -1 on error.
int argsCheck(int argc, char *argv[], struct options *flags)
{
int opt;
//opterr disables error outputs
opterr = 0;
while ((opt = getopt(argc, argv, "p:s::U::S::v::c::")) != -1)
{
switch (opt)
{
case 's':
flags->s = ON;
if (optarg != 0 && argv[optind - 1][2] == '-')
flags->s = OFF;
else if (optarg != 0)
return ERROR;
break;
//p case is only exception. checks for valid PID argument.
case 'p':
if (optarg == NULL) {}
else
{
flags->p = atoi(optarg);
if (flags->p <= 0 || flags->p > maxPid)
return ERROR;
}
break;
case 'U':
flags->U = ON;
if (optarg != 0 && argv[optind - 1][2] == '-')
flags->U = OFF;
else if (optarg != 0)
return ERROR;
break;
case 'S':
flags->S = ON;
if (optarg != 0 && argv[optind - 1][2] == '-')
flags->S = OFF;
else if (optarg != 0)
return ERROR;
break;
case 'v':
flags->v = ON;
if (optarg != 0 && argv[optind - 1][2] == '-')
flags->v = OFF;
else if (optarg != 0)
return ERROR;
break;
case 'c':
flags->c = ON;
if (optarg != 0 && argv[optind - 1][2] == '-')
flags->c = OFF;
else if (optarg != 0)
return ERROR;
break;
//'?' case is a catch-all for unnacceptable arguments
case '?':
return ERROR;
}
}
//returns 0 if everything passed
return 0;
}