aboutsummaryrefslogtreecommitdiff
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
parente248f5b2a8254d20ded7685e426e53ac90940cba (diff)
Fixed two bugs: mmapped files weren't getting closed, and filenames with
%s-style formatting strings caused issues.
-rw-r--r--bp.c18
-rw-r--r--files.c11
-rw-r--r--files.h4
3 files changed, 22 insertions, 11 deletions
diff --git a/bp.c b/bp.c
index 4624b4d..65150c2 100644
--- a/bp.c
+++ b/bp.c
@@ -3,6 +3,7 @@
//
// See `man ./bp.1` for more details
//
+#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
@@ -350,7 +351,10 @@ static int print_matches(def_t *defs, file_t *f, pat_t *pattern)
static int process_file(def_t *defs, const char *filename, pat_t *pattern)
{
file_t *f = load_file(NULL, filename);
- check(f, "Could not open file: %s", filename);
+ if (f == NULL) {
+ fprintf(stderr, "Could not open file: %s\n%s\n", filename, strerror(errno));
+ return 0;
+ }
int matches = 0;
if (mode == MODE_EXPLAIN) {
@@ -415,9 +419,9 @@ int main(int argc, char *argv[])
pat_t *pattern = NULL;
// Load builtins:
- file_t *xdg_file = load_file(&loaded_files, "/etc/xdg/"BP_NAME"/builtins.bp");
+ file_t *xdg_file = load_filef(&loaded_files, "/etc/xdg/"BP_NAME"/builtins.bp");
if (xdg_file) defs = load_grammar(defs, xdg_file);
- file_t *local_file = load_file(&loaded_files, "%s/.config/"BP_NAME"/builtins.bp", getenv("HOME"));
+ file_t *local_file = load_filef(&loaded_files, "%s/.config/"BP_NAME"/builtins.bp", getenv("HOME"));
if (local_file) defs = load_grammar(defs, local_file);
int i, git = 0;
@@ -454,9 +458,9 @@ int main(int argc, char *argv[])
} else if (FLAG("-g") || FLAG("--grammar")) {
file_t *f = load_file(&loaded_files, flag);
if (f == NULL)
- f = load_file(&loaded_files, "%s/.config/"BP_NAME"/%s.bp", getenv("HOME"), flag);
+ f = load_filef(&loaded_files, "%s/.config/"BP_NAME"/%s.bp", getenv("HOME"), flag);
if (f == NULL)
- f = load_file(&loaded_files, "/etc/xdg/"BP_NAME"/%s.bp", flag);
+ f = load_filef(&loaded_files, "/etc/xdg/"BP_NAME"/%s.bp", flag);
check(f != NULL, "Couldn't find grammar: %s", flag);
defs = load_grammar(defs, f); // Keep in memory for debug output
} else if (FLAG("-p") || FLAG("--pattern")) {
@@ -592,9 +596,7 @@ int main(int argc, char *argv[])
// Files pass in as command line args:
struct stat statbuf;
for (int nfiles = 0; i < argc; nfiles++, i++) {
- check(stat(argv[i], &statbuf) == 0,
- "File does not exist: %s", argv[i]);
- if (S_ISDIR(statbuf.st_mode)) // Symlinks are okay if manually specified
+ if (stat(argv[i], &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) // Symlinks are okay if manually specified
found += process_dir(defs, argv[i], pattern);
else
found += process_file(defs, argv[i], pattern);
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) {
diff --git a/files.h b/files.h
index cee341c..c7176b8 100644
--- a/files.h
+++ b/files.h
@@ -17,8 +17,10 @@ typedef struct file_s {
unsigned int mmapped:1;
} file_t;
+__attribute__((nonnull(2)))
+file_t *load_file(file_t **files, const char *filename);
__attribute__((format(printf,2,3)))
-file_t *load_file(file_t **files, const char *fmt, ...);
+file_t *load_filef(file_t **files, const char *fmt, ...);
__attribute__((nonnull(3), returns_nonnull))
file_t *spoof_file(file_t **files, const char *filename, const char *text);
__attribute__((nonnull))