Again remove not used libraries.
[wsti_so.git] / src / process2.c
1 #include <stdio.h>
2
3 /* open/read/write/close */
4 #include <fcntl.h>
5
6 /** If buffer is too small to hold entire string, it is incremented by this value */
7 #define BUFFER_STEP 16
8
9 /**
10  * Program grabs data from process1, calculates number of characters in each line
11  * and pass the value to process3.
12  */
13 int main(void) {
14         /** Named pipe used to communicate with process1 */
15         char * read_pipe = "/tmp/process1pipe";
16
17         /** Named pipe used to communicate with process3 */
18         char * write_pipe = "/tmp/process2pipe";
19
20         int read_descriptor;
21         int write_descriptor;
22         
23         char buffer[BUFFER_STEP];
24         
25         int i = 0;
26         ssize_t count = 0;
27
28         /* Reading from process1 */
29         read_descriptor = open(read_pipe, O_RDONLY);
30
31         /* Writing to process2 */
32         mkfifo(write_pipe, 0666);
33         write_descriptor = open(write_pipe, O_WRONLY);
34
35         int number_of_characters = 0;
36         
37         while(1) {
38                 /* Read data from input pipe */
39                 count = read(read_descriptor, buffer, BUFFER_STEP);
40
41                 fprintf(stderr, "[%s] Fetched: %d bytes\n", "process2", count);
42
43                 if (count > 0) {
44                         for (i = 0; i < count; i++, number_of_characters++) {
45                                 if (buffer[i] == '\n') {
46                                         fprintf(stderr, "[%s] Calculated: %d characters. Sending...\n", "process2", number_of_characters);
47                                         write(write_descriptor, &number_of_characters, sizeof(number_of_characters));
48                                         write(write_descriptor, '\n', 1);
49                                         number_of_characters = 0;
50                                 }
51                         }
52                 }
53                 else {
54                         break;
55                 }
56         }
57
58         close(read_descriptor);
59         close(write_descriptor);
60         unlink(write_descriptor);
61
62         return 0;
63 }