diff --git a/src/sources/files.c b/src/sources/files.c index a8c2abb..bdb2ee9 100644 --- a/src/sources/files.c +++ b/src/sources/files.c @@ -15,15 +15,15 @@ #define MODNAME "files" #define FREE(x) { free(x); x = NULL; } -#define FCLOSE(x) { fclose(x); x = NULL; } +#define FCLOSE(x) { if (x) fclose(x); x = NULL; } typedef struct f2b_file_t { struct f2b_file_t *next; uint32_t lines; uint32_t stag; - FILE *fd; - char *path; struct stat st; + FILE *fd; + char path[]; } f2b_file_t; struct _config { @@ -74,7 +74,6 @@ file_close(f2b_file_t *file) { assert(file != NULL); FCLOSE(file->fd); - FREE(file->path); file->lines = 0; memset(&file->st, 0, sizeof(struct stat)); } @@ -167,16 +166,13 @@ start(cfg_t *cfg) { return NULL; for (size_t i = 0; i < globbuf.gl_pathc; i++) { - if ((file = calloc(1, sizeof(f2b_file_t))) == NULL) { + size_t slen = strlen(globbuf.gl_pathv[i]) + 1; + if ((file = calloc(1, sizeof(f2b_file_t) + slen)) == NULL) { log_msg(cfg, error, "can't allocate memory: %s", strerror(errno)); continue; } - if ((file->path = strndup(globbuf.gl_pathv[i], PATH_MAX)) == NULL) { - FREE(file); - continue; - } + strlcpy(file->path, globbuf.gl_pathv[i], slen); if (!file_open(cfg, file)) { - FREE(file->path); FREE(file); continue; } @@ -217,7 +213,7 @@ next(cfg_t *cfg, char *buf, size_t bufsize, bool reset) { cfg->current = cfg->files; for (f2b_file_t *file = cfg->current; file != NULL; file = file->next) { - if (file_rotated(cfg, file)) + if (file->fd != NULL && file_rotated(cfg, file)) file_close(file); if (file->fd == NULL && !file_open(cfg, file)) continue;