Chinaunix首页 | 论坛 | 博客
  • 博客访问: 119604
  • 博文数量: 29
  • 博客积分: 1215
  • 博客等级: 中尉
  • 技术积分: 305
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-05 16:29
文章分类
文章存档

2010年(29)

我的朋友

分类: C/C++

2010-12-24 10:45:53

程序清单:

#include <stdio.h> //for printf

#include <stdlib.h> //for exit

#include <unistd.h> //for access

#include <fcntl.h> //for open


int main(int argc,char *argv[])
{
    if(argc != 2)
    {
        printf("usage:./a.out ");
        exit(1);    
    }
    if(access(argv[1],R_OK) < 0)
    {
        printf("access error for %s\n",argv[1]);
        exit(1);    
    }
    else
    {
        printf("read access OK\n");    
    }
    if(open(argv[1],O_RDONLY) < 0)
    {
        printf("open error for %s\n",argv[1]);
        exit(1);    
    }
    else
    {
        printf("open for reading OK\n");    
    }
    exit(0);
}

程序执行结果:
obe-240 test/linuxc> gcc access.c
obe-240 test/linuxc> ls -l a.out
-rwxr-xr-x 1 eagle hitv 9808 2010-12-24 10:31 a.out
obe-240 test/linuxc> ./a.out a.out
read access OK
open for reading OK
obe-240 test/linuxc> ls -l /etc/shadow
-rw-r----- 1 root shadow 1361 2010-10-19 19:22 /etc/shadow
obe-240 test/linuxc> ./a.out /etc/shadow
access error for /etc/shadow
阅读(1880) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~