linux下查找文件程序-findlinux下查找文件程序-find

简单的实现linux下find功能的查打文件程序:
程序名:findfile.c
部分函数原型:
struct dirent {                 //头文件中定义的结构体,man readdir有说明
    ino_t d_ino;                // inode number
    off_t d_off;                // offset to the next dirent
    unsigned short d_reclen;    // length of this record
    unsigned char d_type;       // type of file
    char d_name[256];           // filename
};
opendir原型:DIR *opendir(const char *name);
       返回值:The opendir() function returns a pointer to the directory stream.  On error, NULL  is  returned,               and errno is set appropriately.
readdir原型:struct dirent *readdir(DIR *dir);
       返回值:The readdir() function returns a pointer to a dirent structure, or NULL if an error occurs or                  end-of-file is reached.  On error, errno is set appropriately.
源文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <syslog.h>
#include <sys/stat.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
void findfile(char *p, char *q);
void writelog(char *log_dir, char *log_file);
int flag = 0;
int main(int argc, char **argv) //运行:./a.out /my_c_script/ wubi.c
{
    assert((argv[1] != NULL) && (argv[2] != NULL));
    findfile(argv[2], argv[1]);
    if (flag == 0) {
        printf("The file '%s' is not exist in '%s'/n", argv[2], argv[1]);
    }
    writelog(argv[1], argv[2]);
    return 0;
}

void findfile(char *p, char *q)
{
    int j = 1, dir_len = 0;
    DIR *dir;
    struct dirent *ret;
    char *current_dir = NULL;
    if ((current_dir = (char *) malloc(1024)) == NULL) {
        printf("malloc fail!/n");
        exit(1);
    }
    memset(current_dir, 0, 1024);
    dir = opendir(q);           //返回一个DIR类型指针给readdir函数做参数
    if (!dir) {
        printf("%s/n", q);
        printf("%s/n", strerror(errno));
        return;
    }
    while ((ret = readdir(dir)) != NULL) {
        //返回值成功则返回下个目录进入点。有错误发生或读取到目录文件尾则返回NULL
        if (ret->d_type == 8) { //类型为8为普通文件
            if (!strcmp(ret->d_name, p)) {
                flag = 1;
                printf("%s/%s/n", q, ret->d_name);
            }
        }
        if (ret->d_type == 4 && (strcmp(ret->d_name, ".") != 0) && (strcmp(ret->d_name, "..") != 0)) {  //类型为4为目录,strcmp比较是把当前目录>和上一级目录给去掉。
            strcpy(current_dir, q);
            dir_len = strlen(current_dir);
            if (*(current_dir + dir_len - 1) != '/') {
                strcat(current_dir, "/");
            }
            strcat(current_dir, ret->d_name);
            chdir(current_dir);
            findfile(p, current_dir);
            chdir("..");
            j++;
        }
    }

    if(current_dir!=NULL){

        free(current_dir);

        current_dir = NULL;

    }
    closedir(dir);
}

void writelog(char *log_dir, char *log_file)
{
    time_t now;                 //系统时间函数设置
    int y = 0;
    struct stat x;              //定义结构体变量
    stat("/var/log/syslog", &x);        //调用函数
    y = x.st_size;              //求出syslog的大小
    time(&now);                 //系统时间函数设置
    syslog(LOG_USER | LOG_INFO, "当前时间 %s 已在%s目录下查找过%s文件!/n", ctime(&now), log_dir, log_file);     //向日志文件写文字
}
编译:gcc findfile.c -Wall
运行格式:. /a.out +  查找的路径 + 要查找的文件名
运行:        . /a.out  /media/sda7/my_c_script/  happy.c
运行结果:
终端:/media/sda7/my_c_script/socket/happy.c
/var/log/syslog文件显示:
Jan 10 15:16:32 Bear a.out: 当前时间 Wed Jan 10 15:16:32 2007  已在/media/sda7/my_c_script/目录下查找过happy.c文件!

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值