-
Notifications
You must be signed in to change notification settings - Fork 2
/
concurrent_error.c
109 lines (90 loc) · 3.2 KB
/
concurrent_error.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <wait.h>
enum RETURNCODE {
GOOD,
DELIMITER,
ENDOFFILE
};
struct RETURNVALUE {
enum RETURNCODE returncode;
char* returnstring;
};
char*** GLOBAL_ARG = NULL;
struct RETURNVALUE myread(void) {
int c, i = 0, str_size = 8;
struct RETURNVALUE callresult;
callresult.returncode = GOOD;
callresult.returnstring = NULL;
while ((c = getchar()) != EOF) {
if (c != ' ') {
if (c != ';' && c != '\n') {
if (callresult.returnstring == NULL) callresult.returnstring = (char*) calloc(str_size, sizeof(char));
if (++i >= str_size) {
str_size <<= 1;
callresult.returnstring = (char*) realloc(callresult.returnstring, str_size * sizeof(char));
}
callresult.returnstring[i] = 0;
callresult.returnstring[i - 1] = c;
continue;
}
callresult.returncode = DELIMITER;
break;
}
if (callresult.returnstring) break;
}
if (c == EOF) callresult.returncode = ENDOFFILE;
return callresult;
}
void free_dynamic_memory(void) {
if (!GLOBAL_ARG) return;
for (int i = 0; GLOBAL_ARG[i]; ++i) {
for (int j = 0; GLOBAL_ARG[i][j]; ++j) free(GLOBAL_ARG[i][j]);
free(GLOBAL_ARG[i]);
}
free(GLOBAL_ARG);
}
int main(int argc, const char ** argv) {
char** myargv = NULL;
int current_index_argv = 0;
int current_global_argv = 0;
atexit(free_dynamic_memory);
int initial_argvsize = 2;
int initial_argsize = 2;
do {
struct RETURNVALUE result = myread();
if (result.returnstring != NULL) {
if (!myargv) { myargv = (char**) calloc(initial_argvsize, sizeof(char**)); }
if (++current_index_argv >= initial_argvsize) {
initial_argvsize <<= 1;
myargv = (char**) realloc(myargv, initial_argvsize * sizeof(char*));
}
myargv[current_index_argv] = NULL;
myargv[current_index_argv - 1] = result.returnstring;
}
if (myargv && (result.returncode != GOOD)) {
if (!GLOBAL_ARG) GLOBAL_ARG = (char***) calloc(initial_argsize, sizeof(char**));
if (++current_global_argv >= initial_argsize) {
initial_argsize <<= 1;
GLOBAL_ARG = (char***) realloc(GLOBAL_ARG, initial_argsize * sizeof(char**));
}
GLOBAL_ARG[current_global_argv - 1] = myargv;
myargv = NULL;
current_index_argv = 0;
initial_argvsize = 2;
GLOBAL_ARG[current_global_argv] = NULL;
}
if (result.returncode == ENDOFFILE) break;
} while (1);
int exitstatus;
for (int process_num = 0; GLOBAL_ARG[process_num]; process_num++) {
int pid = fork();
if (pid == -1) exit(1);
if (!pid) { execvp(GLOBAL_ARG[process_num][0], GLOBAL_ARG[process_num]); exit(127); }
wait(&exitstatus);
}
if (WIFEXITED(exitstatus)) return WEXITSTATUS(exitstatus);
if (WIFSIGNALED(exitstatus)) return 128 + WTERMSIG(exitstatus);
return 1;
}