commit: 530f0fc4f8110f461e796e83934ee132893089e5
parent 875b0a4d6e3750c482341a2bd78e266bf8154947
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 6 Jun 2025 06:38:59 +0200
cmd/which: handle unexpected errors from access(3)
Diffstat:
1 file changed, 15 insertions(+), 0 deletions(-)
diff --git a/cmd/which.c b/cmd/which.c
@@ -6,6 +6,7 @@
#include "../lib/getopt_nolong.h"
#include "../lib/strchrnul.h"
+#include <errno.h>
#include <limits.h> // PATH_MAX
#include <stdbool.h>
#include <stdio.h> // fprintf
@@ -84,6 +85,7 @@ main(int argc, char *argv[])
buflen += cmdlen;
buf[buflen] = '\0';
+ errno = 0;
if(access(buf, X_OK) == 0)
{
if(!opt_s) puts(buf);
@@ -93,6 +95,19 @@ main(int argc, char *argv[])
if(!opt_a) break;
}
+ switch(errno)
+ {
+ case ENOENT:
+ case 0:
+ break;
+ default:
+ fprintf(stderr,
+ "which: warning: Failed checking access on file '%s': %s\n",
+ buf,
+ strerror(errno));
+ break;
+ }
+
which_cont:
if(*stop == '\0') break;
prev = stop + 1;