Finishing bootstrap program.
[wsti_so.git] / src / process1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /* open/read/write/close */
6 #include <fcntl.h>
7
8 /** If buffer is too small to hold entire string, it is incremented by this value */
9 #define BUFFER_STEP 16
10
11 /**
12  * Program reads entire lines of text from the standard input and pass them
13  * to the process2 using created pipe.
14  */
15 int main(void) {
16         /** Currently fetched from stdin character */
17         int c;
18
19         /** Buffer used to store line of characters */
20         char * buffer = NULL;
21
22         /** Current buffer length*/
23         int buffer_length = 0;
24
25         /** Index of the current character */
26         int i = 0;
27
28         /**
29          * Temporary buffer used as a proxy between
30          * checking memory allocation and copying data to real buffer 
31          */
32         char * tmp = NULL;
33
34         /** Named pipe used to communnicate with process2 */
35         char * write_pipe = "/tmp/process1pipe";
36
37         /** File descriptor of pipe */
38         int file_descriptor;
39
40         mkfifo(write_pipe, 0666);
41
42         file_descriptor = open(write_pipe, O_WRONLY);
43
44         fprintf(stderr, "[%s] Init!\n", "process1");
45
46         do {
47                 c = fgetc(stdin);
48
49                 /*
50                  * Check if current index is bigger than current buffer size.
51                  * If so increment buffer size. On error release memory, and set
52                  * appropriate flags.
53                  */
54                 if (i >= buffer_length) {
55                         tmp = NULL;
56                         buffer_length += BUFFER_STEP;
57                         tmp = (char*) realloc(buffer, buffer_length);
58                         if (tmp == NULL) {
59                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
60                                 free(buffer);
61                                 buffer = NULL;
62                                 c = EOF;
63                         }
64                 }
65
66                 /*
67                  * If there were no errors or it was not just an empty newline:
68                  * parse data.
69                  */
70                 if (c != EOF || ((i == 0) && (c == 10))) {
71                         /* If newline has been found
72                          * return entire string and release the memory
73                          */
74                         if (c == 10 && (i != 0)) {
75                                 buffer[i] = '\n';
76                                 write(file_descriptor, buffer, strlen(buffer));
77                                 fprintf(stderr, "[%s] buffer: %s/%d\n", "process1", buffer, strlen(buffer));
78
79                                 buffer_length = 0;
80                                 i = 0;
81
82                                 free(buffer);
83                                 buffer = NULL;
84                                 tmp = NULL;
85                         }
86                         /*
87                          * Normal character, add it to the buffer
88                          */
89                         else {
90                                 buffer = tmp;
91                                 buffer[i] = c;
92
93                                 /* Used only for debugging..*/
94                                 /*
95                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
96                                 */
97                                 i++;
98                         }
99                 }
100         } while(c != EOF);
101
102         close(write_pipe);
103         unlink(write_pipe);
104
105         if (buffer) {
106                 free(buffer);
107                 buffer = NULL;
108         }
109
110         return 0;
111 }