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 ...
随机推荐
- 设置获取用户登录信息的Seeion类
/** * * 保存用户上下文信息 * 还可以获取session * */ public class UserContext { public static final String USER_IN_ ...
- Android多媒体技术之视频播放
1.Android中视频播放方式 surfaceView+MediaPlayer,通过MediaPlayer来控制视频的播放.暂停.进度等: 使用VideoView 来播放,这个类其实也是继承了Sur ...
- Reference Type Casting
5.5.1. Reference Type Casting Given a compile-time reference type S (source) and a compile-time refe ...
- [C语言]日期间天数差值的计算
刷一些算法题时总能遇到计算日期间天数的问题,每每遇到这种情况,不是打开excel就是用系统自带的计算器.私以为这种问题及其简单以至于不需要自己动脑子,只要会调用工具就好.直到近些天在写一个日历程序的时 ...
- springboot-3-aop
aop存在的目的是进一步解耦, spring支持aspectJ的注解式切面编程 1), 使用@Aspect声明为一个切面, 并使用@Component加入context中 2), 使用@After, ...
- http请求数据封装
package com.wdm.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- Django 用模板来节省公共代码,
如果我们有一个后台网站,或者显示个人信息的网站 标题logo,边框等信息的代码都是固定的,只有部分区域是改变, 我们如果用Django来写html的话就会重复代码.这时候我们只要用模板的方 ...
- [译]用R语言做挖掘数据《七》
时间序列与数据挖掘 一.实验说明 1. 环境登录 无需密码自动登录,系统用户名shiyanlou,密码shiyanlou 2. 环境介绍 本实验环境采用带桌面的Ubuntu Linux环境,实验中会用 ...
- Angular2-三种样式封装策略的区别
Angular2有三种样式封装方式,分别是None.Native.Emulated. 可用元数据“encapsulation”配置,配置方式如下: encapsulation: ViewEncapsu ...
- [javaSE] 进制转换(二进制十进制十六进制八进制)
十进制转二进制,除2运算 十进制6转二进制是 110 (注意从右往左写,使用算式从下往上写) 二进制转十进制,乘2过程 二进制110转十进制 0*2的0次方+1*2的1次方+1*2的2次方=6 对 ...