-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcronjob.c
133 lines (118 loc) · 3.29 KB
/
cronjob.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "shell.h"
#include "utils.h"
#include "cronjob.h"
#include "parse.h"
void handle_cronjob(int argc, char** argv) {
if (argc > 0 && strcmp(argv[argc - 1], "&") == 0) {
argc--;
}
if (argc < 6) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
bool c_next = false;
bool t_next = false;
bool p_next = false;
bool c_found = false;
bool t_found = false;
bool p_found = false;
char* command = (char*)malloc(sizeof(char) * MAX_STATIC_STR_LEN);
command[0] = '\0';
int t, p;
for (int i = 0; i < argc; i++) {
if (t_next) {
t_next = false;
t_found = true;
errno = 0;
t = strtol(argv[i], NULL, 10);
if (errno > 0) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
continue;
}
if (p_next) {
p_next = false;
p_found = true;
errno = 0;
p = strtol(argv[i], NULL, 10);
if (errno > 0) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
continue;
}
if (strcmp(argv[i], "-c") == 0) {
if (c_found) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
c_next = true;
c_found = true;
continue;
}
if (strcmp(argv[i], "-t") == 0) {
if (t_found) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
t_next = true;
c_next = false;
continue;
}
if (strcmp(argv[i], "-p") == 0) {
if (p_found) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
c_next = false;
p_next = true;
continue;
}
if (c_next) {
c_found = true;
strcat(command, argv[i]);
strcat(command, " ");
continue;
} else {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return;
}
}
if (!t_found || !c_found || !p_found) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
if (command != NULL) {
free(command);
}
return;
}
cronjob(command, t, p);
}
bool cronjob(char* command, int t, int p) {
if (p == 0) {
return true;
}
if (t < 0 || p < 0) {
fprintf(stderr, "Could not start cronjob: Invalid arguments.\n");
return false;
}
int num;
if (t > p) {
num = 1;
} else {
num = p / t;
}
pid_t pid = fork();
if (pid < 0) {
perror("Could not start cronjob");
return false;
} else if (pid == 0) {
for (int i = 0; i < num; i++) {
char* temp = (char*)malloc(sizeof(char) * (strlen(command) + 1));
strcpy(temp, command);
parse_command(temp); // pipes not needed, because split by pipe already
sleep(t);
}
exit(EXIT_SUCCESS);
}
}