1. 程式人生 > >linux c 遍歷目錄及文件

linux c 遍歷目錄及文件

cup close logs sed mode 文件 void struct readdir

#include <dirent.h>
void
recovery_backend() { DIR * pdir ; struct dirent * pdirent; struct stat f_ftime; char full_path[PATH_MAX] = {0}; char buf[PATH_MAX] = {0}; char cmd[256] = {0}; pdir = opendir(cups_backend); if(pdir == NULL) return ; for(pdirent = readdir(pdir);pdirent != NULL;pdirent = readdir(pdir)) {
if(strcmp(pdirent->d_name,".")==0||strcmp(pdirent->d_name,"..")==0) continue; memset(full_path,0,sizeof(full_path)); snprintf(full_path,sizeof(full_path),"%s%s",cups_backend,pdirent->d_name); if(stat(full_path,&f_ftime) != 0) if(S_ISDIR(f_ftime.st_mode)) continue
; /*子目錄跳過*/ if(f_ftime.st_mode & S_IFDIR) continue; memset(buf,0,sizeof(buf)); readlink(full_path,buf,sizeof(buf)); if(strcmp(full_hook_backend,buf) == 0) { remove(full_path); memset(cmd,0,sizeof(cmd)); snprintf(cmd,sizeof(cmd),"
cp -P %s%s %s",bk_backend,pdirent->d_name,cups_backend); system(cmd); } } closedir(pdir); }

linux c 遍歷目錄及文件