diff options
Diffstat (limited to 'preproc.c')
-rw-r--r-- | preproc.c | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -1508,7 +1508,7 @@ static bool in_list(const StrList *list, const char *str) * the end of the path. */ static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, - bool missing_ok, const char *mode) + char **found_path, bool missing_ok, const char *mode) { FILE *fp; char *prefix = ""; @@ -1516,11 +1516,20 @@ static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, int len = strlen(file); size_t prefix_len = 0; StrList *sl; + size_t path_len; while (1) { - sl = nasm_malloc(prefix_len+len+1+sizeof sl->next); + path_len = prefix_len + len + 1; + + sl = nasm_malloc(path_len + sizeof sl->next); memcpy(sl->str, prefix, prefix_len); memcpy(sl->str+prefix_len, file, len+1); + + if (found_path != NULL) { + *found_path = nasm_malloc(path_len); + memcpy(*found_path, sl->str, path_len); + } + fp = fopen(sl->str, mode); if (fp && dhead && !in_list(*dhead, sl->str)) { sl->next = NULL; @@ -1531,6 +1540,12 @@ static FILE *inc_fopen(const char *file, StrList **dhead, StrList ***dtail, } if (fp) return fp; + + if (found_path != NULL && *found_path != NULL) { + nasm_free(*found_path); + *found_path == NULL; + } + if (!ip) { if (!missing_ok) break; @@ -1569,7 +1584,7 @@ FILE *pp_input_fopen(const char *filename, const char *mode) StrList *xsl = NULL; StrList **xst = &xsl; - fp = inc_fopen(filename, &xsl, &xst, true, mode); + fp = inc_fopen(filename, &xsl, &xst, NULL, true, mode); if (xsl) nasm_free(xsl); return fp; @@ -2174,7 +2189,7 @@ static int do_directive(Token * tline) bool casesense; int k, m; int offset; - char *p, *pp; + char *p, *pp, *found_path; const char *mname; Include *inc; Context *ctx; @@ -2516,12 +2531,13 @@ static int do_directive(Token * tline) inc = nasm_malloc(sizeof(Include)); inc->next = istk; inc->conds = NULL; - inc->fp = inc_fopen(p, dephead, &deptail, pass == 0, "r"); + found_path = NULL; + inc->fp = inc_fopen(p, dephead, &deptail, &found_path, pass == 0, "r"); if (!inc->fp) { /* -MG given but file not found */ nasm_free(inc); } else { - inc->fname = src_set_fname(p); + inc->fname = src_set_fname(found_path ? found_path : p); inc->lineno = src_set_linnum(0); inc->lineinc = 1; inc->expansion = NULL; @@ -3259,7 +3275,7 @@ issue_error: if (t->type != TOK_INTERNAL_STRING) nasm_unquote(p, NULL); - fp = inc_fopen(p, &xsl, &xst, true, "r"); + fp = inc_fopen(p, &xsl, &xst, NULL, true, "r"); if (fp) { p = xsl->str; fclose(fp); /* Don't actually care about the file */ |