Created first program.
authorRafał Długołęcki <rafal@dlugolecki.net.pl>
Mon, 16 Jun 2014 21:42:53 +0000 (23:42 +0200)
committerRafał Długołęcki <rafal@dlugolecki.net.pl>
Mon, 16 Jun 2014 21:42:53 +0000 (23:42 +0200)
Makefile.am
configure.ac
src/process1.c [new file with mode: 0644]

index 43d88669bbb1be002f3db1aedb0a1c1e791c5150..35b3782bfba64bf7126da0f79cfdab4f03239268 100644 (file)
@@ -1,9 +1,8 @@
 AUTOMAKE_OPTIONS = gnu subdir-objects
 ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS}
 
-bin_PROGRAMS = bin/signals
-
-bin_signals_SOURCES = \
-       src/main.c
-
+bin_PROGRAMS = \
+       bin/process1
 
+bin_process1_SOURCES = \
+       src/process1.c
index de441367816c659af53a9f5f4a071d43da4a7975..9b86d534c1017adaa4488b79c99c4175c35aa7d4 100644 (file)
@@ -6,3 +6,5 @@ AC_CONFIG_HEADERS([config.h])
 AC_PROG_CC
 AM_PROG_CC_C_O
 AC_CONFIG_FILES([Makefile])
+
+AC_OUTPUT
\ No newline at end of file
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