码迷,mamicode.com
首页 > 其他好文 > 详细

《UNIX环境高级编程》读书笔记之文件与目录(2)

时间:2014-09-13 12:07:55      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   os   ar   for   文件   2014   sp   

动手练习:

(1)自己实现ls命令

#include <dirent.h>
#include <string.h>

int ls(int argc,char * argv[])
{
    int i;
    for(i = 2;i < argc;i++)
    {
     DIR * dp;
     struct dirent *dirp;

     if((dp = opendir(argv[i])) == NULL)
       err_sys("can't open %s\n",argv[i]);
     else
       printf("%s:\n",argv[i]);
     while((dirp = readdir(dp)) != NULL)
    {
        if(dirp->d_name[0] != '.')
            printf("%s\t",dirp->d_name);
    }
     printf("\n");
     closedir(dp);
    }

    return 0;
}
</span>

(2)实现简单的stat语句

#include "apue.h"
#include <unistd.h>
#include <time.h>

int get_stat(int argc,char * argv[])
{
  struct stat buf;
  char * type;
  off_t size;
  blkcnt_t blkcount;
  blksize_t blksize;
  dev_t dev;
  dev_t rdev;
  ino_t ino;
  nlink_t nlink;
  uid_t uid;
  gid_t gid;
  time_t a_time;
  time_t m_time;
  time_t c_time;
  char user[4];
  char group[4];
  char other[4];

  int i;
  for(i = 2;i < argc;i++)
  {
      if(lstat(argv[i],&buf) < 0){
        printf("lstat error\n");
        return -1;
  }

  if(S_ISREG(buf.st_mode))
    type = "regular";
  else if(S_ISDIR(buf.st_mode))
    type = "directory";
  else if(S_ISCHR(buf.st_mode))
    type = "character special";
  else if(S_ISBLK(buf.st_mode))
    type = "block special";
  else if(S_ISFIFO(buf.st_mode))
    type = "fifo";
  else if(S_ISLNK(buf.st_mode))
    type = "symbolic link";
  else if(S_ISSOCK(buf.st_mode))
    type = "socket";

  size = buf.st_size;
  blkcount = buf.st_blocks;
  blksize = buf.st_blksize;
  dev = buf.st_dev;
  rdev = buf.st_rdev;
  ino = buf.st_ino;
  nlink = buf.st_nlink;
  uid = buf.st_uid;
  gid = buf.st_gid;
  a_time = buf.st_atime;
  m_time = buf.st_mtime;
  c_time = buf.st_ctime;

  if(buf.st_mode & S_IRUSR)
    user[0] = 'r';
  else
    user[0] = '-';
  if(buf.st_mode & S_IWUSR)
    user[1] = 'w';
  else
    user[1] = '-';
  if(buf.st_mode & S_IXUSR)
    user[2] = 'x';
  else
    user[2] = '-';

  if(buf.st_mode & S_IRGRP)
    group[0] = 'r';
  else
    group[0] = '-';
  if(buf.st_mode & S_IWGRP)
    group[1] = 'w';
  else
    group[1] = '-';
  if(buf.st_mode & S_IXGRP)
    group[2] = 'x';
  else
    group[2] = '-';

  if(buf.st_mode & S_IROTH)
    other[0] = 'r';
  else
    other[0] = '-';
  if(buf.st_mode & S_IWOTH)
    other[1] = 'w';
  else
    other[1] = '-';
  if(buf.st_mode & S_IXOTH)
    other[2] = 'x';
  else
    other[2] = '-';

  user[3] = '\0';
  group[3] = '\0';
  other[3] = '\0';


  printf("File:%s\n",argv[i]);
  printf("Size:%d\n",size);
  printf("Blocks:%d\n",blkcount);
  printf("blksize:%d\n",blksize);
  printf("%s\n",type);
  printf("Device:%d\\%d\n",major(dev),minor(dev));
  printf("Inode:%d\n",ino);
  printf("Links:%d\n",nlink);
  printf("Access:%s%s%s\n",user,group,other);
  printf("Userid:%d\n",uid);
  printf("Groupid:%d\n",gid);
  printf("Access Time:%s",ctime(&a_time));
  printf("Modify Time:%s",ctime(&m_time));
  printf("Change Time:%s",ctime(&c_time));
  }

}


程序截图:

bubuko.com,布布扣

《UNIX环境高级编程》读书笔记之文件与目录(2)

标签:style   blog   http   os   ar   for   文件   2014   sp   

原文地址:http://blog.csdn.net/yao_wust/article/details/39250063

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!