ls命令的简单实现

目标:简单的实现ls命令

实现的mic_ls命令主要功能

1.循环遍历目录

2.列出目标目录所有的子目录和文件

3.列出文件的文件权限,所有者,文件大小等详细信息

参数

-r 循环遍历

-a 列出全部文件

-l 列出详细信息(文件类型、权限。。。)

环境

ubuntu 14.04 gcc.real (Ubuntu 4.8.2-19ubuntu1) 4.8.2

代码

新手菜鸟 如有错误 欢迎指正

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdbool.h>
#include<dirent.h>
#include<string.h>
#include<sys/stat.h>
#include<pwd.h>
#include<grp.h>
bool show_all=false;
bool show_list=false;
bool show_recursion=false;
void do_ls_r(char *pathname,int depth);
void do_ls(char *pathname);
int judge_dir(char *pathname);
void do_showlist(DIR *dp);
void mode_to_letters(int mode,char str[]);
char *uid_to_name(uid_t uid);
char *gid_to_name(gid_t gid); int main(int argc,char *argv[])
{
int opt;
while((opt=getopt(argc,argv,"arl"))!=-1){
switch(opt){
case 'a':
show_all=true;
break;
case 'r':
show_recursion=true;
break;
case 'l':
show_list=true;
break;
case '?':
printf("unknow option :%c\n",optopt);
exit(1);
}
}
if(show_recursion){
if(argc==optind)
do_ls_r(".",4);
else{
for(;optind<argc;optind++){
if(judge_dir(argv[optind])){
printf("%s:\n",argv[optind]);
do_ls_r(argv[optind],4);
}
else{
printf("not a directory!\n");
exit(1);
}
}
}
}
else{
if(argc==optind)
do_ls(".");
else{
for(;optind<argc;optind++){
if(judge_dir(argv[optind])){
printf("%s:\n",argv[optind]);
do_ls(argv[optind]);
}
else{
printf("not a directory!\n");
exit(1);
}
}
}
} } int judge_dir(char *pathname)
{
struct stat statbuf;
if((lstat(pathname,&statbuf))<0){
printf("error in lstat!\n");
exit(1);
}
if(S_ISDIR(statbuf.st_mode))
return 1;
else
return -1;
}
void do_ls_r(char *pathname,int depth)
{
DIR *dp;
struct dirent *entry;
struct stat statbuf;
if((dp=opendir(pathname))==NULL){
printf("can't open dir:%s\n",pathname);
exit(1);
}
chdir(pathname);
while((entry=readdir(dp))!=NULL){
lstat(entry->d_name,&statbuf);
if(S_ISDIR(statbuf.st_mode)){
if(strcmp(".",entry->d_name)==0||strcmp("..",entry->d_name)==0)
continue;
printf("%*s%s/\n",depth," ",entry->d_name);
do_ls_r(entry->d_name,depth+4);
}
else
printf("%*s%s\n",depth," ",entry->d_name);
}
chdir("..");
closedir(dp); }
void do_ls(char *pathname)
{
DIR *dp;
struct dirent *entry;
if((dp=opendir(pathname))==NULL){
printf("can't open dir:%s\n",pathname);
exit(1);
}
if(show_list){
chdir(pathname);
do_showlist(dp);
}
else{
while((entry=readdir(dp))!=NULL){
if(!show_all)
if(*(entry->d_name)=='.')
continue;
printf("%s\n",entry->d_name);
}
closedir(dp);
}
}
void do_showlist(DIR *dp)
{
struct dirent *entry;
struct stat statbuf;
char l_mode[11];
while((entry=readdir(dp))!=NULL){
if((lstat(entry->d_name,&statbuf))<0){
printf("error :in lstat\n");
exit(1);
}
if(!show_all)
if(*(entry->d_name)=='.')
continue;
mode_to_letters(statbuf.st_mode,l_mode);
printf("%s",l_mode);
printf("%4d ",(int)statbuf.st_nlink);
printf("%-8s ",uid_to_name(statbuf.st_uid));
printf("%-8s ",gid_to_name(statbuf.st_gid));
printf("%8ld ",(long)statbuf.st_size);
printf(" %s ",entry->d_name);
printf("\n");
}
}
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';
} char *uid_to_name(uid_t uid)
{
struct passwd *getpwuid(),*pw_ptr;
if((pw_ptr=getpwuid(uid))!=NULL)
return (pw_ptr->pw_name);
else
return NULL;
}
char *gid_to_name(gid_t gid)
{
struct group *getgrid,*grp_ptr;
if((grp_ptr=getgrgid(gid))!=NULL)
return (grp_ptr->gr_name);
else
return NULL;
}

ls命令的简单实现的更多相关文章

  1. Linux系统编程_1_文件夹读取(实现简单ls命令)

    闲来无事.随便写写,实现简单的ls命令: | 1 #include <stdio.h> | 2 #include <stdlib.h> | 3 #include <dir ...

  2. Linux/UNIX编程:使用C语言实现简单的 ls 命令

    刚好把 Linux/UNIX 编程中的文件和IO部分学完了,就想编写个 ls 命令练习一下,本以为很简单,调用个 stat 就完事了,没想到前前后后弄了七八个小时,90%的时间都用在格式化(像 ls ...

  3. Linux命令系列之ls——原来最简单的ls这么复杂

    Linux命令系列之ls--原来最简单的ls这么复杂 ls命令应该是我们最常用的命令之一了,但是我们可能容易忽略那些看起来最简单的东西. 简介 ls命令的使用格式如下 ls [选项] [具体的文件] ...

  4. linux下的ls命令

    在LINUX系统中有一个重要的概念:一切都是文件.其实这是UNIX哲学的一个体现,而Linux是重写UNIX而来,所以这个概念也就传承了下来.在UNIX系统中,把一切资源都看作是文件,包括硬件设备.U ...

  5. Linux下ls命令显示符号链接权限为777的探索

    Linux下ls命令显示符号链接权限为777的探索 --深入ls.链接.文件系统与权限 一.摘要 ls是Linux和Unix下最常使用的命令之一,主要用来列举目录下的文件信息,-l参数允许查看当前目录 ...

  6. ls命令的20个实用范例

    contents ls -l -h -lhS -l --block-size=M -a -d */ -g -G -n --color=never -i -p -r -R -t ls ~ ls --ve ...

  7. linux ls 命令

    ls 命令是 Linux 下最常用的命令之一,用来查询目录下的内容(list directory contents).本文将介绍其基本的用法和一些典型的用例.笔者使用的测试环境为 ubuntu 16. ...

  8. shell的 ls命令

    Linux下shell 的 ls 命令 ls -d 显示当前目录的上层目录,不显示子目录 ls -a 显示当前目录下的所有子目录,包括隐藏的文件 ls -l 显示当前目录下所有文件的所有信息(除隐藏文 ...

  9. Hadoop Ls命令添加显示条数限制參数

    前言 在hadoop的FsShell命令中,预计非常多人比較经常使用的就是hadoop fs -ls,-lsr,-cat等等这种与Linux系统中差点儿一致的文件系统相关的命令.可是细致想想,这里还是 ...

随机推荐

  1. vue项目配置Mock.js

    扯在前面 最近一直在忙跳槽的事情,博客也好久没有更新了,上次更新还是去年,不出意外的话,从今天起继续今年的博客之旅. 今天继续完善我之前的项目架构,从零开始搭建vue移动端项目到上线,有需要的同学可以 ...

  2. python 内建属性

    在python中创建一个类,它不仅有我们自定义的属性和方法,还有与生俱来的一些属性和方法,我们叫它内建属性. 下面是类常用内建属性列表. 常用专有属性 说明 触发方式 __init__ 构造初始化函数 ...

  3. Oracle 多表插入

    多表插入 作用:一条INSERT语句可以完成向多张表的插入任务(Multitable insert).有两种形式:insert all与insert first,准备测试环境:1.创建表T并初始化测试 ...

  4. Django异常 - ImportError: No module named django.core.management

    Django错误 - ImportError: No module named django.core.management问题描述:在命令行输入 manage.py runserver,提示找不到d ...

  5. MQTT的websockets应用_转

    转自:mosquitto 与websocket 的结合 前言 mosquitto 作为一个消息代理, 客户端与 mosquitto 服务端的通信时基于 MQTT 协议的, 而现在的主流 web 应用时 ...

  6. 【转】高性能网络编程2----TCP消息的发送

    在上一篇中,我们已经建立好的TCP连接,对应着操作系统分配的1个套接字.操作TCP协议发送数据时,面对的是数据流.通常调用诸如send或者write方法来发送数据到另一台主机,那么,调用这样的方法时, ...

  7. windows系统开机执行文件

    以下都是在windows系统下执行的 开机自启程序 新建xxx.bat的文件 编辑自己的bat文件,将所要加载的项目引导进来 注意: 如果有相对路径的话,就需要先加载项目,在用python 执行脚本程 ...

  8. 《BUG创造队》作业8:软件测试与Alpha冲刺(第三天)

    项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 BUG创造队 作业学习目标 (1)掌握软件测试基础技术.(2)学习 ...

  9. 正确robots写法,解决百度搜索不显示缩略图问题

    网站上线http://zhimo.yuanzhumuban.cc/有一年左右时间了,百度搜索显示略缩图少之又少,通过自己这几天的观察发现. 结合百度站长平台的 robots 工具和抓取诊断工具检查后, ...

  10. destoon修改搜索页面标题方法

    最近研究destoon内核开发,发现destoon6.0的搜索页面模块的标题太长,是搜索标题+模块+首页标题,这样导致标题过长,百度不喜欢,所以我经过修改成百度所喜欢的. 修改前截图: 修改的文件:/ ...