From d577fa5a5608350d94c14a53c2c8116dd7306462 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Wed, 6 Nov 2019 17:21:44 +0100 Subject: Better error handling/reporting --- bb.c | 22 ++++++++++++++++------ bb.h | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/bb.c b/bb.c index bd086be..c8ffe4a 100644 --- a/bb.c +++ b/bb.c @@ -376,12 +376,12 @@ void* memcheck(void *p) * Prepend `root` to relative paths, replace "~" with $HOME. * The normalized path is stored in `normalized`. */ -void normalize_path(const char *root, const char *path, char *normalized) +char *normalize_path(const char *root, const char *path, char *normalized) { char pbuf[PATH_MAX] = {0}; if (path[0] == '~' && (path[1] == '\0' || path[1] == '/')) { char *home; - if (!(home = getenv("HOME"))) return; + if (!(home = getenv("HOME"))) return NULL; strcpy(pbuf, home); ++path; } else if (path[0] != '/') { @@ -389,8 +389,11 @@ void normalize_path(const char *root, const char *path, char *normalized) if (root[strlen(root)-1] != '/') strcat(pbuf, "/"); } strcat(pbuf, path); - if (realpath(pbuf, normalized) == NULL) + if (realpath(pbuf, normalized) == NULL) { strcpy(normalized, pbuf); // TODO: normalize better? + return NULL; + } + return normalized; } /* @@ -413,12 +416,19 @@ int populate_files(bb_t *bb, const char *path) } else if (strcmp(path, "..") == 0 && strcmp(bb->path, "") == 0) { if (!bb->prev_path[0]) return -1; strcpy(pbuf, bb->prev_path); - if (chdir(pbuf)) return -1; + if (chdir(pbuf)) { + warn("Could not cd to: \"%s\"", pbuf); + return -1; + } } else { - normalize_path(bb->path, path, pbuf); + if (!normalize_path(bb->path, path, pbuf)) + warn("Could not normalize path: \"%s\"", path); if (pbuf[strlen(pbuf)-1] != '/') strcat(pbuf, "/"); - if (chdir(pbuf)) return -1; + if (chdir(pbuf)) { + warn("Could not cd to: \"%s\"", pbuf); + return -1; + } } if (strcmp(bb->path, "") != 0) { diff --git a/bb.h b/bb.h index 34da658..9db73a4 100644 --- a/bb.h +++ b/bb.h @@ -206,7 +206,7 @@ static int is_simple_bbcmd(const char *s); static entry_t* load_entry(bb_t *bb, const char *path, int clear_dots); static inline int matches_cmd(const char *str, const char *cmd); static void* memcheck(void *p); -static void normalize_path(const char *root, const char *path, char *pbuf); +static char* normalize_path(const char *root, const char *path, char *pbuf); static int populate_files(bb_t *bb, const char *path); static void print_bindings(int fd); static void run_bbcmd(bb_t *bb, const char *cmd); -- cgit v1.2.3