aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/cleanpath.c8
-rw-r--r--src/main.c9
2 files changed, 14 insertions, 3 deletions
diff --git a/lib/cleanpath.c b/lib/cleanpath.c
index 5f7db0e..468c610 100644
--- a/lib/cleanpath.c
+++ b/lib/cleanpath.c
@@ -103,16 +103,22 @@ char *cleanpath_read(struct CleanPath *path) {
char *result;
size_t result_len;
size_t part_count;
+ size_t empty;
result = calloc(path->data_len, sizeof(char));
if (result == NULL) {
goto cleanpath_read__failure;
}
+ // Update number of elements in the path post-filter
+ empty = 0;
for (part_count = 0; path->part[part_count] != NULL; part_count++) {
- if (!strlen(path->part[part_count]))
+ if (!strlen(path->part[part_count])) {
+ empty++;
continue;
+ }
}
+ path->part_nelem = part_count - empty;
for (size_t i = 0; i < part_count; i++) {
// Ignore filtered paths
diff --git a/src/main.c b/src/main.c
index e55fa72..7689035 100644
--- a/src/main.c
+++ b/src/main.c
@@ -249,8 +249,13 @@ int main(int argc, char *argv[], char *arge[]) {
if (do_all_sys_vars) {
for (size_t i = 0; environ[i] != NULL; i++) {
char *var = getenv_ex(environ[i]);
+ if (!var) {
+ continue;
+ }
+
path = cleanpath_init(var, sep);
if (path == NULL) {
+ fprintf(stderr, "Unexpected error. Invalid data consumed by cleanpath_init(\"%s\", \"%s\")\n", environ[i], sep);
exit(1);
}
@@ -276,8 +281,8 @@ int main(int argc, char *argv[], char *arge[]) {
if (do_listing) {
show_listing(path);
} else {
- char *data = get_path(path);
- if (strlen(path->data) == 0 && dcount < sizeof(deferred) / sizeof(*deferred)) {
+ char *data = cleanpath_read(path);
+ if (!path->part_nelem && dcount < sizeof(deferred) / sizeof(*deferred)) {
deferred[dcount] = strdup(key);
dcount++;
} else {