程序清单:
#include <stdio.h> //for printf
#include <stdlib.h> //for exit
#include <unistd.h> //for access
#include <fcntl.h> //for open
int main(int argc,char *argv[])
{
if(argc != 2)
{
printf("usage:./a.out ");
exit(1);
}
if(access(argv[1],R_OK) < 0)
{
printf("access error for %s\n",argv[1]);
exit(1);
}
else
{
printf("read access OK\n");
}
if(open(argv[1],O_RDONLY) < 0)
{
printf("open error for %s\n",argv[1]);
exit(1);
}
else
{
printf("open for reading OK\n");
}
exit(0);
}
|
程序执行结果:
obe-240 test/linuxc> gcc access.c
obe-240 test/linuxc> ls -l a.out
-rwxr-xr-x 1 eagle hitv 9808 2010-12-24 10:31 a.out
obe-240 test/linuxc> ./a.out a.out
read access OK
open for reading OK
obe-240 test/linuxc> ls -l /etc/shadow
-rw-r----- 1 root shadow 1361 2010-10-19 19:22 /etc/shadow
obe-240 test/linuxc> ./a.out /etc/shadow
access error for /etc/shadow
阅读(1872) | 评论(0) | 转发(0) |