分类: C/C++
2013-12-03 17:20:18
access函数是按实际用户ID和实际组ID进行存取许可权测试的。
#include
mode参数 R_OK 测试读许可权 W_OK 测试写许可权
X_OK 测试执行许可权 F_OK测试文件是否存在
#include "ourhdr.h"
void err_quit(char *str);
int main(int argc,char *argv[])
{
if(argc!=2)
err_quit("usage: a.out
if(access(argv[1],R_OK)<0)
printf("access error for %s",argv[1]);
else
printf("read access OK\n");
if(open(argv[1],O_RDONLY)<0)
printf("open error for %s",argv[1]);
else
printf("open for reading OK\n");
exit(0);
}
umask函数为进程设置文件方式创建屏蔽字,并返回以前的值
#include "ourhdr.h"
int main(void)
{
umask(0);
疑问?用户执行权限无法获得 需要删除以前创建的文件
if(creat("foo",S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|
S_IROTH|S_IWOTH)<0)
printf("creat error for foo");
umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); //屏蔽了S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH这四个权限
if(creat("bar",S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|
S_IROTH|S_IWOTH)<0)
printf("creat error for bar");
exit(0);
}