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;
}

Linux find 命令的初步实现(C++)的更多相关文章

  1. linux全部命令

    linux全部命令 一.安装和登陆命令1.进入图形界面startx 2.进入图形界面init 5 3.进入字符界面init 3 4.登陆login 5.关机poweroff-p 关闭机器的时候关闭电源 ...

  2. linux常用命令加实例大全

    目  录引言    1一.安装和登录    2(一)    login    2(二)    shutdown    2(三)    halt    3(四)    reboot    3(五)    ...

  3. linux常用命令与实例小全

    转至:https://www.cnblogs.com/xieguohui/p/8296864.html  linux常用命令与实例小全 阅读目录(Content) 引言 一.安装和登录 (一)    ...

  4. linux grep命令

    linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressio ...

  5. Linux常用命令(一)

    Linux常用命令 1. pwd查看当前路径(Print Working Directory)    [root@CentOS ~]# pwd/root 2. cd .. 返回上一级 .. 表示上一级 ...

  6. Linux下命令行安装weblogic10.3.6

    Linux下命令行安装weblogic10.3.6 一.安装前准备工作: 1.创建用户useradd weblogic;创建用户成功linux系统会自动创建一个和用户名相同的分组,并将该用户分到改组中 ...

  7. Linux paste命令

    Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法 paste [-s][-d <间隔字符>][--help][--versi ...

  8. 20145222《信息安全系统设计基础》Linux常用命令汇总

    学习Linux时常用命令汇总 通过Ctrl+f键可在该网页搜索到你想要的命令. Linux中命令格式为:command [options] [arguments] //中括号代表是可选的,即有些命令不 ...

  9. Linux sudo 命令的应用

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

随机推荐

  1. 题解-TJOI2015 弦论

    TJOI2015 弦论 字符串 \(s\) 和 \(t\) 和 \(k\).如果 \(t=0\),不同位置的相同子串算 \(1\) 个:如果 \(t=1\),不同位置的相同子串算多个.求 \(k\) ...

  2. Angular:惰性加载的模块

    ①通过ng new angular-module创建一个全新的angular应用,默认不选路由 ②通过一下命令分别创建2个模块和1个组件 ng g m hx1 ng g c hx1 ng g m hx ...

  3. uniapp导入导出Excel

    众所周知,uniapp作为跨端利器,有诸多限制,其中之一便是vue页面不支持传统网页的dom.bom.blob等对象. 所以,百度上那些所谓的导入导出excel的方法,大部分都用不了,比如xlsx那个 ...

  4. 什么叫做ECS云服务器?

    什么是ECS云服务器,阿里ECS云服务器与虚拟主机的区别是什么.以前,ECS云服务器还不带"云"字,只是叫做"服务器",毫无疑问"ECS云服务器&qu ...

  5. mysql全备、增量备份脚本

     1.mysql全量备份及定时删除备份文件脚本 #!/bin/bash v_user="root" v_password="mysql" backup_date ...

  6. 【面试专栏】JAVA CAS(Conmpare And Swap)原理

    1. CAS简介   在计算机科学中,比较和交换(Conmpare And Swap)是用于实现多线程同步的原子指令.它将内存位置的内容与给定值进行比较,只有在相同的情况下,将该内存位置的内容修改为新 ...

  7. 树莓派了解Linux基本命令

    本节我们来了解一些基本的Linux命令(在树莓派上操作),看完之后,当你再面对Linux黑黑的命令框时至少不会不知所措,你可以用这些基本的命令完成一些需要的操作,比如查找.编辑.查看文件,查看基本的系 ...

  8. MySQL-实体映射关系和连接查询

     一 数据库实体的映射关系 1. 一对一:学生表和学生档案表,一个学生只有一个对应的档案. 2. 一对多/多对一:一个球队友多个球员(一对多),多个球员属于一个球队(多对一) 3. 多对多:一个学生可 ...

  9. 找和为K的两个元素

    总时间限制:1000ms 内存限制: 65536kB 描述 在一个长度为n(n < 1000)的整数序列中,判断是否存在某两个元素之和为k. 输入 第一行输入序列的长度n和k,用空格分开. 第二 ...

  10. python2与python3共存后,如何使用

    借用py的一个参数 py -2 与py -3调用不同是的python版本 所以运行的时候只要 py -2 文件名可以用python2来运行脚本 py -3 文件名就是用python3 来运行脚本 参考 ...