From fe306b90adcee352d3da4ba0842afac66ba3d751 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rafa=C5=82=20D=C5=82ugo=C5=82=C4=99cki?= Date: Mon, 16 Jun 2014 23:42:53 +0200 Subject: [PATCH] Created first program. --- Makefile.am | 9 +++-- configure.ac | 2 ++ src/process1.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/process1.c diff --git a/Makefile.am b/Makefile.am index 43d8866..35b3782 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/configure.ac b/configure.ac index de44136..9b86d53 100644 --- a/configure.ac +++ b/configure.ac @@ -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 index 0000000..695e887 --- /dev/null +++ b/src/process1.c @@ -0,0 +1,89 @@ +#include +#include +#include + +/** 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 -- 2.30.2