aboutsummaryrefslogtreecommitdiff
path: root/bb.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-11-06 17:21:44 +0100
committerBruce Hill <bruce@bruce-hill.com>2019-11-06 17:21:44 +0100
commitd577fa5a5608350d94c14a53c2c8116dd7306462 (patch)
tree346da2843f8cfe4d633e3671171a057da82709e9 /bb.c
parentaf82c6316a51ad890636211fbbca932dae7f9022 (diff)
Better error handling/reporting
Diffstat (limited to 'bb.c')
-rw-r--r--bb.c22
1 files changed, 16 insertions, 6 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, "<selection>") == 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, "<selection>") != 0) {