From 1900710c6854891ea76e04bcef4625a475d99840 Mon Sep 17 00:00:00 2001 From: Perry Werneck Date: Thu, 12 Sep 2019 11:56:43 -0300 Subject: [PATCH] Adding method to build filenames independent of the OS. --- src/core/linux/util.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- src/core/windows/util.c | 73 ++++++++++++++++++++++++++++++++----------------------------------------- src/include/lib3270.h | 18 +++++++++++++++++- src/testprogram/testprogram.c | 6 ++++++ 4 files changed, 127 insertions(+), 57 deletions(-) diff --git a/src/core/linux/util.c b/src/core/linux/util.c index 57f495c..bb15373 100644 --- a/src/core/linux/util.c +++ b/src/core/linux/util.c @@ -36,31 +36,37 @@ #include #include #include +#include -static char * build_filename(const char *root, const char *str, va_list args) +static char * concat(char *path, const char *name, size_t *length) { - size_t szFilename = 1024 + strlen(root); - char * filename = (char *) lib3270_malloc(szFilename); + size_t szCurrent = strlen(path); - strcpy(filename,root); + if(szCurrent > 1 && path[szCurrent-1] != '/') + strcat(path,"/"); - while(str) { + szCurrent += strlen(name); - size_t szCurrent = strlen(filename); + if(szCurrent >= *length) + { + *length += (szCurrent + 1024); + path = lib3270_realloc(path,*length); + } - if(filename[szCurrent-1] != '/') - strcat(filename,"/"); + strcat(path,name); - szCurrent += strlen(str); + return path; +} - if(szCurrent >= szFilename) - { - szFilename += (szCurrent + 1024); - filename = lib3270_realloc(filename,szFilename); - } +static char * build_filename(const char *root, const char *str, va_list args) +{ + size_t szFilename = 1024 + strlen(root); + char * filename = (char *) lib3270_malloc(szFilename); - strcat(filename,str); + strcpy(filename,root); + while(str) { + filename = concat(filename,str,&szFilename); str = va_arg(args, const char *); } @@ -90,3 +96,54 @@ char * lib3270_build_config_filename(const char *str, ...) return filename; } + +char * lib3270_build_filename(const char *str, ...) +{ + size_t szFilename = 1024; + char * filename = (char *) lib3270_malloc(szFilename); + char * tempname; + + // First build the base filename + memset(filename,0,szFilename); + + va_list args; + va_start (args, str); + while(str) { + filename = concat(filename,str,&szFilename); + str = va_arg(args, const char *); + } + va_end (args); + + // Check paths + size_t ix; + + static const char * paths[] = + { + LIB3270_STRINGIZE_VALUE_OF(DATADIR), + LIB3270_STRINGIZE_VALUE_OF(CONFDIR), + "." + }; + + for(ix = 0; ix < (sizeof(paths)/sizeof(paths[0])); ix++) + { + tempname = lib3270_strdup_printf("%s/%s",paths[ix],filename); + + if(access(tempname, F_OK) == 0) + { + lib3270_free(filename); + return tempname; + } + + lib3270_free(tempname); + + } + + // Not found! Force the standard data dir + + tempname = lib3270_strdup_printf("%s/%s",paths[0],filename); + lib3270_free(filename); + + return tempname; + +} + diff --git a/src/core/windows/util.c b/src/core/windows/util.c index 50d053a..2956a6f 100644 --- a/src/core/windows/util.c +++ b/src/core/windows/util.c @@ -276,33 +276,32 @@ char * lib3270_get_user_name() } -/* -LIB3270_EXPORT char * lib3270_build_data_filename(const char *name) +static char * concat(char *path, const char *name, size_t *length) { - // https://github.com/GNOME/glib/blob/master/glib/gwin32.c - - char *p; - char wc_fn[MAX_PATH]; + char *ptr; + size_t szCurrent = strlen(path); - if (!GetModuleFileName(NULL, wc_fn, MAX_PATH)) - return NULL; + for(ptr=path;*ptr;ptr++) + { + if(*ptr == '/') + *ptr = '\\'; + } - if((p = strrchr(wc_fn, '\\')) != NULL) - *p = '\0'; + if(szCurrent > 1 && path[szCurrent-1] != '\\') + strcat(path,"\\"); - if((p = strrchr(wc_fn, '/')) != NULL) - *p = '\0'; + szCurrent += strlen(name); - return lib3270_strdup_printf("%s\\%s",wc_fn,name); + if(szCurrent >= *length) + { + *length += (szCurrent + 1024); + path = lib3270_realloc(path,*length); + } -} + strcat(path,name); -char * lib3270_build_config_filename(const char *name) -{ - // On windows the data and config path are the same. - return lib3270_build_data_filename(name); + return path; } -*/ static char * build_filename(const char *str, va_list args) { @@ -314,6 +313,7 @@ static char * build_filename(const char *str, va_list args) #ifdef DEBUG filename[0] = '.'; + filename[1] = '\\'; #else DWORD szPath = GetModuleFileName(hModule,filename,szFilename); filename[szPath] = 0; @@ -324,28 +324,7 @@ static char * build_filename(const char *str, va_list args) ptr[1] = 0; while(str) { - - size_t szCurrent = strlen(filename); - - for(ptr=filename;*ptr;ptr++) - { - if(*ptr == '/') - *ptr = '\\'; - } - - if(filename[szCurrent-1] != '\\') - strcat(filename,"\\"); - - szCurrent += strlen(str); - - if(szCurrent >= szFilename) - { - szFilename += (szCurrent + 1024); - filename = lib3270_realloc(filename,szFilename); - } - - strcat(filename,str); - + filename = concat(filename,str,&szFilename); str = va_arg(args, const char *); } @@ -376,3 +355,15 @@ char * lib3270_build_config_filename(const char *str, ...) return filename; } +char * lib3270_build_filename(const char *str, ...) +{ + va_list args; + va_start (args, str); + + char *filename = build_filename(str, args); + + va_end (args); + + return filename; +} + diff --git a/src/include/lib3270.h b/src/include/lib3270.h index a9243b7..f3742b3 100644 --- a/src/include/lib3270.h +++ b/src/include/lib3270.h @@ -1483,15 +1483,31 @@ #endif // WIn32 /** - * @brief Build filename on "DATADIR". + * @brief Build filename on application data dir. * * @return Full path for the file (release it with lib3270_free). * */ LIB3270_EXPORT char * lib3270_build_data_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED; + /** + * @brief Build filename on application configuration dir. + * + * @return Full path for the file (release it with lib3270_free). + * + */ LIB3270_EXPORT char * lib3270_build_config_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED; + /** + * @brief Build and search for filename. + * + * Build filename and search for it on current, configuration and data dirs. + * + * @return Full path for the file (release it with lib3270_free). + * + */ + LIB3270_EXPORT char * lib3270_build_filename(const char *str, ...) LIB3270_GNUC_NULL_TERMINATED; + LIB3270_EXPORT void lib3270_set_session_id(H3270 *hSession, char id); LIB3270_EXPORT char lib3270_get_session_id(H3270 *hSession); diff --git a/src/testprogram/testprogram.c b/src/testprogram/testprogram.c index 2f02066..dff29a9 100644 --- a/src/testprogram/testprogram.c +++ b/src/testprogram/testprogram.c @@ -84,6 +84,12 @@ int main(int argc, char *argv[]) printf("Configfile: \"%s\"\n",datafile); } + { + lib3270_autoptr(char) datafile = lib3270_build_filename("Makefile",NULL); + printf("Custom file: \"%s\"\n",datafile); + } + + /* if(lib3270_set_url(h,NULL)) lib3270_set_url(h,"tn3270://fandezhi.efglobe.com"); -- libgit2 0.21.2