From 72a6cb64b0aded790e44d97e71434a4eddb91f15 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Wed, 7 Dec 2022 10:11:04 -0800 Subject: Cache filename after realpath() processing Avoid having to make an additional system call for every time we compare full path names. Signed-off-by: Alan Coopersmith --- def.h | 4 +++- include.c | 24 +++++++++++++++++------- main.c | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/def.h b/def.h index c5a5830..9cbbe61 100644 --- a/def.h +++ b/def.h @@ -112,6 +112,7 @@ struct symtab { struct inclist { char *i_incstring; /* string from #include line */ char *i_file; /* path name of the include file */ + char *i_realpath; /* path name processed by realpath() */ struct inclist **i_list; /* list of files it itself includes */ struct symtab **i_defs; /* symbol table for this file and its children when merged */ @@ -146,7 +147,8 @@ struct symtab **fdefined(const char *symbol, struct inclist *file, struct inclist **srcfile); struct filepointer *getfile(const char *file); void included_by(struct inclist *ip, struct inclist *newfile); -struct inclist *newinclude(const char *newfile, const char *incstring); +struct inclist *newinclude(const char *newfile, const char *incstring, + const char *incpath); void inc_clean(void); struct inclist *inc_path(const char *file, const char *include, int type); diff --git a/include.c b/include.c index f07fb70..b638011 100644 --- a/include.c +++ b/include.c @@ -143,7 +143,7 @@ remove_dotdot(char *path) * Add an include file to the list of those included by 'file'. */ struct inclist * -newinclude(const char *newfile, const char *incstring) +newinclude(const char *newfile, const char *incstring, const char *incpath) { struct inclist *ip; @@ -165,6 +165,20 @@ newinclude(const char *newfile, const char *incstring) fatalerr("strdup() failure in %s()\n", __func__); } + if (incpath == NULL) { + char r_include[PATHMAX + 1]; + + if (realpath(ip->i_file, r_include) == NULL) + ip->i_realpath = ip->i_file; + else + ip->i_realpath = strdup(r_include); + } + else { + ip->i_realpath = strdup(incpath); + } + if (ip->i_realpath == NULL) + fatalerr("strdup() failure in %s()\n", __func__); + inclistnext = inclistp; return (ip); } @@ -328,16 +342,12 @@ inc_path(const char *file, const char *include, int type) /* * Same filename but same file ? */ - char r_saved_path[PATHMAX + 1]; - - if (realpath(ip->i_file, r_saved_path) == NULL) - continue; - if (!strcmp(r_include, r_saved_path)) { + if (!strcmp(r_include, ip->i_realpath)) { inclistnext = ip + 1; return ip; } } } - return newinclude(fp, include); + return newinclude(fp, include, r_include); } diff --git a/main.c b/main.c index 0fcd0c2..9ebe101 100644 --- a/main.c +++ b/main.c @@ -518,7 +518,7 @@ main(int argc, char *argv[]) DBG_PRINT(stderr, "file: %s\n", *fp); filecontent = getfile(*fp); setfile_cmdinc(filecontent, cmdinc_count, cmdinc_list); - ip = newinclude(*fp, (char *) NULL); + ip = newinclude(*fp, NULL, NULL); find_includes(filecontent, ip, ip, 0, FALSE); freefile(filecontent); -- cgit v1.2.1