1、ls能做什么

ls的默认动作是找到当前目录中所有文件的文件名,按字典序排序后输出,ls -l会列出每个文件的详细信息。所以ls做了一下两件事,第一列出目录的内容,第二显示文件的信息。

自己编写一个ls,一下三点需要掌握:

  • 如何列出目录内容
  • 如何读取并显示文件属性
  • 给一个名字,如何能够判断出它是目录还是文件

2、ls是如何实现的

ls的逻辑和who十分相似,主要区别是who从文件中读数据,而ls从目录中读数据。ls的工作流程:打开目录,读取每个条目,展示文件的信息,关闭目录。

目录是文件的列表,记录的序列,每条记录对应一个文件或子目录。通过readdir来读取目录中的记录,readdir返回一个指向目录的当前记录的指针,记录的类型是struct dirent,这个结构定义在/usr/include/dirent.h中,dirent结构中成员d_name用于存放文件名。

struct dirent:

ino_t  d_ino       File serial number.

char   d_name[]    Name of entry.

以上相当于完成了ls的第一件事列出目录内容。ls的第二件事ls -l显示文件的详细信息。ls -l每行总共包含7个字段:

  • 模式(mode):文件访问权限,针对3种对象用户、同组用户、其他用户的读、写、执行权限总共九种文件访问权限
  • 链接数(links):该文件被引用的次数
  • 文件所有者(ower):文件所有者的用户名
  • 组(group):文件所有者所在组
  • 大小(size):文件的大小
  • 最后修改时间(last-modified):文件的最后修改时间
  • 文件名(name):文件名

stat、chmod、chown、utime系统调用:

  • stat():用于得到文件的属性,result=stat(char *fname,struct stat *bufp),stat把文件fname的信息复制到指针bufp所指的结构中。
  • chmod():修改文件的许可权限和特殊属性,result=chmod(char *path,mode_t mode),将文件path的属性修改为mode。
  • chown():修改文件所有者和组,result=chown(char *path,uid_t owner,gid_t group),将文件的所有者id改为owner,文件的组id改为group。
  • utime():修改文件最后修改时间和最后访问时间,utime(char *path,struct utimebuf *newtime),将文件path的最后修改时间和最后访问时间修改为指向newtime的utimebuf结构体。

通过命令man -k file | grep -i stat查找有关文件状态的系统调用。用stat可以得到文件的信息,通过系统调用stat将文件filename的属性信息存入结构struct stat中去。struct stat的成员变量有:

  • st_mode:16位,4位文件类型、3位文件特殊属性、9位文件许可权限,可以通过掩码获取9位文件许可权限
  • st_uid:用户所有者的ID,可以通过系统调用getpwuid获取用户所有者的ID对应的用户名
  • st_gid:所在组ID,可以通过系统调用getgrgid获取用户所在组的ID对应的组名
  • st_size:所占字节数
  • st_nlink:文件链接数
  • st_mtime:文件最后修改时间
  • st_atime:文件最后访问时间
  • st_ctime:文件属性最后改变时间

dev_t     st_dev     Device ID of device containing file.
              ino_t     st_ino     File serial number.
              mode_t    st_mode    Mode of file (see below).
              nlink_t   st_nlink   Number of hard links to the file.
              uid_t     st_uid     User ID of file.
              gid_t     st_gid     Group ID of file.

dev_t     st_rdev    Device ID (if file is character or block special).

off_t     st_size    For regular files, the file size in bytes.
                                   For symbolic links, the length in bytes of the
                                   pathname contained in the symbolic link.

For a shared memory object, the length in bytes.

For a typed memory object, the length in bytes.

For other file types, the use of this field is
                                   unspecified.
              time_t    st_atime   Time of last access.
              time_t    st_mtime   Time of last data modification.
              time_t    st_ctime   Time of last status change.

blksize_t st_blksize A file system-specific preferred I/O block size for
                                   this object. In some file system types, this may
                                   vary from file to file.
              blkcnt_t  st_blocks  Number of blocks allocated for this object.

3、自己编写一个ls

ls1.c

#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
void do_ls(char dirname[])
{DIR *dir_ptr;struct dirent* direntp;if((dir_ptr=opendir(dirname))==NULL)fprintf(stderr,"ls1:cannot open %s\n",dirname);else{while((direntp=readdir(dir_ptr))!=NULL)printf("%s\n",direntp->d_name);closedir(dir_ptr);}
}
int main(int argc,char *argv[])
{if(argc==1)do_ls(".");else{while(--argc){printf("%s:\n",*++argv);do_ls(*argv);}}return 0;
}

ls2.c

#include<stdio.h>
#include<sys/types.h>
#include<dirent.h>
#include<sys/stat.h>
void do_ls(char dirname[])
{DIR *dir_ptr;struct dirent *direntp;if((dir_ptr=opendir(dirname))==NULL)fprintf(stderr,"ls1:cannot open %s\n",dirname);else{while((direntp=readdir(dir_ptr))!=NULL)dostat(direntp->d_name);closedir(dir_ptr);}
}
void dostat(char *filename)
{struct stat info;if(stat(filename,&info)==-1)perror(filename);elseshow_file_info(filename,&info);
}
void show_file_info(char *filename,struct stat *info_p)
{char *uid_to_name(),*gid_to_name(),*ctime(),*filemode();  //先声明两个函数char modestr[11];mode_to_letters(info_p->st_mode,modestr);printf("%s",modestr);printf("%4d ",(int)info_p->st_nlink);printf("%-8s ",uid_to_name(info_p->st_uid));printf("%-8s ",gid_to_name(info_p->st_gid));printf("%8ld ",(long)info_p->st_size);printf("%.12s ",4+ctime(&info_p->st_mtime));printf("%s\n",filename);
}
void mode_to_letters(int mode,char str[])
{strcpy(str,"----------");if(S_ISDIR(mode)) str[0]='d';if(S_ISCHR(mode)) str[0]='c';if(S_ISBLK(mode)) str[0]='b';if(mode&S_IRUSR) str[1]='r';if(mode&S_IWUSR) str[2]='w';if(mode&S_IXUSR) str[3]='x';if(mode&S_IRGRP) str[4]='r';if(mode&S_IWGRP) str[5]='w';if(mode&S_IXGRP) str[6]='x';if(mode&S_IROTH) str[7]='r';if(mode&S_IWOTH) str[8]='w';if(mode&S_IXOTH) str[9]='x';}
#include<pwd.h>
char *uid_to_name(uid_t uid)
{struct passwd *pw_ptr;static char numstr[10];if((pw_ptr=getpwuid(uid))==NULL){sprintf(numstr,"%d",uid);return numstr;}elsereturn pw_ptr->pw_name;
}
#include<grp.h>
char *gid_to_name(gid_t gid)
{struct group *grp_ptr;static char numstr[10];if((grp_ptr=getgrgid(gid))==NULL){sprintf(numstr,"%d",gid);return numstr;}elsereturn grp_ptr->gr_name;
}
int main(int argc,char *argv[])
{if(argc==1)do_ls(".");else{while(--argc){printf("%s:\n",*++argv);do_ls(*argv);}}return 0;
}

04 ls——stat、chmod、chown、utime相关推荐

  1. chgrp、chmod和chown命令-改变文件的权限和属性

    chgrp.chmod和chown命令-改变文件的权限和属性 如何改变文件的权限和属性 改变所属群组:chgrp 改变文件拥有者:chown 改变权限:chmod ♡\color{pink}{\hea ...

  2. chmod g+s 、chmod o+t 、chmod u+s:Linux高级权限管理

    关于linux下权限操作chmod的一些说明!比rxw高级内容! 转载自http://blog.chinaunix.net/uid-26642180-id-3378119.html Set uid, ...

  3. chmod g+s 、chmod o+t 、chmod u+s

    Set uid, gid,sticky bit的三个权限的详细说明 一个文件都有一个所有者, 表示该文件是谁创建的. 同时, 该文件还有一个组编号, 表示该文件所属的组, 一般为文件所有者所属的组. ...

  4. Linux中关于useradd、chmod、chown、getfacl、setfact等权限设置

    Linux中关于useradd.chmod.chown.getfacl.setfact等权限设置 文章目录: 一.Linux用户管理 1.用户(user).用户组(group).其他用户概念(othe ...

  5. 文件/目录权限相关命令:chmod、chown、umask、lsattr/chattr命令解析

    2019独角兽企业重金招聘Python工程师标准>>> 本文索引: 文件/目录权限修改:chmod 预备知识 几种具体用法 重要参数: -R 所有者/所属组修改: chown 几种具 ...

  6. Linux中chown、chmod、chgrp的区别和用法

    chmod修改第1列内容,chown修改第3.4列内容,chgrp修改第4列内容: 先从文件属性开始. 目录 文件属性详解 chown chmod chgrp 文件属性详解 权限的计算是除去第一位字母 ...

  7. Linux学习笔记1:文件权限和chgrp、chown、chmod指令

    文件权限 Linux中文件属性都有所有者owner.所有者所在群组的其他成员group.其他群组的使用者others. Linux下中ls -al指令能够查看所有文件的名字以及相关的属性. 使用该指令 ...

  8. linux命令chmod、chown、chgrp详解

    介绍 权限介绍:Linux系统中的每个文件和目录都有访问许可权限,分为只读,只写和可执行三种.文件被创建时,文件所有者自动拥有对该文件的读.写和可执行权限. 用户介绍:Linux系统中有三种不同类型的 ...

  9. linux权限命令chgrp,Linux常用命令之用户权限管理chmod、chown、chgrp、umask命令讲解...

    这节课我们重点来学习权限管理命令,说到权限大家可能第一时间能想到的就是读.写.执行 rwx 三种权限,在正式讲解权限命令之前,先简单的介绍一下rwx权限对于文件和目录的不同含义.权限字符权限对文件的权 ...

最新文章

  1. linux shell下获取cpu温度
  2. 【NCEPU】韩绘锦:图信号处理与图卷积神经网络
  3. Spring进阶的几大要点,你有做到吗?
  4. GLSL实现图像处理
  5. 怎么寻回执行页内操作时的错误磁盘的数据
  6. 5种处理js跨域问题方法汇总(转载)
  7. 第一百二十二期:大数据分析:红包先抢好,还是后抢好
  8. python批量转换图片格式_python批量将图片转换为JPEG格式
  9. [Kaggle] Housing Prices 房价预测
  10. 5.2.1 标准原子类型
  11. Android开发/源码资源汇总
  12. 三星android p内测,两年前老机重生!三星开启Galaxy C9 Pro升级安卓8.0内测
  13. Mifare 1卡(M1卡、IC卡)读写操作及工作原理整理
  14. linux编写弹球游戏,汇编写的DOS弹球游戏
  15. 如何在MSDN上获取Win7镜像
  16. 一文详解SQL关联子查询
  17. 程序阅读_全面详解LTE:MATLAB建模仿真与实现_自学笔记(1)调制与编码_程序阅读
  18. 算术,逻辑左移右移(转)
  19. 如何解决“由于无法验证发行者,所以WINDOWS已经阻止此软件”
  20. SPA(单页面)和MPA(多页面)的区别与优缺点

热门文章

  1. C++ 实现康拓展开(leetcode 60)
  2. web书店的设计与实现毕业设计
  3. rm -fr * 数据恢复
  4. 取模软件 模拟显示验证取模数据正确性 逆向 把点阵数组bin文件转显示定位
  5. 基于SPN的专线业务承载方案
  6. 角逐利器 MBR一体化污水处理设备实现市政污水固液分离
  7. [记]SAF 中缓存服务的实现
  8. 面向对象基础 python
  9. 高交会20周年盛典来袭 治水提质主题展为环保事业打Call
  10. mysql error1406_MySQL插入中文时出现ERROR 1406 (22001): Data too long for column 'name' at row 1 (转)...