Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve is_file and posix_searchpath #421

Merged
merged 1 commit into from
Jun 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading