From c34590c4bf067d4e749e5298a8ce9f90072d6332 Mon Sep 17 00:00:00 2001 From: Will Estes Date: Sat, 12 Dec 2015 11:03:05 -0500 Subject: [PATCH] Made search for m4 more explicit. --- src/flexdef.h | 3 +++ src/main.c | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/flexdef.h b/src/flexdef.h index 9c6baec..3aa63df 100644 --- a/src/flexdef.h +++ b/src/flexdef.h @@ -66,6 +66,9 @@ #ifdef HAVE_SYS_PARAMS_H #include #endif +#ifdef HAVE_SYS_STAT_H +#include +#endif #ifdef HAVE_SYS_WAIT_H #include #endif diff --git a/src/main.c b/src/main.c index 5fadfc4..c276bc4 100644 --- a/src/main.c +++ b/src/main.c @@ -359,8 +359,46 @@ void check_options (void) /* Setup the filter chain. */ output_chain = filter_create_int(NULL, filter_tee_header, headerfilename); - if ( !(m4 = getenv("M4"))) - m4 = M4; + if ( !(m4 = getenv("M4"))) { + char *slash; + m4 = M4; + if (slash = strrchr(M4, '/')) { + m4 = slash+1; + /* break up $PATH */ + const char *path = getenv("PATH"); + if (!path) { + m4 = M4; + } else { + do { + char m4_path[PATH_MAX]; + int length = strlen(path); + struct stat sbuf; + + const char *endOfDir = strchr(path, ':'); + if (!endOfDir) + endOfDir = path+length; + + if ((endOfDir-path+2) >= sizeof(m4_path)) { + path = endOfDir+1; + continue; + } + + strncpy(m4_path, path, sizeof(m4_path)); + m4_path[endOfDir-path] = '/'; + m4_path[endOfDir-path+1] = '\0'; + strncat(m4_path, m4, sizeof(m4_path)); + if (stat(m4_path, &sbuf) == 0 && + (S_ISREG(sbuf.st_mode)) && sbuf.st_mode & S_IXUSR) { + m4 = strdup(m4_path); + break; + } + path = endOfDir+1; + } while (path[0]); + if (!path[0]) + m4 = M4; + } + } + } filter_create_ext(output_chain, m4, "-P", 0); filter_create_int(output_chain, filter_fix_linedirs, NULL); -- 2.40.0