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 ...
随机推荐
- Leetcode 解题 Median of Two sorted arrays
题目:there are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 加密你的SQLite
转自王中周的个人博客 关于SQLite SQLite是一个轻量的.跨平台的.开源的数据库引擎,它的在读写效率.消耗总量.延迟时间和整体简单性上具有的优越性,使其成为移动平台数据库的最佳解决方案(如iO ...
- jquery.inputmask.js 输入框input输入内容格式限制插件
今天使用的就是这几行代码. 利用 jquery.inputmask.js 下载地址(如果打不开的话 请FQ http://plugins.jquery.com/jquery.inputmask/) ...
- Java数据库连接关闭后无法启动
错误如下: java.sql.SQLException: No operations allowed after connection closed. at com.mysql.jdbc.Connec ...
- 制作qtopia-2.2.0和qt4文件系统
转自 rootfs_qtopia_qt4.img 1. 解压rootfs_qtopia_qt4-20100816.tar.gz,得到目录rootfs_qtopia_qt4,里面内容比较大,超过了64M ...
- pcDuino无显示器刷机与使用
准备工作: pcduino : 点此购买 一个可用的并且启用了DHCP的有线路由器. 连接pcduino与路由器的网线一根. 至少1张4G microSD卡,如果内存卡不大,可以用内存卡刷内核,用u盘 ...
- C# winform 导出导入Excel/Doc 完整实例教程[网上看到的]
还真没做过winform的导出导入,今天上网百度了一下.结果--- 所以还是我自己写个吧.之前做过web的,半搬半做就OK. 1添加引用:Aspose.Cells.dll(我们就叫工具包吧,可以从网上 ...
- 微软开放技术发布针对 Mac 和 Linux 的更新版 Azure Node.JS SDK 和命令行工具
发布于 2013-12-04 作者 Eduard Koller 这次为我们使用Linux 的朋友带来了更多关于部署云上虚拟机的消息.今天,微软开放技术有限公司 (MS Open Tech),想与大家分 ...
- Muduo-Base-TimeStamp类
Muduo的时间戳类. 主要功能: 能够获取当前的时间 能够将当前的时间以string的形式返回 能够获取两个时间戳类的时间差 能够获取当前精确的时间(微秒级) #ifndef TIMESTAMP_H ...
- asp.net 前后台交互
转自:http://www.cnblogs.com/softpower/archive/2010/11/16/1878769.html 1.如何在JavaScript访问C#函数? 2.如何在Java ...