From f7510dc6aaef16f32a9cdfc8880498d05ab57690 Mon Sep 17 00:00:00 2001 From: iceman1001 Date: Sat, 21 Sep 2019 11:12:36 +0200 Subject: [PATCH] better detection of directory (@doegox) --- client/fileutils.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/client/fileutils.c b/client/fileutils.c index 717787293..3599ec6f4 100644 --- a/client/fileutils.c +++ b/client/fileutils.c @@ -69,6 +69,23 @@ int fileExists(const char *filename) { return result == 0; } +/** + * @brief checks if path is file or directory. + * @param filename + * @return + */ +int is_regular_file(const char *filename) { +#ifdef _WIN32 + struct _stat st; + _stat(filename, &st); + return S_ISREG(_st.st_mode); +#else + struct stat st; + stat(filename, &st); + return S_ISREG(st.st_mode); +#endif +} + static char *filenamemcopy(const char *preferredName, const char *suffix) { if (preferredName == NULL) return NULL; if (suffix == NULL) return NULL; @@ -1016,9 +1033,11 @@ int searchFile(char **foundpath, const char *pm3dir, const char *searchname, con return PM3_EINVARG; if (searchname == NULL || strlen(searchname) == 0) return PM3_EINVARG; - if (searchname[ strlen(searchname)-1] == '/') + + if (is_regular_file(searchname) == 0) return PM3_EINVARG; + char *filename = filenamemcopy(searchname, suffix); if (filename == NULL || strlen(filename) == 0) return PM3_EMALLOC;