[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. 教你在你的应用程序中扩展使用dynamic类型

    教你在你的应用程序中扩展使用dynamic类型 相信大家在开发中经常会接触到mvc中的ViewBag,有心的同学会,发现这就是一个dynamic类型,查看源码一谈究竟,本文也是根据dynamic来扩展 ...

  2. sublime部署开发环境

    安装nodejs 去官网下载安装,暂时不建议安装5.0以上版本. 命令行运行以下命令查看是否安装成功: 全局安装gulp $ npm install -g gulp 全局安装requirejs 压缩资 ...

  3. POJ 2528 Mayor's posters(线段树)

    点我看题目 题意 :建一堵墙粘贴海报,每个候选人只能贴一张海报,海报的高度与墙一样高,一张海报的宽度是整数个单位,墙被划分为若干个部分,每个部分的宽度为一个单位,每张海报完全的覆盖一段连续的墙体,墙体 ...

  4. VC判断当前用户有无Administrator的权限(用EqualSid API函数判断与Admin是否在一个组的Sid)

    /************************************************************************/ /* 函数说明:判断有无Administrator ...

  5. linux字符图形界面

    /etc/inittab 1)  字符界面标识: id:3:initdefault: 2)  图形界面标识: id:5:initdefault:   [root@ora9i ~]# vi /etc/i ...

  6. maven install 跳过 测试 test

    你可能想要配置 Maven 使其完全跳过单元测试. 可能你有一个很大的系统,单元测试需要花好多分钟来完成,而你不想在生成最终输出前等单元测试完成. 你可能正工作在一个遗留系统上面,这个系统有一系列的失 ...

  7. ELK之topbeat部署

    topbeat定期收集系统信息如每个进程信息.负载.内存.磁盘等等,然后将数据发送到elasticsearch进行索引,最后通过kibana进行展示. 下面是具体的安装及配置步骤: 1.安装topbe ...

  8. Asp.net--Ajax前后台数据交互

    转自:http://www.cnblogs.com/guolebin7/archive/2011/02/22/1961737.html 代码由前后台两部分组成: 前台:(新建一个Default.asp ...

  9. 数学:lucas定理的总结

    今天考试的题目中有大组合数取模,不会唉,丢了45分,我真是个弱鸡,现在还不会lucas. 所以今天看了一下,定理差不多是: (1)Lucas定理:p为素数,则有: 即:lucas(n,m,p)=c(n ...

  10. IIS出现Server Error in '/' Application.CS0016的解决办法

    这两天一直在弄IIS的事,全都是报错,网上找了好多资料,也尝试了很多,终于在几分钟之前把困扰了我一周的麻烦给解决了,现整理出来,希望对大家有用,闲话少说,直接上图了 Server Error in ' ...