Chinaunix首页 | 论坛 | 博客
  • 博客访问: 699953
  • 博文数量: 94
  • 博客积分: 2812
  • 博客等级: 少校
  • 技术积分: 1555
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-08 21:28
文章分类

全部博文(94)

文章存档

2012年(23)

2011年(39)

2010年(14)

2009年(18)

分类: C/C++

2009-11-10 13:11:23

一、设计一个程序,要求判断“/etc/passwd”的文件类型。
使用st_mode属性,可以使用几个宏来判断:
S_ISLNK(st_mode)是否是一个连接,S_ISREG是否是一个常规文件,
S_ISDIR是否是一个目录,S_ISFIFO是否是一个FIFO文件,S_ISSOCK
是否是一个SOCKET文件;
并且要求打开文件“/etc/passwd”,判断它的最后一次访问时间。

 

 


#include
#include
#include
#include
#include

void what_is( int);

int main()
{

    struct stat info;

    if( stat("/etc/passwd", &info) == -1 )
 perror("etc/passwd");

    else
 what_is( info.st_mode );

    printf("last access time is %s\n", ctime(&info.st_mtime));   //以字符串的形式打印时间;

    return 0;

}


void what_is( int mode )
{
    if( S_ISDIR(mode) ) printf("/etc/passwd is Directory file.\n");    //stat_mode返回的是条件中的字符串;
    if( S_ISFIFO(mode) ) printf("/etc/passwd is Fifo file.\n");
    if( S_ISLNK(mode) ) printf("/etc/passwd is link file.\n");
    if( S_ISREG(mode) )  printf("/etc/passwd is regular file.\n");
   if( S_ISSOCK(mode) )  printf("/etc/passwd is socket file.\n");
}

阅读(4629) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~