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. orangepi获取cpu温度

    cat /sys/devices/virtual/hwmon/hwmon1/temp1_input

  2. SQL+C#:一次多语言混合编程的经验总结

    1.用JAVA做,采取轮询策略: 2.用sql语言+C#混合编程,采取触发策略

  3. WebSocket转载

    目录   概述  WebSocket 客户端  WebSocket 服务端  WebSocket 代理  FAQ  完整示例  资料 概述 WebSocket 是什么? WebSocket 是一种网络 ...

  4. SQL PLUS 远程连接数据库

    -- SQL PLUS 远程连接Oracle数据库(WINDOWS+SQL PLUS)命令:用户名/密码@ip地址[:端口]/service_name [as sysdba] EG: ORCL/ORC ...

  5. Springboot手动获取bean

    使用如下工具类即可 package com.rio.ums.spa.commons.utils; import org.springframework.beans.BeansException; im ...

  6. [ipsec][strongswan] strongswan源码分析--(〇)总体架构图

    history: 2019-06-05, 增加配置文件解析部分. 2019-06-05,增加plugin优先级排序部分. charon进程初始化阶段的流程图 约定: 实线代表流程图. 虚线代表调用栈, ...

  7. Python标准库-datatime和time

    Python标准库-datatime和time 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.标准库datatime 1>.datatime模块 #!/usr/bin/e ...

  8. python中的logging日志模块

    日志是程序不可或缺的一部分.它可以记录程序的运行情况,帮助我们更便捷地发现问题,而python中的logging日志模块给我们提供了这个机会. logging给我们提供了五种函数用来输出日志:debu ...

  9. 【Java】《Java程序设计基础教程》第五章学习

    5.1 抽象类 Java语言中,父类的某些方法不包括任何逻辑,并且需要由子类重写.在这种情况下,用abstract关键字来修饰一个类时,这个类叫做抽象类,用abstract关键字来修饰一个方法时,这个 ...

  10. test20190815 NOIP2019 模拟题

    100+60+40=200,被后面两个题卡着我很不爽. 立方数 [问题描述] 作为 XX 战队的狂热粉丝,MdZzZZ 看到了自己心仪的队伍在半决赛落败,顿时 心灰意冷.看着自己手中的从黄牛那里抢来的 ...