FILE * m4_path_search (const char *file, char **result) { FILE *fp; includes *incl; char *name; /* buffer for constructed name */ int e; if (result) *result = NULL; /* Reject empty file. */ if (!*file) { errno = ENOENT; return NULL; } /* Look in current working directory first. */ fp = m4_fopen (file); if (fp != NULL) { if (result) *result = xstrdup (file); return fp; } /* If file not found, and filename absolute, fail. */ if (IS_ABSOLUTE_FILE_NAME (file) || no_gnu_extensions) return NULL; e = errno; for (incl = dir_list; incl != NULL; incl = incl->next) { name = mfile_name_concat (incl->dir, file, NULL); #ifdef DEBUG_INCL xfprintf (stderr, "m4_path_search (%s) -- trying %s\n", file, name); #endif fp = m4_fopen (name); if (fp != NULL) { if (debug_level & DEBUG_TRACE_PATH) DEBUG_MESSAGE2 ("path search for `%s' found `%s'", file, name); if (result) *result = name; else free (name); return fp; } free (name); } errno = e; return fp; }
/* Generic load function. Push the input file or load the module named FILENAME, if it can be found in the search path. Complain about inaccesible files iff SILENT is false. */ bool m4_load_filename (m4 *context, const m4_call_info *caller, const char *filename, m4_obstack *obs, bool silent) { char *filepath = NULL; char *suffix = NULL; bool new_input = false; if (m4_get_posixly_correct_opt (context)) { if (access (filename, R_OK) == 0) filepath = xstrdup (filename); } else filepath = m4_path_search (context, filename, FILE_SUFFIXES); if (filepath) suffix = strrchr (filepath, '.'); if (!m4_get_posixly_correct_opt (context) && suffix && STREQ (suffix, LT_MODULE_EXT)) { m4_module_load (context, filename, obs); } else { FILE *fp = NULL; if (filepath) fp = m4_fopen (context, filepath, "r"); if (fp == NULL) { if (!silent) m4_error (context, 0, errno, caller, _("cannot open file '%s'"), filename); free (filepath); return false; } m4_push_file (context, fp, filepath, true); new_input = true; } free (filepath); return new_input; }