ls 操作命令 -l/-R和rm -r dir 功能实现
ls -R
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h> int do_ls(const char *dir)
{
char dir_name[];
DIR *dirp;
struct dirent *dp;
struct stat dir_stat; if ( != access(dir, F_OK) )
{
return ;
} if ( > stat(dir, &dir_stat) )
{
perror("get directory stat error");
return -;
} if ( S_ISDIR(dir_stat.st_mode) )
{
dirp = opendir(dir);
printf("%s:\n",dir);
int count = ;
while ( (dp=readdir(dirp)) != NULL )
{ ++count;
if ( ( == strcmp(".", dp->d_name)) || ( == strcmp("..", dp->d_name)) ) {
continue;
}
printf("%s\t",dp->d_name);
if( == count)
{
printf("\n");
count = ;
} }
printf("\n---\n");
rewinddir(dirp); while ( (dp=readdir(dirp)) != NULL )
{
if ( ( == strcmp(".", dp->d_name)) || ( == strcmp("..", dp->d_name)) ) {
continue;
} char buf[] = {};
sprintf(buf,"%s/%s",dir,dp->d_name);
do_ls(buf); }
} }
int main()
{
do_ls(".");
}
rm -r
1 #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <libgen.h>
#include <string.h>
void rmr(char* path)
{
DIR* dir = opendir(path);
if(dir == NULL)
perror("opendir"),exit(-);
struct dirent* ent;
char buf[];
while((ent=readdir(dir)))
{
if(ent->d_type == )
{
if(strcmp(ent->d_name,".")==||strcmp(ent->d_name,"..")==)
continue;
sprintf(buf,"%s/%s",path,ent->d_name); rmr(buf); }
if(ent->d_type == )
{
sprintf(buf,"%s/%s",path,ent->d_name);
if(remove(buf)!=) perror("remove"),exit(-);
}
}
if(rmdir(path)!=) perror("rmdir"),exit(-);
}
int main(int argc,char* argv[])
{
if(argc != )
{
printf("Usage:%s directory name",basename(argv[]));
exit(-);
} rmr(argv[]);
printf("rm -r %s success.\n",argv[]);
return ;
}
ls -l
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h> void show_file_info(char* filename, struct stat* info_p)
{
char* uid_to_name(), *ctime(), *gid_to_name(), *filemode();
void mode_to_letters();
char modestr[]; 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", + ctime(&info_p->st_mtime));
struct passwd *curr;
curr = getpwuid(getuid()); if(curr->pw_gid == info_p->st_gid && info_p->st_mode & S_IXGRP )
{
printf("\033[0;32m");
} if(curr->pw_gid == info_p->st_gid && curr->pw_uid == info_p->st_uid && info_p->st_mode & S_IXUSR )
{
printf("\033[0;32m");
} if(info_p->st_mode & S_IXOTH)
{
printf("\033[0;32m");
} printf(" %s\n",filename);
printf("\033[0m");
} void mode_to_letters(int mode, char str[])
{
strcpy(str,"----------"); if (S_ISDIR(mode))
{
str[] = 'd';
} if (S_ISCHR(mode))
{
str[] = 'c';
} if (S_ISBLK(mode))
{
str[] = 'b';
} if ((mode & S_IRUSR))
{
str[] = 'r';
} if ((mode & S_IWUSR))
{
str[] = 'w';
} if ((mode & S_IXUSR))
{
str[] = 'x';
} if ((mode & S_IRGRP))
{
str[] = 'r';
} if ((mode & S_IWGRP))
{
str[] = 'w';
} if ((mode & S_IXGRP))
{
str[] = 'x';
} if ((mode & S_IROTH))
{
str[] = 'r';
} if ((mode & S_IWOTH))
{
str[] = 'w';
} if ((mode & S_IXOTH))
{
str[] = 'x';
}
} char* uid_to_name(uid_t uid)
{
struct passwd* getpwuid(),* pw_ptr;
static char numstr[]; if((pw_ptr = getpwuid(uid)) == NULL)
{
sprintf(numstr,"%d",uid); return numstr;
}
else
{
return pw_ptr->pw_name;
}
} char* gid_to_name(gid_t gid)
{
struct group* getgrgid(),* grp_ptr;
static char numstr[]; if(( grp_ptr = getgrgid(gid)) == NULL)
{
sprintf(numstr,"%d",gid);
return numstr;
}
else
{
return grp_ptr->gr_name;
}
}
void do_ls(char dirname[])
{
DIR* dir_ptr;
struct dirent* direntp; if ((dir_ptr = opendir(dirname)) == NULL)
{
fprintf(stderr, "ls2: cannot open %s \n", dirname);
}
else
{
while ((direntp = readdir(dir_ptr)) != NULL)
{
dostat(direntp->d_name);
} close(dir_ptr);
}
} void dostat(char* filename)
{
struct stat info; if (stat(filename, &info) == -)
{
perror(filename);
}
else
{
show_file_info(filename, &info);
}
} int main(int ac,char* av[])
{
if(ac == )
{
do_ls(".");
}
else
{
while(--ac)
{
printf("%s: \n",++*av);
do_ls(*av);
}
}
}
ls 操作命令 -l/-R和rm -r dir 功能实现的更多相关文章
- linux下rm -r误删NTFS文件恢复方法
一时疏忽,手一抖,把整个挂载的F盘删了一半!顿时傻眼!! 被删的F盘是Windows下NTFS分区,在Ubuntu12.04中挂载了F盘,使用rm命令时粗心大意,误删了一半的数据. 血的教训告诉我们, ...
- 删除GitHub或者GitLab 上的文件夹,git rm -r --ceched 文件夹名 ,提交commit,git push
方法一 这里以删除 .setting 文件夹为案例 git rm -r --cached .setting #--cached不会把本地的.setting删除 git commit -m 'delet ...
- T100——程序从标准签出客制后注意r.c和r.l
标准签出客制后,建议到对应4gl目录,客制目录 r.c afap280_01 r.l afap280_01 ALL 常用Shell操作命令: r.c:编译程序,需在4gl路径之下执行,产生的42m会自 ...
- git rm–r folder fatal:pathspec "" did not match any files
问题描述: 某年某月某日,在查看git库的时候,发现文件的分布和文件夹的名字是极其不合理的,所以移动和重命名了某些文件. 在删除(git rm –r folder)一个空文件夹的时候,出现错误:fat ...
- ViewGroup.layout(int l, int t, int r, int b)四个输入参数的含义
ViewGroup.layout(int l, int t, int r, int b)这个方法是确定View的大小和位置的,然后将其绘制出来,里面的四个参数分别是View的四个点的坐标,他的坐标不是 ...
- git rm -r --cache命令 及 git .gitignore 文件
git 的 .gitignore 文件的作用是在代码提交时自动忽略一个文件.不将其纳入版本控制系统. 比如.一般我们会忽略IDE自动生成的配置文件等. 如果一个你要忽略的文件已经纳入到了git ,也 ...
- git rm -r --cached 去掉已经托管在git上的文件
1.gitignore文件 在git中如果想忽略掉某个文件,不让这个文件提交到版本库中,可以使用修改 .gitignore 文件的方法.这个文件每一行保存了一个匹配的规则例如: # 此为注释 – 将被 ...
- SQL_Server_2008定期自动备份详细图解
SQL_Server_2008定期自动备份详细图解 设置自动数据库的定期备份计划. http://wenku.baidu.com/link?url=Tu ...
- git rm -r --cached解决已提交的文件在.gitignore中加入忽略后无效的问题。
有时候,发现有不该提交的文件已经提交后,仅仅在.gitignore中加入忽略是不行的.这个时候需要执行: git rm -r --cached 文件/文件夹名字 去掉已经托管的文件,然后重新提交: g ...
随机推荐
- 如何查看yum安装路径
#yum install hdf5 #rpm -qa|grep hdf5 hdf5-1.8.7-1.el6.rf.x86_64 #rpm -ql hdf5-1.8.7-1.el6.rf.x86_64
- ECharts概念学习系列之ECharts的下载和安装(图文详解)
不多说,直接上干货! http://echarts.baidu.com/download.html 前言 如果你想要用较少的代码实现比较酷炫的数据统计表,echarts是值得你考虑的一种实现方式.官网 ...
- 一头扎进 Java IO中
Java IO 概述 在这一小节,我会试着给出Java IO(java.io)包下所有类的概述.更具体地说,我会根据类的用途对类进行分组.这个分组将会使你在未来的工作中,进行类的用途判定时,或者是为某 ...
- i.mx6 Android5.1.1 Zygote
0. 总结: 0.1 相关源码目录: framework/base/cmds/app_process/app_main.cppframeworks/base/core/jni/AndroidRunti ...
- REDIS与MEMCACHED的区别 8大点
如果简单地比较Redis与Memcached的区别,大多数都会得到以下观点:1 Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,hash等数据结构的存储.2 Redis支持数据 ...
- wpf 窗口最小化后,触发某事件弹出最小化窗口并置顶
//如果窗口最小化了弹出并置顶----事件触发调用 ShowWindowAsync(new System.Windows.Interop.WindowInteropHelper(CommonHelpe ...
- SQL空和NULL的区别
1.NULL意思为缺失的值(missing value). 2.三值逻辑(three-valued-logic: TRUE,FALSE,UNKNOWN). 在SQL中有三个逻辑谓词:TURE,FALS ...
- C# 批量 json 读取
// 方法一 //string test = "[{ 'CreateUser': 'CN=koujirou nishikawaOMHBK','CreateUserJ': '西川 公二郎'}, ...
- [javaSE] GUI(鼠标事件)
调用Button对象的addMouseListener方法,参数:MouseListener对象,这个类是个接口,需要实现以下方法 mouseClicked mousePressed mouseRel ...
- php 在函数前面加个@的作用
@是错误控制运算符,用屏蔽错误提示比如:@mysql_connect() 不会出现Warning, 而原来mysql_connect 会在页面上访提示Warning.主要是高版本的php不在支持mys ...