[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. 实现CodeFirst自动数据迁移无需命令

    本主题假设您掌握了实体框架中 Code First 迁移的基本知识. 借助自动迁移功能,您无需对您所做的每一个更改都在程序包管理器控制台中运行Update-Database 命令. 启用迁移 只需执行 ...

  2. python中的单下划线和双下划线意义和作用

    Python中并没有真正意义上的“私有”,类的属性的的可见性取决于属性的名字(这里的属性包括了函数).例如,以单下划线开头的属性(例如_spam),应被当成API中非公有的部分(但是注意,它们仍然可以 ...

  3. Codeforces Round #197 (Div. 2) : D

    这题也是一个线段树的水题: 不过开始题目没看明白,害得我敲了一个好复杂的程序.蛋疼啊.... 最后十几分钟的时候突然领悟到了题意,但是还是漏掉一个细节,老是过不去... 以后比赛的时候不喝啤酒了,再也 ...

  4. Stanford CoreNLP--Named Entities Recognizer(NER)

    Standford Named Entities Recognizer(NER),命名实体识别是信息提取(Information Extraction)的一个子任务,它把文字的原子元素(Atomic ...

  5. 【poj3693】Maximum repetition substring(后缀数组+RMQ)

    题意:给定一个字符串,求重复次数最多的连续重复子串. 传说中的后缀数组神题,蒟蒻真的调了很久才对啊.感觉对后缀数组和RMQ的模版都不是很熟,导致还是会有很多各种各样的小错误= = 首先,枚举重复子串的 ...

  6. 单片机 C 语言模块化编程

    好的开始是成功的一半 通过上一章的学习,我想你已经掌握了如何在程序中释放CPU了.希望能够继续坚持下去.一个良好的开始是成功的一半.我们今天所做的一切都是为了在单片机编程上做的更好. 在谈论今天的主题 ...

  7. VC提交网页表单(一共八篇)

    VC提交网页表单-自动评论留言(1)http://blog.csdn.net/wangningyu/article/details/4526357VC提交网页表单-自动评论留言(2)http://bl ...

  8. 设置VS2015上关闭和打开tab快捷键

    Ctrl+W关闭Tab: Tools > Options > Environment > Keyboard > File.Close > Use new shortcut ...

  9. 《ruby编程语言》笔记 1

    赋值: ruby支持并行赋值,即允许在赋值表达式中出现多余一个值和多于一个的变量: x,y=1,2a,b=b,ax,y,z=[1,2,3] (python同样可以正常上面的语句). Methods i ...

  10. 【转】如何使用TestFlight进行Beta测试 -- 不错

    原文网址:http://www.cocoachina.com/ios/20141022/10009.html 假如你现在完成一个App的开发并准备进行真机测试,那么请问你会怎么做呢?难道是直截了当的把 ...