From 0a9d50d2c38d3638457c0d654796dfb426d010a1 Mon Sep 17 00:00:00 2001 From: angel Date: Tue, 13 Sep 2005 17:34:58 +0000 Subject: [PATCH] New function locate_file(), that returns the resolved path to a file using path_fopen(). git-svn-id: file:///home/angel/tmp/svn-triptico/ahxm/trunk@720 c87de0a0-a11c-0410-a1e5-866214bc28b2 --- support.c | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/support.c b/support.c index 7ee4059..da5bbff 100644 --- a/support.c +++ b/support.c @@ -38,6 +38,8 @@ static char ** library_path=NULL; static int n_library_paths=0; +static char located_file[2048]; + /******************* Code ********************/ @@ -131,6 +133,9 @@ FILE * path_fopen(char * filename, char * mode) /* try first here */ if((f=fopen(filename, mode)) != NULL) { + strncpy(located_file, filename, sizeof(located_file)); + located_file[sizeof(located_file) - 1]='\0'; + add_to_library_path(filename, 1); return(f); } @@ -138,15 +143,13 @@ FILE * path_fopen(char * filename, char * mode) /* couldn't open; try concatenating all stored paths */ for(n=n_library_paths - 1;n >= 0;n--) { - char tmp[4096]; - - snprintf(tmp, sizeof(tmp) - 1, "%s/%s", + snprintf(located_file, sizeof(located_file), "%s/%s", library_path[n], filename); - tmp[sizeof(tmp) - 1]='\0'; + located_file[sizeof(located_file) - 1]='\0'; - if((f=fopen(tmp, mode)) != NULL) + if((f=fopen(located_file, mode)) != NULL) { - add_to_library_path(tmp, 1); + add_to_library_path(located_file, 1); break; } } @@ -155,3 +158,22 @@ FILE * path_fopen(char * filename, char * mode) } +/** + * locate_file - Locates a file inside the path + * @filename: the file to be located + * + * Locates a file inside the library path maintained by path_fopen() + * and add_library_path(). If the file is found, a pointer to a static + * buffer containing the real path of the file is returned, or + * NULL otherwise. + */ +char * locate_file(char * filename) +{ + FILE * f; + + if((f=path_fopen(filename, "r")) == NULL) + return(NULL); + + fclose(f); + return(located_file); +} -- 2.11.4.GIT