lots of stuff
[assignments.git] / lab / lab8 / trivial_shell.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <pthread.h>
4 #include <string.h>
5 #include <signal.h>
6
7 pthread_t threads[255];
8
9 void cleanup_handler(void *pipe) {
10
11 FILE *cast_pipe;
12
13 cast_pipe = (FILE *) pipe;
14
15 pclose(pipe);
16 }
17
18 void *exec_newthread(void *input) {
19
20 char *cast_input, *out_buf;
21 FILE *out_pipe;
22
23 out_buf = (char *) calloc(1, 512);
24 cast_input = (char *) input;
25
26 out_pipe = popen(cast_input, "r");
27 pthread_cleanup_push(cleanup_handler, (void *) out_pipe);
28 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
29
30 for(;;) {
31
32 fgets(out_buf, 255, out_pipe);
33
34 if(feof(out_pipe))
35 break;
36
37 printf("%s", out_buf);
38 }
39
40 pthread_cleanup_pop(1);
41
42 return 0x0;
43 }
44
45 int main(int argc, char *argv[]) {
46
47 int thread_counter;
48 char *input_buf;
49
50 thread_counter = 0;
51 input_buf = (char *) calloc(1, 512);
52
53 while(strncmp(input_buf, "quit", 4)) {
54
55 fgets(input_buf, 255, stdin);
56
57 if(!strncmp(input_buf, "quit", 4))
58 break;
59
60 pthread_create(&threads[thread_counter], NULL, exec_newthread, input_buf);
61 pthread_detach(threads[thread_counter]);
62 ++thread_counter;
63 }
64
65 return 0;
66 }
67
68