加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

列出指定目录下与正则表达式”*.sh”匹配的所有文件

发布时间:2020-12-14 01:44:20 所属栏目:百科 来源:网络整理
导读:#includestdio.h #includestdlib.h #includesys/stat.h #includeunistd.h #includesys/types.h #includedirent.h #includestring.h #define MAX 1024 int dir_run(char *path,char *str) { DIR *dir; struct stat st; struct dirent *entry,*en; char fp[MAX]
#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<sys/types.h>
#include<dirent.h>
#include<string.h>
#define MAX 1024
int dir_run(char *path,char *str)

{

DIR *dir;

struct stat st; struct dirent *entry,*en; char fp[MAX]; dir=opendir(path); if(dir==NULL) { return -1; } while((entry = readdir(dir)) != NULL) { if((strcmp(entry->d_name,".") == 0) || (strcmp(entry->d_name,"..") == 0)) { continue; } sprintf(fp,"%s/%s",path,entry->d_name); stat(fp,&st); if(S_ISREG(st.st_mode)) { if(strstr(entry->d_name,str)) { printf("%s目录下后缀为%s的文件:n",str); printf("%sn",entry->d_name); } } if(S_ISDIR(st.st_mode)) { dir_run(fp,str); } } return 0; } int main(int argc,char * argv[]) { if(argc==3) { dir_run(argv[1],argv[2]); } else printf("输入参数不对,正确格式:./main dirpath .shn"); }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读