博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下扫描文件并按时间排序
阅读量:4162 次
发布时间:2019-05-26

本文共 4451 字,大约阅读时间需要 14 分钟。

check_dir.h

#ifndef _CHECK_DIR_#define _CHECK_DIR_#include 
#ifdef __cplusplusextern "C" {#endif#define CHECK_DIR_SIZE (30000)#define FN_SIZE 1024typedef 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 
//#include
#include
#include
#include
#include
#include
#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); }}

转载地址:http://fcixi.baihongyu.com/

你可能感兴趣的文章
[转]C语言printf
查看>>
C 语言 学习---回调、时间定时更新程序
查看>>
第十一章 - 直接内存
查看>>
Single Number II --出现一次的数(重)
查看>>
对话周鸿袆:从程序员创业谈起
查看>>
Mysql中下划线问题
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
vue项目使用安装sass
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>
laravel 修改api返回默认的异常处理
查看>>
laravel事务
查看>>
【JavaScript 教程】浏览器—History 对象
查看>>
这才是学习Vite2的正确姿势!
查看>>
7 个适用于所有前端开发人员的很棒API,你需要了解一下
查看>>