From 40bcf8e167028fe35160ac7dfe8755480d736c89 Mon Sep 17 00:00:00 2001 From: apocelipes Date: Wed, 5 Jun 2024 20:45:02 +0800 Subject: [PATCH] Prefer to use magick rather than convert Fix #418. --- CImg.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/CImg.h b/CImg.h index 0619e054..ee928cb3 100644 --- a/CImg.h +++ b/CImg.h @@ -173,6 +173,8 @@ #include #include #include +#include +#include #elif cimg_OS==2 #ifndef NOMINMAX #define NOMINMAX @@ -67427,6 +67429,34 @@ namespace cimg_library { } #endif +#if cimg_OS==1 + inline bool posix_searchpath(const char *file) { + if (!file || !*file) return false; + const char *path = getenv("PATH"); + + if (!path) path = "/usr/local/bin:/bin:/usr/bin"; + size_t fileLen = strnlen(file, NAME_MAX+1); + if (fileLen > NAME_MAX) return false; + size_t pathTotalLen = strnlen(path, PATH_MAX-1)+1; + + char buff[pathTotalLen+fileLen+1]; + for (const char *p = path, *z = NULL; ; p = z) { + z = strchr(p, ':'); + if (!z) z = p+strlen(p); + if ((size_t)(z-p) >= pathTotalLen) { + if (!*z++) break; + continue; + } + memcpy(buff, p, z-p); + buff[z-p] = '/'; + memcpy(buff+(z-p)+(z>p), file, fileLen+1); + if (access(buff, F_OK) == 0) return true; + if (!*z++) break; + } + return false; + } +#endif + //! Get the file or directory attributes with support for UTF-8 paths (Windows only). #if cimg_OS==2 inline DWORD win_getfileattributes(const char *const path) { @@ -67876,6 +67906,10 @@ namespace cimg_library { std::strcpy(s_path,"./convert"); if ((file=cimg::std_fopen(s_path,"r"))!=0) { cimg::fclose(file); path_found = true; } } + if (!path_found) { + std::strcpy(s_path, "magick"); + if (posix_searchpath("magick")) path_found = true; + } if (!path_found) std::strcpy(s_path,"convert"); #endif winformat_string(s_path);