diff --git a/include/util/input_file.h b/include/util/input_file.h index 3225772..3b762b4 100644 --- a/include/util/input_file.h +++ b/include/util/input_file.h @@ -15,11 +15,7 @@ typedef struct { int (*handle)(input_file_t *f, void *obj); } keyword_handler_t; -void cleanup_file(input_file_t *f); - -int open_file(input_file_t *f, const char *filename); - -int process_file(input_file_t *f, const keyword_handler_t *handlers, +int process_file(const char *filename, const keyword_handler_t *handlers, size_t count, void *obj); void input_file_complain(const input_file_t *f, const char *msg); diff --git a/lib/util/input_file.c b/lib/util/input_file.c index 9ecfa6f..b89565f 100644 --- a/lib/util/input_file.c +++ b/lib/util/input_file.c @@ -57,47 +57,37 @@ retry: return 0; } -void cleanup_file(input_file_t *f) -{ - fclose(f->f); - free(f->line); -} - -int open_file(input_file_t *f, const char *filename) -{ - memset(f, 0, sizeof(*f)); - - f->filename = filename; - f->f = fopen(filename, "r"); - - if (f->f == NULL) { - perror(f->filename); - return -1; - } - - return 0; -} - void input_file_complain(const input_file_t *f, const char *msg) { fprintf(stderr, "%s: %zu: %s\n", f->filename, f->linenum, msg); } -int process_file(input_file_t *f, const keyword_handler_t *handlers, +int process_file(const char *filename, const keyword_handler_t *handlers, size_t count, void *obj) { + input_file_t f; size_t i, len; char *ptr; int ret; + memset(&f, 0, sizeof(f)); + + f.filename = filename; + f.f = fopen(filename, "r"); + + if (f.f == NULL) { + perror(f.filename); + return -1; + } + for (;;) { - ret = prefetch_line(f); + ret = prefetch_line(&f); if (ret < 0) - return -1; + goto fail; if (ret > 0) break; - ptr = f->line; + ptr = f.line; for (i = 0; i < count; ++i) { len = strlen(handlers[i].name); @@ -108,19 +98,25 @@ int process_file(input_file_t *f, const keyword_handler_t *handlers, continue; for (ptr += len; isspace(*ptr); ++ptr) ; - memmove(f->line, ptr, strlen(ptr) + 1); + memmove(f.line, ptr, strlen(ptr) + 1); break; } if (i == count) { fprintf(stderr, "%s: %zu: unknown keyword\n", - f->filename, f->linenum); - return -1; + f.filename, f.linenum); + goto fail; } - if (handlers[i].handle(f, obj)) - return -1; + if (handlers[i].handle(&f, obj)) + goto fail; } + fclose(f.f); + free(f.line); return 0; +fail: + fclose(f.f); + free(f.line); + return -1; } diff --git a/main/cmd/pack/desc.c b/main/cmd/pack/desc.c index 732b696..bde1a66 100644 --- a/main/cmd/pack/desc.c +++ b/main/cmd/pack/desc.c @@ -97,21 +97,13 @@ static compressor_t *get_default_compressor(void) int desc_read(const char *path, pkg_desc_t *desc) { - input_file_t f; char *ptr; memset(desc, 0, sizeof(*desc)); - if (open_file(&f, path)) + if (process_file(path, line_hooks, NUM_LINE_HOOKS, desc)) return -1; - if (process_file(&f, line_hooks, NUM_LINE_HOOKS, desc)) { - cleanup_file(&f); - return -1; - } - - cleanup_file(&f); - if (desc->datacmp == NULL) desc->datacmp = get_default_compressor(); diff --git a/main/cmd/pack/filelist.c b/main/cmd/pack/filelist.c index 73211b4..24f8952 100644 --- a/main/cmd/pack/filelist.c +++ b/main/cmd/pack/filelist.c @@ -281,12 +281,8 @@ static int alloc_file_ids(image_entry_t *list) int filelist_read(const char *filename, image_entry_t **out) { image_entry_t *list = NULL; - input_file_t f; - if (open_file(&f, filename)) - return -1; - - if (process_file(&f, line_hooks, NUM_LINE_HOOKS, &list)) + if (process_file(filename, line_hooks, NUM_LINE_HOOKS, &list)) goto fail; if (list != NULL) { @@ -296,11 +292,9 @@ int filelist_read(const char *filename, image_entry_t **out) goto fail; } - cleanup_file(&f); *out = list; return 0; fail: - cleanup_file(&f); image_entry_free_list(list); return -1; }