Created first program.
[wsti_so.git] / src / process1.c
diff --git a/src/process1.c b/src/process1.c
new file mode 100644 (file)
index 0000000..695e887
--- /dev/null
@@ -0,0 +1,89 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/** If buffer is too small to hold entire string, it is incremented by this value */
+#define BUFFER_STEP 16
+
+/**
+ * Program reads entire lines of text from the standard input and returns them
+ * on the standard output without changing anything.
+ */
+int main(void) {
+       /** Currently fetched from stdin character */
+       int c;
+
+       /** Buffer used to store line of characters */
+       char * buffer = NULL;
+
+       /** Current buffer length*/
+       int buffer_length = 0;
+
+       /* Index of the current character */
+       int i = 0;
+       
+       /** Temporary buffer used as a proxy between
+        * checking memory allocation and copying data to real buffer 
+        */
+       char *tmp = NULL;
+
+       do {
+               c = fgetc(stdin);
+
+               /*
+                * Check if current index is bigger than current buffer size.
+                * If so increment buffer size. On error release memory, and set
+                * appropriate flags.
+                */
+               if (i >= buffer_length) {
+                       tmp = NULL;
+                       buffer_length += BUFFER_STEP;
+                       tmp = (char*) realloc(buffer, buffer_length);
+                       if (tmp == NULL) {
+                               fprintf(stderr, "[%s] Memory allocation problem on read!\n", "process1");
+                               free(buffer);
+                               buffer = NULL;
+                               c = EOF;
+                       }
+               }
+
+               /*
+                * If there were no errors parse data.
+                */
+               if (c != EOF) {
+                       /* If newline has been found
+                        * return entire string and release the memory
+                        */
+                       if (c == 10) {
+                               fprintf(stdout, "%s\n", buffer);
+
+                               buffer_length = 0;
+                               i = 0;
+
+                               free(buffer);
+                               buffer = NULL;
+                               tmp = NULL;
+                       }
+                       /*
+                        * Normal character, add it to the buffer
+                        */
+                       else {
+                               buffer = tmp;
+                               buffer[i] = c;
+
+                               /* Used for debug..*/
+                               /*
+                               printf("c: %c/%d, i: %d, bl: %d\n", c, c, i, buffer_length);
+                               */
+                               i++;
+                       }
+               }
+       } while(c != EOF);
+       
+       if (buffer) {
+               free(buffer);
+               buffer = NULL;
+       }
+
+       return 0;
+}
\ No newline at end of file