Created first program.
[wsti_so.git] / src / process1.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 /** If buffer is too small to hold entire string, it is incremented by this value */
6 #define BUFFER_STEP 16
7
8 /**
9  * Program reads entire lines of text from the standard input and returns them
10  * on the standard output without changing anything.
11  */
12 int main(void) {
13         /** Currently fetched from stdin character */
14         int c;
15
16         /** Buffer used to store line of characters */
17         char * buffer = NULL;
18
19         /** Current buffer length*/
20         int buffer_length = 0;
21
22         /* Index of the current character */
23         int i = 0;
24         
25         /** Temporary buffer used as a proxy between
26          * checking memory allocation and copying data to real buffer 
27          */
28         char *tmp = NULL;
29
30         do {
31                 c = fgetc(stdin);
32
33                 /*
34                  * Check if current index is bigger than current buffer size.
35                  * If so increment buffer size. On error release memory, and set
36                  * appropriate flags.
37                  */
38                 if (i >= buffer_length) {
39                         tmp = NULL;
40                         buffer_length += BUFFER_STEP;
41                         tmp = (char*) realloc(buffer, buffer_length);
42                         if (tmp == NULL) {
43                                 fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
44                                 free(buffer);
45                                 buffer = NULL;
46                                 c = EOF;
47                         }
48                 }
49
50                 /*
51                  * If there were no errors parse data.
52                  */
53                 if (c != EOF) {
54                         /* If newline has been found
55                          * return entire string and release the memory
56                          */
57                         if (c == 10) {
58                                 fprintf(stdout, "%s\n", buffer);
59
60                                 buffer_length = 0;
61                                 i = 0;
62
63                                 free(buffer);
64                                 buffer = NULL;
65                                 tmp = NULL;
66                         }
67                         /*
68                          * Normal character, add it to the buffer
69                          */
70                         else {
71                                 buffer = tmp;
72                                 buffer[i] = c;
73
74                                 /* Used for debug..*/
75                                 /*
76                                 printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
77                                 */
78                                 i++;
79                         }
80                 }
81         } while(c != EOF);
82         
83         if (buffer) {
84                 free(buffer);
85                 buffer = NULL;
86         }
87
88         return 0;
89 }