From b3dda0980bfa04a9041327ec5aad5c1845f0620f Mon Sep 17 00:00:00 2001 From: David Phillips Date: Mon, 26 Oct 2015 23:17:00 +1300 Subject: Initial commit --- .gitignore | 2 ++ Makefile | 3 +++ config.mk | 1 + match_pool.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 config.mk create mode 100644 match_pool.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2450a3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +match_pool +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a0e6cb9 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +include config.mk + +all: match_pool diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..e8c9b31 --- /dev/null +++ b/config.mk @@ -0,0 +1 @@ +CFLAGS += -Wall -Wextra -Werror diff --git a/match_pool.c b/match_pool.c new file mode 100644 index 0000000..6df6af8 --- /dev/null +++ b/match_pool.c @@ -0,0 +1,49 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + int i = 0; + char buffer[1024]; + char *res = NULL; + char *pool = NULL; + char *local_pool = NULL; + size_t pool_size = 0; + + + if (argc != 2) + { + fprintf(stderr, "Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + pool = argv[1]; + pool_size = strlen(pool); + + local_pool = malloc(pool_size); + if (local_pool == NULL) + { + fprintf(stderr, "%s: malloc for local pool failed\n", argv[0]); + return EXIT_FAILURE; + } + + while (fgets(buffer, sizeof(buffer), stdin)) + { + buffer[strcspn(buffer, "\r\n")] = '\0'; /* chomp */ + + if (strlen(buffer) == 0) + continue; + + strcpy(local_pool, pool); + for (i = strlen(buffer)-1; i >= 0; i--) + { + if ((res = memchr(local_pool, buffer[i], pool_size)) != NULL) + *res = '\0'; + else + break; + } + printf("%s %s\n", buffer, i < 0? "matches" : "doesn't match" ); + } + return 0; +} -- cgit v1.1