if (!j)
return -ENOMEM;
- if (is_dir(j, true))
- continue;
-
if (access(j, X_OK) >= 0) {
- /* Found it! */
+ _cleanup_free_ char *with_dash;
- if (ret)
- *ret = path_simplify(TAKE_PTR(j), false);
+ with_dash = strjoin(j, "/");
+ if (!with_dash)
+ return -ENOMEM;
- return 0;
+ /* If this passes, it must be a directory, and so should be skipped. */
+ if (access(with_dash, X_OK) >= 0)
+ continue;
+
+ /**
+ * We can't just `continue` inverting this case, since we need to update last_error.
+ */
+ if (errno == ENOTDIR) {
+ /* Found it! */
+ if (ret)
+ *ret = path_simplify(TAKE_PTR(j), false);
+
+ return 0;
+ }
}
/* PATH entries which we don't have access to are ignored, as per tradition. */