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. 【java】JSON.toJSONString 空对象也可以转化为JSON字符串

    <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifac ...

  2. tcp/ip原理/三次握手/四次挥手

    @ tcp/ip原理 1.1 tcp/ip三次握手 1.1.1 建立过程说明 a)   由主机A发送建立TCP连接的请求报文, 其中报文中包含seq序列号, 是由发送端随机生成的, 并且还将报文中SY ...

  3. 使用MySQL乐观锁解决超卖问题

    在秒杀系统设计中,超卖是一个经典.常见的问题,任何商品都会有数量上限,如何避免成功下订单买到商品的人数不超过商品数量的上限,这是每个抢购活动都要面临的难点. 1 超卖问题描述 在多个用户同时发起对同一 ...

  4. Linux安装Mycat1.6.7.4并实现Mysql数据库读写分离简单配置

    1. Mycat简介 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群 一 ...

  5. 【JAVA基础】static的定义

    public class STATIC { public static void main(String[] args) { // 创建两个不同的类 只要赋值一个 另外一个定义好的static属性 会 ...

  6. C# 海量数据瞬间插入到数据库的方法

    C# 海量数据瞬间插入到数据库的方法 当我们在数据库中进行大量的数据追加时,是不是经常因为数据量过大而苦恼呢?而所谓的海量数据,一般也是上万级的数据,比如我们要添加一百万条数据,应该如何提高它的效率呢 ...

  7. Jupyter Notebook在多个虚拟环境配置与使用

    1 问题描述 使用Anaconda配置了包括Pytorch.Tensorflow等多个虚拟环境后,依然无法使用Jupyter Notebook选择不同的虚拟环境运行代码,问题如下图所示. 2 解决方法 ...

  8. Nessus<强大的漏扫工具>

    刷漏洞,抓鸡必备,,,,,,, 参考文章: https://blog.csdn.net/wwl012345/article/details/96998187 肝,,,,太全了,,, ps : 我不是脚 ...

  9. Unity射击游戏实例—物理碰撞的实现

    前言: 这一篇章实现物理碰撞,就是游戏体碰撞减装甲,这几天想要试着做出兼具装甲与血量的模式,可自动回复的装甲与永久损伤的血量,在一些平台上找到了不少有意思的模型,有兴趣的可以自己找找模型替换一下. 射 ...

  10. Python操作PDF-文本和图片提取(使用PyPDF2和PyMuPDF)

    PDF文件格式 如今,可移植文档格式(PDF)属于最常用的数据格式.在1990年,PDF文档的结构由Adobe定义.PDF格式的思想是,对于通信过程中涉及的双方(创建者,作者或发送者以及接收者)而言, ...