diff options
author | Will Estes <westes575@gmail.com> | 2015-12-12 11:03:05 -0500 |
---|---|---|
committer | Will Estes <westes575@gmail.com> | 2015-12-12 11:03:05 -0500 |
commit | c34590c4bf067d4e749e5298a8ce9f90072d6332 (patch) | |
tree | 546dfeb9e4324d180271a15e91369eba5d9f8140 /src/main.c | |
parent | f863c9490e6912ffcaeb12965fb3a567a10745ff (diff) |
Made search for m4 more explicit.
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 42 |
1 files changed, 40 insertions, 2 deletions
@@ -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); |