设为首页 加入收藏

TOP

Linux C 实现ls庖丁解牛(一)
2014-11-23 22:06:52 来源: 作者: 【 】 浏览:24
Tags:Linux 实现 庖丁解牛

一:背景



Linux下的ls可以实现什么效果呢,ls有很多的选项,最为常用的选项莫过于是-l选项,列出所有文件的详细信息。本文也着重去实现ls -l。首先看下ls -l的效果。本文将会完整的去描述怎么样一步一步去实现。


[root@bogon unix]# ls -l


total 116


-rw-r--r--. 1 root root 1063 Jul 6 20:18 aaaa


-rwxr-xr-x. 1 root root 9811 Jul 18 22:17 a.out


-rw-r--r--. 1 root root 1474 Jul 10 21:58 cp1.c


-rw-r--r--. 1 root root 386 Jul 10 21:54 exis.c


-rw-r--r--. 1 root root 601 Jul 15 22:22 fileinfo.c


-rw-r--r--. 1 root root 515 Jul 6 21:39 logout_tty.c


-rw-r--r--. 1 root root 755 Jul 12 15:21 ls1.c


-rw-r--r--. 1 root root 2625 Jul 18 22:17 ls2.c


----------. 1 root root 1063 Jul 3 21:31 more01.c


-rwxrwxrwx. 1 root root 1651 Jul 5 21:48 more02.c


-rw-r--r--. 1 root root 270 Jul 15 22:16 stattest.c


-rw-r--r--. 1 root root 262 Jul 4 21:51 test1.c


-rw-r--r--. 1 root root 1337 Jul 12 14:19 test2.c


-rw-r--r--. 1 root root 140 Jul 12 14:11 test3.c


-rw-r--r--. 1 root root 527 Jul 3 22:19 test.c


-rw-r--r--. 1 root root 169 Jul 7 22:42 ttytest.c


-rw-r--r--. 1 root root 955 Jul 7 20:54 utmplib.c


-rw-r--r--. 1 root root 87 Jul 6 21:13 utmplib.h


-rw-r--r--. 1 root root 2688 Jul 6 21:12 utmplib.o


-rwxr-xr-x. 1 root root 980 Jul 12 14:22 who1.c


-rwxr-xr-x. 1 root root 9576 Jul 6 21:13 who2


-rw-r--r--. 1 root root 1791 Jul 12 13:15 who2.c


-rw-r--r--. 1 root root 2720 Jul 6 21:13 who2.o


-rw-r--r--. 1 root root 1424 Jul 12 12:45 who_am_i.c


-rw-r--r--. 1 root root 80 Jul 7 22:43 whoami.c



这是一个使用ls -l选项列出的的文件信息,可以看出ls -l列出了所有文件的 属性,连接,属主,属组,大小,ctime以及文件名等信息。要获取文件的这些信息并进去适当的格式化,就是本文所要做的事情了。


------------------------------分割线------------------------------


C语言梳理一下,分布在以下10个章节中:


二:开始实现


要想自己实现ls -l就得知道可以通过什么系统调用可以获取到这些文件信息。


首先自然是要通过man来查找相关的系统调用。


man -k file|grep status


man -k file|grep infomation


man -k file |grep info


不停的换上多个关键词来搜索,通过上面的搜索就可以得到stat这个系统调用来获取文件信息。


紧接着就可以man 2 stat来获取系统调用的详细使用方法。


#include


#include


#include


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


struct stat {


dev_t st_dev; /* ID of device containing file */


ino_t st_ino; /* inode number */


mode_t st_mode; /* protection */


nlink_t st_nlink; /* number of hard links */


uid_t st_uid; /* user ID of owner */


gid_t st_gid; /* group ID of owner */


dev_t st_rdev; /* device ID (if special file) */


off_t st_size; /* total size, in bytes */


blksize_t st_blksize; /* blocksize for file system I/O */


blkcnt_t st_blocks; /* number of 512B blocks allocated */


time_t st_atime; /* time of last access */


time_t st_mtime; /* time of last modification */


time_t st_ctime; /* time of last status change */


};



到了这里处理起来似乎很顺畅了,只要获取相应字段进行格式处理就OK了。


三:权限处理


st_mode就是文件的权限部分,对于这个部分要把其处理成ls -l列出的那种形式。


st_mode本身就是一个16位的二进制,前四位是文件的类型,紧接着三位是文件的特殊权限,最后的九位就是ls -l列出来的九个权限。如何把st_mode转换成对应的权限就是权限处理这块的关键了。


Linux本身提供了很多测试宏来测试文件的类型的。


#define __S_IFMT 0170000 /* These bits determine file type. */


/* File types. */


#define __S_IFDIR 0040000 /* Directory. */


#define __S_IFCHR 0020000 /* Character device. */


#define __S_IFBLK 0060000 /* Block device. */


#define __S_IFREG 0100000 /* Regular file. */


#define __S_IFIFO 0010000 /* FIFO. */


#define __S_IFLNK 0120000 /* Symbolic link. */


#define __S_IFSOCK 0140000 /* Socket. */



# define S_IFMT __S_IFMT
# define S_IFDIR __S_IFDIR
# define S_IFCHR __S_IFCHR
# define S_IFBLK __S_IFBLK
# define S_IFREG __S_IFREG



利用上面的测试宏就可以判断文件的类型,至于文件的权限部分可以使用掩码的方式来处理

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Flask-mail扩展的基本使用 下一篇Python写WEB日志分析程序的一些思..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: