一、源代码:
<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:vim 4_8.c
1 #include "apue.h"
2 #include <fcntl.h>
3
4 int main(int argc, char *argv[])
5 {
6 if ( argc != 2 )
7 err_quit("Usage: access <pathname>");
8 if ( access(argv[1], R_OK) < 0 )
9 err_ret("access error");
10 else
11 printf("real user id has read permission of %s\n",argv[1]);
12 if ( open(argv[1],O_RDONLY) < 0 )
13 err_ret("open error");
14 else
15 printf("open file %s successfully\n", argv[1]);
16
17 exit(0);
18
19 }
20
~
二、运行结果:
<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access error.c
real user id has read permission of error.c
open file error.c successfully
<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access /etc/passwd
real user id has read permission of /etc/passwd
open file /etc/passwd successfully
<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G:access /etc/shadow
access error: Permission denied
open error: Permission denied
<bldc:/home/tingbinz/apue.3e/SBSCODE/4>R*_*G: