如何来写自己的ls命令

来源:互联网 发布:苏沉船 王晶 知乎 编辑:程序博客网 时间:2024/05/21 09:02

如何来写自己的ls命令

作者:王老师,华清远见嵌入式学院讲师。

很多实际证明,最好的学习方法是将相关的知识点应用到具体的例子中。这样我们不仅知道了原理,也学会了怎么应用。在学习文件IO时,我们可以尝试来写ls命令。所以在写ls命令之前,我们必须要明确ls命令能做些什么,然后才能知道要怎么去写ls命令。

其实ls的参数选项很多,大多也可以组合使用。我们必须明确实现自己的ls命令不是一步就到位的,要先学会怎样去实现它的基本功能。在这里,我以最简单的

ls –l (特定的文件)

作为例子里给大家分析下如何去写linux的命令。

我们观察终端的打印信息,怎样才能按照特定的格式去输出文件的这些信息?为了完成这个特定的功能我们要完成以下两步:

1.如何获取文件信息

2.如何按格式规则去输出文件信息

下面我们来做进一步分析:

第一步,如何来获取文件信息。 
        在C库中为我们提供了一组函数用来获取文件(普通文件,目录,管道,socket,字符,块)的属性。 
        它们的函数原型 
        #include <sys/types.h> 
        #include <sys/stat.h> 
        #include <unistd.h>

int stat(const char *path, struct stat *buf); /*提供文件名字,获取文件对应属性。*/
        int fstat(int filedes, struct stat *buf); /*通过文件描述符获取文件对应的属性。*/
        int lstat(const char *path, struct stat *buf);/* 连接文件描述命,获取文件属性。*/

这里要指出的stat和lstat不同点在于对于链接文件,stat显示的是链接文件指向的实际的文件的属性,也就是返回该符号链接引用文件的信息,而lstat显示的是由返回该符号链接的有关信息

参数: path: 
        文件路径名。 filedes:文件描述词。 
        buf:是以下结构体的指针,用来描述文件对应的属性 
        struct stat 
        { 
                dev_t     st_dev;     /* 文件所在设备的标识  */ 
                ino_t     st_ino;     /* 文件结点号  */ 
                mode_t    st_mode;    /* 文件保护模式  */ 
                nlink_t   st_nlink;   /* 硬连接数  */ 
                uid_t     st_uid;     /* 文件用户标识  */ 
                gid_t     st_gid;     /* 文件用户组标识  */ 
                dev_t     st_rdev;    /* 文件所表示的特殊设备文件的设备标识  */ 
                off_t     st_size;    /* 总大小,字节为单位  */ 
                blksize_t st_blksize; /* 文件系统的块大小  */ 
                blkcnt_t  st_blocks;  /* 分配给文件的块的数量,512字节为单元  */ 
                time_t    st_atime;   /* 最后访问时间  */ 
                time_t    st_mtime;   /* 最后修改时间  */ 
                time_t    st_ctime;   /* 最后状态改变时间  */ 
        };

函数实现如下:

#include <sys/types.h> 
        #include <sys/stat.h>
        #include <time.h>
        #include <fcntl.h>
        #include <stdio.h>
        #include <pwd.h>
        #include <grp.h>
        #include <unistd.h>
        int main(int argc,char *argv[])
                { if(argc < 2 )
                        {
                                printf("commend error!\n");
                                return -1;
                        }
                        int i;
                        struct stat buf;
                        char out[500];
                        char *p;
                        if (lstat(argv[1],&buf)==-1)
                        {
                        // printf("No such file\n");
                                    return -1;
                        }
                }

这个时候我们已经得到了buf这个结构体,从这个结构体中很容易就能写出以下信息,实现起来比较容易:

//连接数 
        printf(" %d",buf.st_nlink); 
        // 用户id
        struct passwd *user;
        user=getpwuid(buf.st_uid);
        printf(" %s",user->pw_name);
        //组id
        struct group *group;
        group=getgrgid(buf.st_gid);
        printf(" %s",group->gr_name);
        //大小 
        printf(" %d ",buf.st_size);
        //时间 
        struct tm *t;
        t=localtime(&buf.st_ctime);
        printf(" %d-%d-%d %d:%d",t->tm_year+1900,
        t->tm_mon+1,
        t->tm_mday,
        t->tm_hour,
        t->tm_sec);
        printf(" %s",argv[1]);

到目前为止,还剩下文件类型,以及对于用户,组,其他用户的读写权限没有解析出来,其实我们所需要的信息已经包含在buf的st_mode中,只要对这个返回的st_mode进行解析就可以得到我们所需要的信息。

先来看看文件的类型,如何对文件类型来判断呢?可以使用掩码来解码得到文件的类型。在<sys/stat.h>中有以下定义:

#define S_IFMT 0170000
        #define S_IFREG 0100000
        #define S_IFDIR 0040000
        #define S_IFBLK 0060000
        ....
        我们如何要判断文件类型是否是普通文件,可以写如下代码: 
        if( (info.st_mode & 0170000) == 0100000)
        printf("this is a regular file");

当然我们也可采取<sys/stat.h>中定义的宏来实现 
        #define S_ISFIFO(m) ((m) &(0170000) == (0010000))
        #define S_ISREG(m) ( (m) & (0170000) == 0100000))
        ....

所以我们也可以写如下代码:

if( S_ISDIR(info.st_mode) )
        printf("this is a regular file");

现在继续补充刚刚没有写完的程序:

if (S_ISREG(buf.st_mode)) p="-";
        else if (S_ISDIR(buf.st_mode)) p="r";
        else if (S_ISCHR(buf.st_mode)) p="c";
        else if (S_ISBLK(buf.st_mode)) p="b";
        else if (S_ISFIFO(buf.st_mode)) p="f";
        else if (S_ISLNK(buf.st_mode)) p="l";
        else if (S_ISSOCK(buf.st_mode)) p="s";

还剩下文件对不同用户的权限的描述怎么实现,同理用以上的方式来实现。

int n;
        for(n=8;n>=0;n--)
        {
                if(buf.st_mode&(1<<n))
                switch (n%3)
                {
                case 2: printf("r"); break;
                case 1: printf("w"); break;
                case 0: printf("x"); break;
                }
                else
                printf("-");
        }

这样将这四段写在一起,就完成了实现ls –l 特定的文件的功能。

0 0
原创粉丝点击