Linux find 命令的初步实现(C++)
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++)的更多相关文章
- linux全部命令
linux全部命令 一.安装和登陆命令1.进入图形界面startx 2.进入图形界面init 5 3.进入字符界面init 3 4.登陆login 5.关机poweroff-p 关闭机器的时候关闭电源 ...
- linux常用命令加实例大全
目 录引言 1一.安装和登录 2(一) login 2(二) shutdown 2(三) halt 3(四) reboot 3(五) ...
- linux常用命令与实例小全
转至:https://www.cnblogs.com/xieguohui/p/8296864.html linux常用命令与实例小全 阅读目录(Content) 引言 一.安装和登录 (一) ...
- linux grep命令
linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressio ...
- Linux常用命令(一)
Linux常用命令 1. pwd查看当前路径(Print Working Directory) [root@CentOS ~]# pwd/root 2. cd .. 返回上一级 .. 表示上一级 ...
- Linux下命令行安装weblogic10.3.6
Linux下命令行安装weblogic10.3.6 一.安装前准备工作: 1.创建用户useradd weblogic;创建用户成功linux系统会自动创建一个和用户名相同的分组,并将该用户分到改组中 ...
- Linux paste命令
Linux paste命令用于合并文件的列. paste指令会把每个文件以列对列的方式,一列列地加以合并. 语法 paste [-s][-d <间隔字符>][--help][--versi ...
- 20145222《信息安全系统设计基础》Linux常用命令汇总
学习Linux时常用命令汇总 通过Ctrl+f键可在该网页搜索到你想要的命令. Linux中命令格式为:command [options] [arguments] //中括号代表是可选的,即有些命令不 ...
- Linux sudo 命令的应用
.note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...
随机推荐
- tornado 网页提交内容 展示内容作业
s2.py import tornado.ioloop import tornado.web text_list=[] class MainHandler(tornado.web.RequestHan ...
- react项目中对dom元素样式修改的另一种方法以及将组件插入到node节点中
在项目中,以前如果遇到对dom元素的操作都是直接获取dom元素,比如说: 但是如果修改的样式比较多的话,不如直接"切换"dom元素,如下例子: 这样会节省一些性能.因为操作dom的 ...
- 从零开始了解多线程 之 深入浅出AQS -- 上
java锁&AQS深入浅出学习--上 上一篇文章中我们一起学习了jvm缓存一致性.多线程间的原子性.有序性.指令重排的相关内容, 这一篇文章便开始和大家一起学习学习AQS(AbstractQu ...
- DPSK通信系统的FPGA实现
之前接触过一些FPGA的相关知识,借着实现一个简单的DPSK系统,顺便复习和记录一下Verilog HDL的简单使用方法.准备直接用一张图展现DPSK的调制解调原理,再按照模块介绍Verilog的实现 ...
- Linux命令-2.文件属性
在Linux系统中文件的属性有:文件类型.属主属性.属组属性.其他属性,使用10个字符表示,如下图: 其中第0位表示文件类型: 当为[ d ]则是目录 当为[ - ]则是文件: 若是[ l ]则表示为 ...
- node目录
1 [了解nodejs] 1.1 [node简介] 1.2 [node的特点] 1.3 [如何安装] 1.4 [如何运行] 2 [es6--基本语法] 2.1 [变量的解构赋值] 2.2 [解构赋值] ...
- js下 Day01、DOM对象,BOM浏览器对象模型
一.初识DOM 1.什么是DOM?为什么学习DOM 2.DOM是实现js在网页实现交互的关键环节,我们的js代码就是通过DOM的方法来实现对于html内容的操作. 3.认识DOM实现了js和网页结合的 ...
- [日常摸鱼]bzoj2463 [中山市选2009]谁能赢呢?
题意:两个人在$n*n$的棋盘上玩游戏,一开始棋子在左上角,可以上下左右的走到没有走过的地方,最后不能走的那个输,两个人都采取最优决策问哪个赢 猜结论!$n$为偶数则先手赢,否则后手赢. 考虑用$1* ...
- php代码审计整理
目录 变量覆盖 1x01.extract 变量覆盖 定义和用法 语法 漏洞产生:使用了默认设置 攻击方法:制造变量名冲突,对于需要相等的值可以同时置空 修复:设定一个冲突时的处理规则 例题: 1x02 ...
- Maven之继承
这里我还是将通过一个例子来了解一下Maven继承的初步使用配置.还是使用三个工程项目Project-Parent.Project-C和Project-D来进行说明,三个项目关系如下: <?xml ...