Here's a slightly more specific diff avoiding useless stat(2)

Index: engine.c
===================================================================
RCS file: /cvs/src/usr.bin/make/engine.c,v
retrieving revision 1.70
diff -u -p -r1.70 engine.c
--- engine.c    25 Oct 2021 19:54:29 -0000      1.70
+++ engine.c    28 May 2023 16:07:44 -0000
@@ -84,6 +84,7 @@
 #include "extern.h"
 #include "lst.h"
 #include "timestamp.h"
+#include "main.h"
 #include "make.h"
 #include "pathnames.h"
 #include "error.h"
@@ -210,6 +211,7 @@ node_failure(GNode *gn)
                fflush(stdout);
        else {
                print_errors();
+               dump_unreadable();
                Punt(NULL);
        }
 }
Index: main.c
===================================================================
RCS file: /cvs/src/usr.bin/make/main.c,v
retrieving revision 1.129
diff -u -p -r1.129 main.c
--- main.c      17 Jan 2023 13:03:22 -0000      1.129
+++ main.c      28 May 2023 16:07:45 -0000
@@ -87,6 +87,8 @@ bool          ignoreErrors;   /* -i flag */
 bool           beSilent;       /* -s flag */
 bool           dumpData;       /* -p flag */
 
+static LIST    unreadable;
+
 struct dirs {
        char *current;
        char *object;
@@ -826,6 +828,48 @@ main(int argc, char **argv)
                return 0;
 }
 
+struct unreadable {
+       char *fname;
+       int errcode;
+};
+
+void
+dump_unreadable(void)
+{
+       struct unreadable *u;
+
+       if (Lst_IsEmpty(&unreadable))
+               return;
+
+       fprintf(stderr, "Makefile(s) that couldn't be read:\n");
+
+       while ((u = Lst_Pop(&unreadable))) {
+               fprintf(stderr, "\t%s: %s\n", u->fname, strerror(u->errcode));
+               free(u->fname);
+               free(u);
+       }
+}
+
+static FILE *
+open_makefile(const char *fname)
+{
+       FILE *stream;
+       struct unreadable *u;
+
+       stream = fopen(fname, "r");
+       if (stream != NULL)
+               return stream;
+
+       if (errno != ENOENT) {
+               u = emalloc(sizeof *u);
+               u->fname = strdup(fname);
+               u->errcode = errno;
+               Lst_AtEnd(&unreadable, u);
+       }
+
+       return NULL;
+}
+
 /*-
  * ReadMakefile  --
  *     Open and parse the given makefile.
@@ -848,14 +892,14 @@ ReadMakefile(void *p, void *q)
                Var_Set("MAKEFILE", "");
                Parse_File(estrdup("(stdin)"), stdin);
        } else {
-               if ((stream = fopen(fname, "r")) != NULL)
+               if ((stream = open_makefile(fname)) != NULL)
                        goto found;
                /* if we've chdir'd, rebuild the path name */
                if (d->current != d->object && *fname != '/') {
                        char *path;
 
                        path = Str_concat(d->current, fname, '/');
-                       if ((stream = fopen(path, "r")) == NULL)
+                       if ((stream = open_makefile(path)) == NULL)
                                free(path);
                        else {
                                fname = path;
@@ -866,7 +910,14 @@ ReadMakefile(void *p, void *q)
                name = Dir_FindFile(fname, userIncludePath);
                if (!name)
                        name = Dir_FindFile(fname, systemIncludePath);
-               if (!name || !(stream = fopen(name, "r")))
+               if (!name)
+                       return false;
+               /* do not try to open a file we already have, so that
+                * dump_unreadable() yields non-confusing results.
+                */
+               if (strcmp(name, fname) == 0)
+                       return false;
+               if ((stream = open_makefile(name)) == NULL)
                        return false;
                fname = name;
                /*
Index: main.h
===================================================================
RCS file: /cvs/src/usr.bin/make/main.h,v
retrieving revision 1.6
diff -u -p -r1.6 main.h
--- main.h      13 Jan 2020 14:51:50 -0000      1.6
+++ main.h      28 May 2023 16:07:45 -0000
@@ -41,4 +41,8 @@ extern Lst    create;
 /* set_notparallel(): used to influence running mode from parse.c */
 extern void set_notparallel(void);
 
+/* dump_unreadable: in case of some errors, dump makefile names
+ * we found but are unable to read.
+ */
+extern void dump_unreadable(void);
 #endif
Index: parse.c
===================================================================
RCS file: /cvs/src/usr.bin/make/parse.c,v
retrieving revision 1.134
diff -u -p -r1.134 parse.c
--- parse.c     6 Mar 2021 08:31:42 -0000       1.134
+++ parse.c     28 May 2023 16:07:45 -0000
@@ -1696,6 +1696,7 @@ void
 Parse_MainName(Lst listmain)   /* result list */
 {
        if (mainNode == NULL) {
+               dump_unreadable();
                Punt("no target to make.");
                /*NOTREACHED*/
        } else if (mainNode->type & OP_DOUBLEDEP) {

Reply via email to