Browse Source

+ filelist.[ch]

master
Alex 'AdUser' Z 8 years ago
parent
commit
252d8e6c6c
  1. 2
      src/CMakeLists.txt
  2. 49
      src/filelist.c
  3. 10
      src/filelist.h

2
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})

49
src/filelist.c

@ -0,0 +1,49 @@
#include <glob.h>
#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;
}

10
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_ */
Loading…
Cancel
Save