Skip to content

Commit

Permalink
Merge pull request #421 from apocelipes/feat-little-improvements
Browse files Browse the repository at this point in the history
Improve is_file and posix_searchpath
  • Loading branch information
dtschump authored Jun 8, 2024
2 parents 8882adc + eddea44 commit 41b823b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions CImg.h
Original file line number Diff line number Diff line change
Expand Up @@ -7382,6 +7382,9 @@ namespace cimg_library {
#if cimg_OS==2
const DWORD res = cimg::win_getfileattributes(path);
return res!=INVALID_FILE_ATTRIBUTES && !(res&FILE_ATTRIBUTE_DIRECTORY);
#elif cimg_OS==1
struct stat st_buf;
return (!stat(path,&st_buf) && (S_ISREG(st_buf.st_mode) || S_ISFIFO(st_buf.st_mode) || S_ISCHR(st_buf.st_mode) || S_ISBLK(st_buf.st_mode)));
#else
std::FILE *const file = cimg::std_fopen(path,"rb");
if (!file) return false;
Expand Down Expand Up @@ -67430,7 +67433,7 @@ namespace cimg_library {
#if cimg_OS==1
inline bool posix_searchpath(const char *file) {
if (!file || !*file) return false;
const char *path = getenv("PATH");
const char *path = std::getenv("PATH");

if (!path) path = "/usr/local/bin:/bin:/usr/bin";
size_t file_len = strnlen(file,NAME_MAX + 1);
Expand All @@ -67441,15 +67444,15 @@ namespace cimg_library {
const char *p = path, *z = 0;
while (true) {
z = std::strchr(p,':');
if (!z) z = p + strlen(p);
if (!z) z = p + std::strlen(p);
if ((size_t)(z - p)>=path_total_len) {
if (!*z++) break;
continue;
}
std::memcpy(buf,p,z - p);
buf[z - p] = '/';
std::memcpy(buf + (z - p) + (z>p),file,file_len + 1);
if (!access(buf,F_OK)) { delete[] buf; return true; }
if (cimg::is_file(buf)) { delete[] buf; return true; }
if (!*z++) break;
p = z;
}
Expand Down

0 comments on commit 41b823b

Please sign in to comment.