作者:曹忠明,华清远见嵌入式学院讲师.
ls命令是linux下最常用的命令之一,它的使用很简单,可是功能却很多,有很多的参数,这里我们就自己写一个ls命令,实现ls基本的功能.在这之前我们先介绍几个在实现ls过程中使用的函数.
stat/lstat函数
这两个函数功能基本相同,都是获得文件的属性,区别在于如果文件是符号链接stat返回的是符号链接指向文件的属性,而lstat返回的是符号链接本身的属性.
函数原型:
int stat(const char *path, struct stat *buf);
int lstat(const char *path, struct stat *buf);
path为文件路径,buf为返回的状态,类型为struct stat,结构体内容为:
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 */
};
这个结构体中成员st_mode用来表示文件的类型和文件的权限,它的定义如下:
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 |