9374646fb5ae6e4baf745486e96ac0fab2695425
[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 * fifo = "/tmp/process1fifo";
36
37         /** File descriptor of pipe */
38         int file_descriptor;
39
40         mkfifo(fifo, 0666);
41
42         file_descriptor = open(fifo, O_WRONLY);
43         
44         do {
45                 c = fgetc(stdin);
46
47                 /*
48                  * Check if current index is bigger than current buffer size.
49                  * If so increment buffer size. On error release memory, and set
50                  * appropriate flags.
51                  */
52                 if (i >= buffer_length) {
53                         tmp = NULL;
54                         buffer_length += BUFFER_STEP;
55                         tmp = (char*) realloc(buffer, buffer_length);
56                         if (tmp == NULL) {
57                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
58                                 free(buffer);
59                                 buffer = NULL;
60                                 c = EOF;
61                         }
62                 }
63
64                 /*
65                  * If there were no errors parse data.
66                  */
67                 if (c != EOF) {
68                         /* If newline has been found
69                          * return entire string and release the memory
70                          */
71                         if (c == 10) {
72                                 buffer[i] = '\n';
73                                 write(file_descriptor, buffer, ++buffer_length);
74                                 /*fprintf(stdout, "%s\n", buffer);*/
75
76                                 buffer_length = 0;
77                                 i = 0;
78
79                                 free(buffer);
80                                 buffer = NULL;
81                                 tmp = NULL;
82                         }
83                         /*
84                          * Normal character, add it to the buffer
85                          */
86                         else {
87                                 buffer = tmp;
88                                 buffer[i] = c;
89
90                                 /* Used for debug..*/
91                                 /*
92                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
93                                 */
94                                 i++;
95                         }
96                 }
97         } while(c != EOF);
98
99         close(fifo);
100         unlink(fifo);
101
102         if (buffer) {
103                 free(buffer);
104                 buffer = NULL;
105         }
106
107         return 0;
108 }