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 ...
随机推荐
- 配置tomcat-users
<role rolename="admin-gui"/><role rolename="admin-script"/><role ...
- flot中文详解
调用plot函数的方法如下: var plot = $.plot(placeholder, data, options) Data的结构: data应该是data series的一个数组: [ ser ...
- 文本相似度分析(基于jieba和gensim)
基础概念 本文在进行文本相似度分析过程分为以下几个部分进行, 文本分词 语料库制作 算法训练 结果预测 分析过程主要用两个包来实现jieba,gensim jieba:主要实现分词过程 gensim: ...
- 【LeetCode题解】349_两个数组的交集
目录 [LeetCode题解]349_两个数组的交集 描述 方法一:两个哈希表 Java 实现 类似的 Java 实现 Python 实现 类似的 Python 实现 方法二:双指针 Java 实现 ...
- 【LeetCode题解】144_二叉树的前序遍历
目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...
- iOS开源项目周报0209
由OpenDigg 出品的iOS开源项目周报第七期来啦.我们的iOS开源周报集合了OpenDigg一周来新收录的优质的iOS开源项目,方便iOS开发人员便捷的找到自己需要的项目工具等.Hedwig 向 ...
- angularjs 过滤掉textarea输入内容中夹带的特殊字符
<body ng-app="app"> <div ng-controller="main"> <textarea ng-model ...
- 将字符串 “ hello word,你 好 世 界 ! ” 两端空格去掉并且将其中的其他所有空格替换成一个空格 输出结果为“hello word,你 好 世界”
string str = " hello word,你 好 世 界 ! "; string msg = str.Trim(); //去掉首尾空格 //使用split分割字符串,st ...
- C# 转换Json类
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.R ...
- Rest架构下的增删改查
首先还是要连接一下什么是Rest, REST是英文representational state transfer(表象性状态转变)或者表述性状态转移;Rest是web服务的一种架构风格;使用HTTP, ...