From 252d8e6c6c3df4d624e346d1e9b6f843b6c54226 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Thu, 25 Feb 2016 22:44:04 +1000 Subject: [PATCH] + filelist.[ch] --- src/CMakeLists.txt | 2 +- src/filelist.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++ src/filelist.h | 10 ++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 src/filelist.c create mode 100644 src/filelist.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c14c823..62eb476 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(SOURCES "main.c" "logfile.c" "log.c" "matches.c" "ipaddr.c") +set(SOURCES "main.c" "logfile.c" "log.c" "matches.c" "ipaddr.c" "filelist.c") add_executable(f2b ${SOURCES}) diff --git a/src/filelist.c b/src/filelist.c new file mode 100644 index 0000000..20a89d8 --- /dev/null +++ b/src/filelist.c @@ -0,0 +1,49 @@ +#include + +#include "common.h" +#include "logfile.h" +#include "filelist.h" +#include "log.h" + +f2b_logfile_t * +f2b_filelist_append(f2b_logfile_t *list, f2b_logfile_t *file) { + assert(file != NULL); + + if (list != NULL) + return file->next = list; + return file; +} + +void +f2b_filelist_apply(f2b_logfile_t *list, void (*cb)(f2b_logfile_t *, void *), void *arg) { + assert(cb != NULL); + + for (; list != NULL; list = list->next) + (*cb)(list, arg); +} + +f2b_logfile_t * +f2b_filelist_from_glob(const char *pattern) { + f2b_logfile_t *file = NULL; + f2b_logfile_t *files = NULL; + glob_t globbuf; + + assert(pattern != NULL); + + if (glob(pattern, GLOB_MARK | GLOB_NOESCAPE, NULL, &globbuf) != 0) + return NULL; + + for (size_t i = 0; i < globbuf.gl_pathc; i++) { + if ((file = calloc(1, sizeof(f2b_logfile_t))) == NULL) + continue; + if (f2b_logfile_open(file, globbuf.gl_pathv[i]) == false) { + log_msg(log_error, "can't open file: %s: %s", globbuf.gl_pathv[i], strerror(errno)); + FREE(file); + continue; + } + files = f2b_filelist_append(files, file); + } + + globfree(&globbuf); + return files; +} diff --git a/src/filelist.h b/src/filelist.h new file mode 100644 index 0000000..ac2f554 --- /dev/null +++ b/src/filelist.h @@ -0,0 +1,10 @@ +#ifndef FILELIST_H_ +#define FILELIST_H_ + +f2b_logfile_t * +f2b_filelist_from_glob(const char *pattern); + +f2b_logfile_t * +f2b_filelist_append(f2b_logfile_t *list, f2b_logfile_t *file); + +#endif /* FILELIST_H_ */