/***************************************************
* 函数说明: 判断当前path参数是否为一个可读的文件
* 函数返回: 0 - 文件可读 1 - 权限拒绝 -1 -函数错误
* 参 数 : path 文件路径.
***************************************************/
int isReadFile(const char *path)
{
struct stat info;
int euid,egid;
if ( 0!= stat(path,&info) )
{
return -1;
}
//if it is a file
if( S_IFREG != (info.st_mode & S_IFMT ) )
{
return 1;
}
euid=geteuid();//returns the real user ID of the
current process
egid=getegid();//returns the effective user ID of the
current process
//if effective user is root
if(euid==0)
{
if(info.st_mode & S_IRUSR || info.st_mode &
S_IRGRP ||info.st_mode & S_IROTH)
{
return 0;
}
else
{
return 1;
}
}
//another user can read
if( info.st_mode & S_IROTH )
return 0;
//group can read
if((info.st_gid == egid) && ( info.st_mode & S_IRGRP)
)
return 0;
if((info.st_uid == euid) && (info.st_mode & S_IRUSR))
return 0;
return 1;
}
文件权限.
S_IRUSR 读权限,文件属主
S_IWUSR 写权限,文件属主
S_IXUSR 执行权限,文件属主
S_IRGRP 读权限,文件所属组
S_IWGRP 写权限,文件所属组
S_IXGRP 执行权限,文件所属组
S_IROTH 读权限,其他用户
S_IWORH 写权限,其他用户
S_IXORH 执行权限,其他用户