• linux之access函数解析


    [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 ");
    }


    [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 ");
            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> ");
            exit(1);
        }
        if(access(argv[1], R_OK) < 0)
        {
            warn("access error for %s", argv[1]);
        }
        else
            printf("read access OK ");
        if(open(argv[1], O_RDONLY) < 0)
        {
            warn("open error for %s", argv[1]);
        }
        else
            printf("open for reading OK ");
        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]$ 


  • 相关阅读:
    默认约束(十六)
    唯一约束(十五)
    主键约束(十四)
    自动编号(十三)
    空值(NULL)和非空(NOT NULL)(十二)
    Codeforces Round #249 (Div. 2) A B
    MySQL 採用Xtrabackup对数据库进行全库备份
    强算KMeans聚类算法演示器
    提高短信营销效果的四大技巧分析
    14年7月总结
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3233730.html
Copyright © 2020-2023  润新知