你好,游客 登录 注册 搜索
背景:
阅读新闻

Ubuntu 编写自己的ls命令

[日期:2012-07-09] 来源:Linux社区  作者:lr-ting [字体:大 中 小]

2.ls命令实现过程用到的函数

函数原型:

1)int stat(const char *path, struct stat *buf);

  stat函数 将path(文件)的属性信息保存在 buf结构体中

2)struct passwd *getpwuid(uid_t uid);

  getpwuid函数是通过用户的uid查找用户的passwd数据,其中包括username

3)struct group *getgrgid(gid_t gid);

  getgrgid函数通过用户组gid指定的组识别码逐一搜索组文件,找到时便将该组的数据以group结构返回。

4)struct tm *localtime(const time_t *timep);

   localtime函数把从1970-1-1零点零分到当前时间系统所偏移的秒数时间转换为日历时间 。并且此函数获得的tm结构体的时间,是已经进行过时区转化为本地时间。

5)  DIR *opendir(const char *name);

  opendir()函数的作用是:打开目录句柄,返回一个文件描述符。

6)  struct dirent *readdir(DIR *dirp);

  readdir()函数读取一个目录文件描述符的信息,将信息返回到dirent结构体中。

 

struct stat 内容:

/* The stat structure.  */
struct stat {
    unsigned long   st_dev;        /* Device.  */
    unsigned long   st_ino;        /* File serial number.  */
    unsigned int    st_mode;       /* File mode.  */
    unsigned int    st_nlink;      /* Link count.  */
    unsigned int    st_uid;        /* User ID of the file's owner.  */
    unsigned int    st_gid;        /* Group ID of the file's group. */
    unsigned long   st_rdev;       /* Device number, if device.  */
    unsigned long   __pad1;
    long            st_size;       /* Size of file, in bytes.  */
    int             st_blksize;    /* Optimal block size for I/O.  */
    int             __pad2;
    long            st_blocks;     /* Number 512-byte blocks allocated. */
    int             st_atime;      /* Time of last access.  */
    unsigned int    st_atime_nsec;
    int             st_mtime;      /* Time of last modification.  */
    unsigned int    st_mtime_nsec;
    int             st_ctime;      /* Time of last status change.  */
    unsigned int    st_ctime_nsec;
    unsigned int    __unused4;
    unsigned int    __unused5;
};

struct stat中的st_mode值各个位代表的意思:

       The following flags are defined for the st_mode field:
      /*  是什么类型的文件  */
           S_IFMT     0170000   bit mask for the file type bit fields
           S_IFSOCK   0140000   socket
           S_IFLNK    0120000   symbolic link
           S_IFREG    0100000   regular file
           S_IFBLK    0060000   block device
           S_IFDIR    0040000   directory
           S_IFCHR    0020000   character device
           S_IFIFO    0010000   FIFO
           S_ISUID    0004000   set UID bit
           S_ISGID    0002000   set-group-ID bit (see below)
           S_ISVTX    0001000   sticky bit (see below)
      /* 是否有可读写权限 */ S_IRWXU
00700 mask for file owner permissions S_IRUSR 00400 owner has read permission S_IWUSR 00200 owner has write permission S_IXUSR 00100 owner has execute permission S_IRWXG 00070 mask for group permissions S_IRGRP 00040 group has read permission S_IWGRP 00020 group has write permission S_IXGRP 00010 group has execute permission S_IRWXO 00007 mask for permissions for others (not in group) S_IROTH 00004 others have read permission S_IWOTH 00002 others have write permission S_IXOTH 00001 others have execute permission

struct passwd 内容:

/* The passwd structure.  */
struct passwd
{
  char *pw_name;          /* Username.  */
  char *pw_passwd;        /* Password.  */
  __uid_t pw_uid;         /* User ID.  */
  __gid_t pw_gid;         /* Group ID.  */
  char *pw_gecos;         /* Real name.  */
  char *pw_dir;           /* Home directory.  */
  char *pw_shell;         /* Shell program.  */
};

struct group 内容:

/* The group structure.     */
struct group
  {
    char *gr_name;         /* Group name.    */
    char *gr_passwd;       /* Password.    */
    __gid_t gr_gid;        /* Group ID.    */
    char **gr_mem;         /* Member list.    */
  };
struct tm 内容:
/* Used by other time functions.  */
struct tm
{
  int tm_sec;            /* Seconds.    [0-60] (1 leap second) */
  int tm_min;            /* Minutes.    [0-59] */
  int tm_hour;            /* Hours.    [0-23] */
  int tm_mday;            /* Day.        [1-31] */
  int tm_mon;            /* Month.    [0-11] */
  int tm_year;            /* Year    - 1900.  */
  int tm_wday;            /* Day of week.    [0-6] */
  int tm_yday;            /* Days in year.[0-365]    */
  int tm_isdst;            /* DST.        [-1/0/1]*/

#ifdef    __USE_BSD
  long int tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *tm_zone;    /* Timezone abbreviation.  */
#else
  long int __tm_gmtoff;        /* Seconds east of UTC.  */
  __const char *__tm_zone;    /* Timezone abbreviation.  */
#endif
};

struct dirent内容:

struct dirent
  {
#ifndef __USE_FILE_OFFSET64
    __ino_t d_ino;
    __off_t d_off;
#else
    __ino64_t d_ino;
    __off64_t d_off;
#endif
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];        /* We must not include limits.h! */
  };
linux
相关资讯       ls 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款