检查权限(状态)位.
#include <sys/stat.h>
bool can_exec(const char *file)
{
struct stat st;
if (stat(file, &st) < 0)
return false;
if ((st.st_mode & S_IEXEC) != 0)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
访问(2):
#include <unistd.h>
if (! access (path_name, X_OK))
// executable
Run Code Online (Sandbox Code Playgroud)
调用stat(2)会有更高的开销填写结构.除非您需要额外的信息.