zoukankan      html  css  js  c++  java
  • 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 == '' && *second == '') 
            return 1; 
        if (*first == '*' && *(first+1) != '' && *second == '') 
            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
    ", 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
    ", 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'
    ", optopt);
                    }else{
                        fprintf(stderr, "option requires an argument '-%c'
    ", optopt);
                    }
                    return 1;
            }
        }
        findInDir(path);
        return 0;
    }
    
  • 相关阅读:
    闭包
    作用域
    既然踏足前端,便要立志成为专家
    D3引擎用正则运算的方式,实现智能设备APP消息推送
    基于ArduinoUNOR3的智能调速风扇
    【一起来玩RTOS系列】之RT-Thread Nano快速创建工程
    MCU代码自动生成工具,全面升级
    ESP8266 SOC门磁系统(一)---短信报警功能
    正点原子F407/103,接入机智云,点亮LED
    机智云5.0推出IoT套件GoKit4.0 可实现物联网应用协同开发
  • 原文地址:https://www.cnblogs.com/justsong/p/12219780.html
Copyright © 2011-2022 走看看