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\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函数解析的更多相关文章
- linux下access函数
Linux内核总是根据进程的有效用户ID和有效组ID来决定一个进程是否有权访问某个文件. 因此,在编写调整用户ID的程序时,在读写一个文件之前必须明确检查其用户是否原本就有对此文件的访问权限. 为了实 ...
- Linux exec族函数解析
背景 在提到 vfork 函数时,我们提到了这个概念.为了更好地学习与运用,我们对exec族函数进行展开. exec函数族 介绍 有时我们希望子进程去执行另外的程序,exec函数族就提供了一个在进程中 ...
- linux之unlink函数解析
[lingyun@localhost unlink]$ cat unlink.c /********************************************************* ...
- linux之ioctl函数解析
[lingyun@localhost ioctl_1]$ ls ipconfig.c [lingyun@localhost ioctl_1]$ cat ipconfig.c /*********** ...
- linux之umask函数解析
[lingyun@localhost umask_1]$ vim umask.c + umask.c ...
- linux之utime函数解析
[lingyun@localhost utime]$ ls hello utime.c world [lingyun@localhost utime]$ cat utime.c /******* ...
- linux之chdir函数解析
[lingyun@localhost chdir]$ ls chdir.c [lingyun@localhost chdir]$ cat chdir.c /********************* ...
- linux之getcwd函数解析
[lingyun@localhost getcwd]$ cat getcwd.c /********************************************************** ...
- linux之stat函数解析
[lingyun@localhost stat_1]$ vim stat.c + stat.c ...
随机推荐
- STM32之SD卡
目录 一.SD卡概述 1.定义 2.容量等级 3.SD卡框图 4.SD卡与TF卡的区别 二. SD卡内部结构 1. SD卡内部结构简图 2. 存储阵列结构图 3.Buffer 4.“存储阵列Block ...
- 解决vsftpd 2.2.2读取目录列表失败的问题
该错误是由iptables的配置引起的,临时的解决方法是执行如下命令: [root@localhost soft]# modprobe ip_nat_ftp 再次登陆列表正常啦! 但当你重新启动服务器 ...
- SQL Server 2012 Features
SQL SQL Server 2012 新增加的几个函数: SELECT CONVERT (INT, 'Angkor-216.00') 直接报错 SELECT TRY_CONVERT(INT, 'SS ...
- git的id_rsa.pub的生成(也就是github上的SSH Keys)
只需要一条语句就可以实现生成id_rsa.pub和id_rsa的目的:ssh-keygen -t rsa -C your_email 注意:这个邮箱是你github上的邮箱.只有在gthub上添加了这 ...
- Android开源项目发现---ImageView 篇(持续更新)
1. PhotoView 支持双击或双指缩放的ImageView 在ViewPager等Scrolling view中正常使用,相比上面的AndroidTouchGallery,不仅支持ViewPag ...
- AlertDialog.Builder 样式设置
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.AlertDia ...
- C 语言字符串(译)
C 语言的 switch 语句非常强大.然而,它不能用字符串作为判断条件,只能用常整数.这是可以理解的,因为 C 的字符串仅仅是数组,它们并不是并不是一个整体. 在某些情况下,将 string 作为 ...
- bzoj1263
观察可得,最大的拆分方法是尽量拆成3,特殊的,如果最后剩下了1,那么就把3+1变成2+2 然后高精度计算即可 var s2,s3,i,n,l:longint; a:..] of longint ...
- HDU 5935 Car 【模拟】 (2016年中国大学生程序设计竞赛(杭州))
Car Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- win8 mysqlzip install
1. 下载MySQL Community Server 5.6.142. 解压MySQL压缩包 将以下载的MySQL压缩包解压到自定义目录下.3. 添加环境变量 变量名:MYSQL_HOME 变量值: ...