-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhu1.c
271 lines (226 loc) · 6.68 KB
/
hu1.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*--- hu1.c ----------------------------------------------------------------*/
/* Hermann Sutter, Florian Thomas */
#include <eos32sys.h>
#include <eos32lib.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include "hu1.h"
#define MAX_LINE 500
#define MAX_COMMANDS 10
#define MAX_ARGUMENTS 19
#define FALSE 0
#define TRUE 1
/* count the finished Programs */
int finishedPrograms = 0;
/* signal handler */
void signal_callback_handler(int signum){
}
int main(void){
char inputStr[MAX_LINE];
char *token = NULL; /* read tokens from stream and command */
char *tmpArg; /* command could not overwrite by array allocate */
char *ptr; /* remove \n from string */
Commands **cmd;
struct tbuffer st_cpu; /* start time */
struct tbuffer en_cpu; /* end time */
int cntCom, cntArg, i, j; /* counter variables */
int childPid, pid, errno, status, exitValue;
/* SIGINT init, need for interrupt programm */
signal(SIGINT, signal_callback_handler);
while(TRUE){
/* reset cmd and finishedPrograms counter */
cmd = NULL;
finishedPrograms = 0;
/* read input line, string must be 501 because of last
null or \n entry, not sure about this point */
printf("> ");
if(fgets(inputStr, MAX_LINE+2, stdin) == NULL){
perror("error: input stream\n");
return 1;
}
if(strlen(inputStr) > MAX_LINE+1){
perror("error: input string to long\n");
return 1;
}
/* create command array */
tmpArg = (char *)malloc(sizeof(char) * strlen(inputStr));
strcpy(tmpArg, inputStr);
token = strtok(tmpArg, ";");
cntCom = 0;
while(token != NULL){
/* check if token is empty */
token = trimwhitespace(token);
if(strlen(token) == 0){
token = strtok(NULL, ";");
continue;
}
token = strtok(NULL, ";");
cntCom++;
}
/* if no valid command was entered continue at the next prompt */
if(cntCom == 0){
continue;
}
cmd = (Commands **)malloc(sizeof(Commands *));
if(cmd == NULL){
perror("error: out of memory\n");
return 1;
}
for(i=0; i<cntCom; i++){
cmd[i] = (Commands *)malloc(sizeof(Commands));
if(cmd[i] == NULL){
perror("error: out of memory\n");
return 1;
}
/* set exitStatus initially to "not finished correctly" */
cmd[i]->exitStatus = -1;
}
/* check for max commands, max commands are 10 */
if(cntCom > MAX_COMMANDS+1){
perror("error: to much commands\n");
return 1;
}
/* split string in tokens and save in struct */
strcpy(tmpArg, inputStr);
token = strtok(inputStr, ";");
for(i=0; i<cntCom; i++){
if( (ptr = strchr(token, '\n')) != NULL){
*ptr = '\0';
}
cmd[i]->command = token;
token = strtok(NULL, ";");
}
/* saves arguments in char array for each command */
for(i=0; i<cntCom; i++){
/* command can't be over written */
tmpArg = (char *) malloc(sizeof(char) * strlen(cmd[i]->command));
strcpy(tmpArg, cmd[i]->command);
/* count arguments */
cntArg = 0;
token = strtok(tmpArg, " ");
while(token != NULL){
cntArg++;
token = strtok(NULL, " ");
}
if((cntArg-1) > 20){
perror("error: to much arguments\n");
return 1;
}
/* create arguments array */
cmd[i]->args = (char **)malloc(sizeof(char *));
if(cmd[i]->args == NULL){
perror("error: out of memory\n");
return 1;
}
for(j=0; j<cntArg+1; j++){
cmd[i]->args[j] = (char *)malloc(sizeof(char));
if(cmd[i]->args == NULL){
perror("error: out of memory\n");
return 1;
}
}
strcpy(tmpArg, cmd[i]->command);
token = strtok(tmpArg, " ");
cmd[i]->command = token;
/* first element MUST be the command */
for(j=0; j<cntArg; j++){
cmd[i]->args[j] = token;
token = strtok(NULL, " ");
}
/* last element MUST be a NULL */
cmd[i]->args[cntArg] = NULL;
if(strlen(cmd[i]->command) > 21){
perror("error: command to long");
return 1;
}
}
/* fork each process */
for(i = 0; i < cntCom; i++){
childPid = fork();
if(childPid < 0){
finishedPrograms = finishedPrograms + 1;
printf("Fork of %s failed", cmd[i]->command);
}else if(childPid == 0){
/* exitValue is not 0 if the command can not be executed */
exitValue = execvp(cmd[i]->command, cmd[i]->args);
exit(exitValue);
}else{ /* Parent process */
cmd[i]->pid = childPid;
}
}
/* wait for all children */
while(TRUE){
times(&st_cpu);
pid = wait(&status);
times(&en_cpu);
for(i = 0; i < cntCom; i++){
if(cmd[i]->pid == (int)pid){
cmd[i]->exitStatus = 0;
cmd[i]->passedTime = en_cpu.child_user_time - st_cpu.child_user_time;
break;
}
}
finishedPrograms = finishedPrograms + 1;
if(finishedPrograms >= cntCom){
break;
}
}
/* only for good look */
printf("\n");
/* comes to this point by an interrupt with CTRL C */
if(errno == EINTR){
/* here are the non terminated processes */
while((pid = wait(&status)) != -1){
finishedPrograms = finishedPrograms + 1;
for(i = 0; i < cntCom; i++){
if(pid == cmd[i]->pid){
cmd[i]->exitStatus = -1;
break;
}
}
}
printSummary(cmd, cntCom);
exit(0);
}
printSummary(cmd, cntCom);
}
return 0;
}
/* prints the summary */
void printSummary(Commands **cmd, int numberOfCommands){
int summe, i;
/* only for good look */
printf("\n\n");
summe = 0;
for(i = 0; i < numberOfCommands; i++){
if (cmd[i]->exitStatus != 0){
printf("%s: [execution error]\n", cmd[i]->command);
}else{
/*sysconf(_SC_CLK_TCK);*/
printf("%s: user time = %d\n", cmd[i]->command, (int)(cmd[i]->passedTime));
summe += cmd[i]->passedTime;
}
}
summe = (int)(summe);
printf("sum of user times = %d\n", summe);
}
/* deletes whitespace before and after a given string */
/* this function was taken from
http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way */
char *trimwhitespace(char *str){
char *end;
/* Trim leading space */
while(isspace(*str)) str++;
if(*str == 0) /* All spaces? */
return str;
/* Trim trailing space */
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
/* Write new null terminator */
*(end+1) = 0;
return str;
}