[lingyun@localhost access_1]$ ls
access.c
实例一:

[lingyun@localhost access_1]$ cat access.c 
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  access.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/02/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "08/02/2013 04:10:44 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <unistd.h>

int main(void)
{
    if(access("/etc/passwd", R_OK) == 0)
        printf("/etc/passwd can be read\n");
}

[lingyun@localhost access_1]$ gcc access.c 
[lingyun@localhost access_1]$ ./a.out 
/etc/passwd can be read
[lingyun@localhost access_1]$

实例二:

[lingyun@localhost access_2]$ vim access.c
 + access.c                                                                                                                  
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  access.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/02/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "08/02/2013 04:18:45 PM"
 *                 
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    if(argc < 2)
    {
        printf("Usave: ./test filename\n");
        exit(1);
    }

if(access(argv[1], F_OK) == -1)
    {
        puts("File not exists!");
        exit(2);
    }

if(access(argv[1], R_OK) == -1)
        puts("You can't read the file!");
    else
        if(access(argv[1], R_OK|W_OK) != -1)
            puts("You can read and write the file");
        else
            puts("You can read the file");

exit(0);
}
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
 ~/apue/access/access_2/access.c[+]   CWD: /usr/local/src/lingyun/apue/access/access_2   Line: 41/42:12                      
"access.c" [New] 42L, 1090C written

[lingyun@localhost access_2]$ gcc access.c 
[lingyun@localhost access_2]$ ./a.out /etc/passwd
You can read the file
[lingyun@localhost access_2]$ ./a.out access.c 
You can read and write the file
[lingyun@localhost access_2]$

[lingyun@localhost access_3]$ vim access.c 
 + access.c                                                                                                                  
/*********************************************************************************
 *      Copyright:  (C) 2013 fulinux<fulinux@sina.com> 
 *                  All rights reserved.
 *
 *       Filename:  access.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(08/02/2013~)
 *         Author:  fulinux <fulinux@sina.com>
 *      ChangeLog:  1, Release initial version on "08/02/2013 04:10:44 PM"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        printf("usage: a.out <pathname>\n");
        exit(1);
    }
    if(access(argv[1], R_OK) < 0)
    {
        warn("access error for %s", argv[1]);
    }
    else
        printf("read access OK\n");
    if(open(argv[1], O_RDONLY) < 0)
    {
        warn("open error for %s", argv[1]);
    }
    else
        printf("open for reading OK\n");
    exit(0);
}

~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
~                                                                                                                            
 ~/apue/access/access_3/access.c[+]   CWD: /usr/local/src/lingyun/apue/access/access_3   Line: 36/41:5                       
"access.c" 41L, 1065C written

[lingyun@localhost access_3]$ gcc access.c 
[lingyun@localhost access_3]$ ./a.out 
usage: a.out <pathname>
[lingyun@localhost access_3]$ ./a.out access.c 
read access OK
open for reading OK
[lingyun@localhost access_3]$ ls -l /etc/shadow
---------- 1 root root 1151 Jun  4 23:16 /etc/shadow
[lingyun@localhost access_3]$ ./a.out /etc/shadow
a.out: access error for /etc/shadow: Permission denied
a.out: open error for /etc/shadow: Permission denied
[lingyun@localhost access_3]$ sudo su
[root@localhost access_3]# chown root a.out 
[root@localhost access_3]# chmod u+s a.out 
[root@localhost access_3]# ls -l a.out 
-rwsr-xr-x 1 root trainning 7220 Aug  2 17:04 a.out
[root@localhost access_3]# exit
exit
[lingyun@localhost access_3]$ ./a.out /etc/shadow
a.out: access error for /etc/shadow: Permission denied
open for reading OK
[lingyun@localhost access_3]$

linux之access函数解析的更多相关文章

  1. linux下access函数

    Linux内核总是根据进程的有效用户ID和有效组ID来决定一个进程是否有权访问某个文件. 因此,在编写调整用户ID的程序时,在读写一个文件之前必须明确检查其用户是否原本就有对此文件的访问权限. 为了实 ...

  2. Linux exec族函数解析

    背景 在提到 vfork 函数时,我们提到了这个概念.为了更好地学习与运用,我们对exec族函数进行展开. exec函数族 介绍 有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中 ...

  3. linux之unlink函数解析

    [lingyun@localhost unlink]$ cat unlink.c  /********************************************************* ...

  4. linux之ioctl函数解析

    [lingyun@localhost ioctl_1]$ ls ipconfig.c [lingyun@localhost ioctl_1]$ cat ipconfig.c  /*********** ...

  5. linux之umask函数解析

    [lingyun@localhost umask_1]$ vim umask.c  + umask.c                                                 ...

  6. linux之utime函数解析

    [lingyun@localhost utime]$ ls hello  utime.c  world [lingyun@localhost utime]$ cat utime.c  /******* ...

  7. linux之chdir函数解析

    [lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c  /********************* ...

  8. linux之getcwd函数解析

    [lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...

  9. linux之stat函数解析

    [lingyun@localhost stat_1]$ vim stat.c  + stat.c                                                     ...

随机推荐

  1. 有两个数组a,b,大小都为n;通过交换a,b中的元素,使sum(a)-sum(b)最小。

    今天在浏览网页的时候,发现了一个叫做  华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用 ...

  2. linux下测试磁盘的读写IO速度【转】

    Reference1:http://server.chinabyte.com/495/12361995.shtmlReference2:https://www.deleak.com/blog/2011 ...

  3. NotePad++更改背景颜色

    白色的编辑框看得眼睛不舒服,怎么样更改NotePad++的背景颜色使眼睛更舒服些? 1.设置--语言格式设置 2.设置背景色 “背景色”一栏,选择背景色颜色   “使用全局背景色”一栏要打上√,否则无 ...

  4. POJ 2442 Sequence 优先队列

    题目: http://poj.org/problem?id=2442 #include <stdio.h> #include <string.h> #include <q ...

  5. 解决CocoaPods在OS X 10.11出现问题-b

    最近把mac系统升级到10.11系统,但是在用pod install命令的时候,却提示command not found.后来上网查了下才知道,Cocoapods在10.11系统上发生了变化. 在st ...

  6. <c:if>判断参数是否为空

    EL表达式判断字符串是否为空: <c:if test="${empty str}"> str为空</c:if> <c:if test="${ ...

  7. [topcoder]KingdomReorganization

    http://community.topcoder.com/stat?c=problem_statement&pm=11282&rd=14724 这道题是最小生成树,但怎么转化是关键. ...

  8. SQLite 对中文路径的支持(用到了StringToWideChar和Utf8Encode在D7的System单元中自带)

    最近用SQLITE作为数据库,发现,如果直接传递带中文路径或文件名的数据库,会导致无法打开数据库的情况.看了一下SQLITE的源码,才发现,原来SQLITE中是用UTF8编码进行文件打开操作的. 所以 ...

  9. 【Xamarin开发 Android 系列 13】 应用打包部署

    原文:[Xamarin开发 Android 系列 13] 应用打包部署 开始倒叙咯................ 先更新大宝部署吧,这个章节比较的Easy,童鞋们不用费脑筋.点解?从界面上填写几个参 ...

  10. SpringMVC源码解析 - HandlerMethod

    HandlerMethod及子类主要用于封装方法调用相关信息,子类还提供调用,参数准备和返回值处理的职责. 分析下各个类的职责吧(顺便做分析目录): HandlerMethod 封装方法定义相关的信息 ...