Implement a myfind command following the find command in UNIX operating system. The myfind command starts from the specified directory and recursively looks up the specified file. The command format is as follows:

myfind PATH -option parameters

PATH:The directory for looking up.

-option parameters:

-name “file”: Specify the name of the file to be found, which can have wildcard characters? *, etc

-mtime n: Search by time, search for files modified n days before today

-ctime n:Search by time for files created n days before today.

Finally, output the search results to standard output.

#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <dirent.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h> int opterr = 0;
char path[1024] = "";
char targetname[100] = "";
int modifiedtime = -1;
int changetime = -1; char match(char *first, char *second)
{
if (*first == '\0' && *second == '\0')
return 1;
if (*first == '*' && *(first+1) != '\0' && *second == '\0')
return 0;
if (*first == '?' || *first == *second)
return match(first+1, second+1);
if (*first == '*')
return match(first+1, second) || match(first, second+1);
return 0;
} char isFileOk(char *name, time_t mt, time_t ct){
time_t now = time(0);
int spd = 24*3600;
int mtd = (now - mt)/spd;
int ctd = (now - ct)/spd; //printf("filename: %s target: %s\n", name, targetname);
if(match(targetname, name) == 1){
if(modifiedtime != -1 && mtd > modifiedtime) return 0;
if(changetime != -1 && ctd > changetime) return 0;
return 1;
}
return 0;
} void findInDir(char *path) {
DIR *d;
struct dirent *dir;
d = opendir(path);
if (d) {
while ((dir = readdir(d)) != NULL) {
char newpath[1024];
snprintf(newpath, sizeof(newpath), "%s/%s", path, dir->d_name);
if (dir->d_type == DT_DIR) {
if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
findInDir(newpath);
} else {
struct stat buf;
if(stat(newpath, &buf)==0){
if(isFileOk(dir->d_name, buf.st_mtime, buf.st_ctime)){
printf("%s/%s\n", path, dir->d_name);
}
}
}
}
closedir(d);
}
} int main(int argc, char *argv[]){
char *optstr = "p:n:m:c:";
struct option opts[] = {
{"path", 1, NULL, 'p'},
{"name", 1, NULL, 'n'},
{"mtime", 1, NULL, 'm'},
{"ctime", 1, NULL, 'c'},
{0, 0, 0, 0},
};
int opt;
while((opt = getopt_long(argc, argv, optstr, opts, NULL)) != -1){
switch(opt) {
case 'p':
strcpy(path, optarg);
break;
case 'n':
strcpy(targetname, optarg);
break;
case 'm':
modifiedtime = atoi(optarg);
break;
case 'c':
changetime = atoi(optarg);
break;
case '?':
if(strchr(optstr, optopt) == NULL){
fprintf(stderr, "unknown option '-%c'\n", optopt);
}else{
fprintf(stderr, "option requires an argument '-%c'\n", optopt);
}
return 1;
}
}
findInDir(path);
return 0;
}

最新文章

  1. 烂泥:VMWare Workation双网卡配置IP地址
  2. Spring集成MyBatis完整示例
  3. ThreadLocal之我见
  4. Monkey测试4——Monkey命令行可用的全部选项
  5. 微信公众平台回复链接可以直接访问,但不能是锚文字链接&lt;a&gt;标签
  6. Dynamic CRM 2013学习笔记(三十二)自定义审批流3 - 节点及实体配置
  7. 关于iReport报表的分页
  8. Naming Conventions for .NET / C# Projects
  9. magento提速的一些小技巧,列举manegnto网站提速的
  10. NFC framework
  11. android和javascript之间相互通信实例分析
  12. Oracle Database Transaction Isolation Levels 事务隔离级别
  13. 升级项目到.NET Core 2.0,在Linux上安装Docker,并成功部署
  14. Gradient Descent
  15. Useful command for Docker
  16. Android布局优化:include 、merge、ViewStub的详细总结
  17. P3954 成绩(noip2017普及组)
  18. vue数据修改 但未渲染页面
  19. springMVC学习 十二 拦截器
  20. [每天解决一问题系列 - 0009] File System Redirector

热门文章

  1. 深入理解Java虚拟机(八)——类加载机制
  2. 职场中究竟什么是ownership,你是一个有ownership的人吗?
  3. 使用Swiper快速实现3D效果轮播
  4. JavaScript判断视频编码是否为h.264
  5. 制作3D小汽车游戏(下)
  6. MySQL如何计算统计redo log大小
  7. ML.net重新训练模型需要注意的事项。
  8. Android插件换肤 一.实现原理
  9. SpringBoot进阶教程(六十八)Sentinel实现限流降级
  10. 理解Tomcat工作原理