-
Notifications
You must be signed in to change notification settings - Fork 0
/
detecter.c
88 lines (67 loc) · 3.18 KB
/
detecter.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
/*
OPERATING SYSTEM ARCHITECTURE COURSE PROJECT
SARA SHAMILOVA
RAHIM SHARIFOV
SABINA HADJIMURADOVA
MARCH 2019
*/
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include "DetecLib.h"
int main (int argc, char* argv[]){
if (argc==1) {
printf("Usage: ./detect [-t format] [-i interval] [-l limit] [-c] prog arg arg \n");
exit(1);
}
char *format=NULL; // time format that is to be printed, NULL by default that is time is not going to be printed
int interval=10000*1000; //interval between two consecutive launch in millisecond, 10000 by default that is 10 seconds between each launch
int limit=-1; // limit which is number of times that program is going to be executed, -1 by default that is command is going to be executed infinite times
int EXIT=0; //variable to check the condition whether print exit value, 0 by default that is exit value is not going to be printed
char option; // option that getopt takes from command line at a time
while ((option=getopt(argc,argv,"+t:i:l:c"))!=-1){ // getopt is not POSIXLY CORRECT, '+' is for getopt to stop when encounter non '-' argument
switch (option){
case 't':
format=optarg; //assinging argument of option t, as a format for printing time
break;
case 'i':
interval=atoi(optarg)*1000; //assigning argument of option i, as a time interval between to consecutive launch
if (interval == 0 ){
printf ("NUll interval \n "); // Program exits when null interval is given
exit (1);
}
if (interval < 0 ){
printf ("Negative interval \n "); // Program exits when negative interval is given
exit (1);
}
break;
case 'l':
limit=atoi(optarg); // assigning argument of option l, as a limit for command launches
if (limit <= 0){
printf("%d", limit);
printf ("Invalid value for limit \n "); //Program exits when limit is negative or 0
exit(1);
}
break;
case 'c':
EXIT=1; // changing state of EXIT to print exit value of command
break;
default:
printf ("INVALID OPTION %c \n", option); // ERROR message when an invalid option is given
exit(1);
}
}
if (argv[optind]==NULL){
printf("--null program\n"); //Program exits when no command is given to execute
exit(1);
}
char **args; //declaring double pointer to store array of strings
args=argv+optind; //assigning pointer that points to command and its arguments to newly created pointer
launch(limit,EXIT,args,interval,format); //function that is takes all arguments prints in displat what is needed
return 0;
}