linux下扫描文件并按时间排序

check_dir.h

#ifndef _CHECK_DIR_
#define _CHECK_DIR_
#include <time.h>

#ifdef	__cplusplus
extern "C" {
#endif
#define  CHECK_DIR_SIZE (30000)
#define  FN_SIZE 1024
typedef struct my_struct
{
    char f_name[FN_SIZE];
    time_t ctime;
}f_struct,*p_f_struct;

//初始化文件缓冲队列
int neo_init_check_size();

//释放初始化的内存
int neo_close_check_size();

//获取缓冲队列首地址
p_f_struct neo_get_p_head();

//获取缓冲队列尾地址
p_f_struct neo_get_p_end();

//获取下一文件名地址
p_f_struct neo_get_p_next(p_f_struct p);

//显示缓冲区内容
void neo_print_f_name();

//将目录下所有文件名放入队列
int neo_check_dir(char *dir); 

//更具key将数组分为两部分
//p_f_struct partion(p_f_struct pstHead,p_f_struct pstLow,p_f_struct pstHigh);
p_f_struct partion(p_f_struct pstHead, p_f_struct pstEnd);

//对扫描到的文件按最后一次修改时间进行排序
//int quick_sort(p_f_struct pstHead, p_f_struct pstLoiw, p_f_struct pstHigh);
void quick_sort(p_f_struct pstHead, p_f_struct pstEnd);

#ifdef	__cplusplus
}
#endif
#endif

check_dir.c

#include <unistd.h>
//#include <openssl/md5.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include "check_dir.h"

static p_f_struct neo_p_head = NULL;
static p_f_struct neo_p_end = NULL;

//初始化文件缓冲队列
int neo_init_check_size()
{
    if(neo_p_head != NULL)
        return -1;
    neo_p_head = (p_f_struct)malloc(sizeof(f_struct) * CHECK_DIR_SIZE);
    if(neo_p_head == NULL)
        return -1;
    memset(neo_p_head,0,sizeof(f_struct) * CHECK_DIR_SIZE);
    neo_p_end = neo_p_head;
    return 0;
}

//释放初始化的内存
int neo_close_check_size()
{
    //p_f_struct p = neo_p_head;
    if(neo_p_head != NULL)
    {
        free(neo_p_head);
        neo_p_head = NULL;
        neo_p_end = NULL;
        return 0;
    }
    return -1;
}

//获取缓冲队列首地址
p_f_struct neo_get_p_head()
{
    if(neo_p_head->f_name[0] == 0)
        return NULL;
    return neo_p_head;
}

//获取缓冲队列尾地址
p_f_struct neo_get_p_end()
{
    if(neo_p_head->f_name[0] == 0)
        return NULL;
    neo_p_end = neo_p_head;
    while((neo_p_end + 1)->f_name[0] != 0)
        neo_p_end ++;
    return neo_p_end;
}

//获取下一文件名地址
p_f_struct neo_get_p_next(p_f_struct p)
{
    p++;
    if(p->f_name[0] == 0)
        return NULL;
    return p;
}

//显示缓冲区内容
void neo_print_f_name()
{
    int i = 0;
    p_f_struct p = neo_p_head;
    while(p->f_name[0] != 0)
    {
        printf("ctime : [%ld]      file name : [%s]\n",p->ctime,p->f_name);
        p = p ++;
        i ++;
    }
    printf("sum = %d\n",i);
}

//将目录下所有文件名放入队列
int neo_check_dir(char *dir) 
{
    DIR * dp;
    struct dirent *dent;
    struct stat st;
    char fn[1024];
    //char fn1[1024];
    if(neo_p_head == NULL)
    {
        printf("neo_p_head is NULL,need neo_init_check_size()!");
        return;
    }
    p_f_struct p = neo_p_head;
    //p_f_struct q = neo_p_head;

    dp = opendir(dir);
    if (!dp) {
        printf("无法打开文件夹[%s]\n", dir);

        return;
    }
    while ((dent = readdir(dp)) != NULL) {
        if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) {
            continue;
        }
        //MFLIST *mf;
        sprintf(fn, "%s/%s", dir, dent->d_name);
        if (stat(fn, &st) == 0) {
            if (S_ISDIR(st.st_mode)) {
                neo_check_dir(fn);

            } else if (S_ISREG(st.st_mode)) {
                snprintf(p->f_name,FN_SIZE,"%s",fn);
                p->ctime = st.st_ctime;
                //neo_p_end = p;
                p = p ++;
                if(p - neo_p_head >= (CHECK_DIR_SIZE - 1))
                    return -1;
                /*p = (p_f_struct)malloc(sizeof(f_struct));
                  sprintf(p->f_name,"%s",fn);
                  p->ctime = st.st_ctime;
                  if(neo_p_head == NULL)
                  {
                  neo_p_head = p;
                  neo_p_head->next = NULL;
                  neo_p_head->front = neo_p_head;
                  continue;
                  }
                  if(p->ctime <= neo_p_head->ctime)
                  {
                  p->next = neo_p_head;
                  neo_p_head = p;
                  neo_p_head->front = neo_p_head;
                  continue;
                  }
                  q = neo_p_head->next;
                  while(q != NULL)
                  {
                  if(p->ctime <= q->ctime)
                  {
                  q->front->next = p;
                  p->next = q;
                  break;
                  }
                  q = q->next;
                  }
                  if(q == NULL)
                  {
                  q = p;
                  q->next = NULL;
                  }*/



            } 
        } else {
            printf("can't stat %s\n", fn);
        }
    }
    closedir(dp);
    return 0;
}

//更具key将数组分为两部分
p_f_struct partion(p_f_struct pstHead, p_f_struct pstEnd)
{
    f_struct temp_struct;
    memcpy(&temp_struct, pstHead, sizeof(f_struct));
    while(pstHead != pstEnd)
    {
        while((pstHead < pstEnd) && (pstEnd->ctime >= temp_struct.ctime))
            pstEnd --;
        if(pstHead < pstEnd){
    //        printf("%s,%ld\n",pstEnd->f_name,pstEnd->ctime);
            memcpy(pstHead, pstEnd, sizeof(f_struct));
            pstHead ++;
        }
        while((pstHead < pstEnd) && (pstHead->ctime <= temp_struct.ctime))
            pstHead ++;
        if(pstHead < pstEnd){
  //          printf("%s,%ld\n",pstHead->f_name,pstHead->ctime);
            memcpy(pstEnd, pstHead, sizeof(f_struct));
            pstEnd --;
        }
    }
//            printf("%s,%ld\n",temp_struct.f_name,temp_struct.ctime);
    memcpy(pstHead, &temp_struct, sizeof(f_struct));
    return pstHead;
}
//对扫描到的文件按最后一次修改时间进行排序
void quick_sort(p_f_struct pstHead, p_f_struct pstEnd)
{
    if(pstHead < pstEnd)
    {
        p_f_struct temp_Head = pstHead;
        p_f_struct temp_End = pstEnd;
        p_f_struct pstTemp = partion(temp_Head, temp_End);
        quick_sort(pstHead, pstTemp - 1);
        quick_sort(pstTemp + 1, pstEnd);
    }
}




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高晓伟_Steven

相逢即是有缘,动力源于金钱。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值