aboutsummaryrefslogtreecommitdiff
path: root/files.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2021-01-17 18:06:00 -0800
committerBruce Hill <bruce@bruce-hill.com>2021-01-17 18:06:00 -0800
commitc6dd967c7062625ba0d473f8f6088142c16ed6b0 (patch)
tree112de705619ffeea42f045f3377d15c52a066fab /files.c
parente248f5b2a8254d20ded7685e426e53ac90940cba (diff)
Fixed two bugs: mmapped files weren't getting closed, and filenames with
%s-style formatting strings caused issues.
Diffstat (limited to 'files.c')
-rw-r--r--files.c11
1 files changed, 9 insertions, 2 deletions
diff --git a/files.c b/files.c
index 9e38c57..7ab3dd2 100644
--- a/files.c
+++ b/files.c
@@ -43,7 +43,7 @@ static void populate_lines(file_t *f)
//
// Read an entire file into memory.
//
-file_t *load_file(file_t **files, const char *fmt, ...)
+file_t *load_filef(file_t **files, const char *fmt, ...)
{
char filename[PATH_MAX+1] = {0};
va_list args;
@@ -51,7 +51,14 @@ file_t *load_file(file_t **files, const char *fmt, ...)
check(vsnprintf(filename, PATH_MAX, fmt, args) <= PATH_MAX,
"File name is too large");
va_end(args);
+ return load_file(files, filename);
+}
+//
+// Read an entire file into memory.
+//
+file_t *load_file(file_t **files, const char *filename)
+{
int fd = filename[0] == '\0' ? STDIN_FILENO : open(filename, O_RDONLY);
if (fd < 0) return NULL;
size_t length;
@@ -81,9 +88,9 @@ file_t *load_file(file_t **files, const char *fmt, ...)
if (length >= capacity)
f->contents = xrealloc(f->contents, sizeof(char)*(capacity *= 2) + 1);
}
- if (fd != STDIN_FILENO) close(fd);
finished_loading:
+ if (fd != STDIN_FILENO) close(fd);
f->end = &f->contents[length];
populate_lines(f);
if (files != NULL) {