-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry.c
More file actions
106 lines (80 loc) · 2.34 KB
/
try.c
File metadata and controls
106 lines (80 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define BUFFSIZE 128u
Boolean_e
is_string_matched_regex (
const char *filename,
const char *pattern
)
{
Boolean_e is_matched = FALSE;
uint32 matched_no = 0;
uint32 ret = 0;
uint8 err_buf[BUFFSIZE] = {0};
regex_t reg;
regmatch_t pm[10];
const size_t nmatch = 10;
RETURN_VAL_IF_FAIL(filename == NULL, FALSE);
RETURN_VAL_IF_FAIL(pattern != NULL, FALSE);
#ifdef DEBUG
printf("%s is processing...\n",__FUNCTION__);
#endif
ret = regcomp(®, pattern, REG_EXTENDED|REG_ICASE|REG_NEWLINE);
if (ret != 0)
{
regerror(ret, ®, err_buf, sizeof(err_buf));
fprintf(stderr, "%s: filename '%s' \n", err_buf,filename);
is_matched = FALSE;
return(is_matched);
}
ret = regexec(®, filename, nmatch, pm, 0);
if (ret == REG_NOMATCH )
{
is_matched = FALSE;
return(is_matched);
}
else
if (ret != 0)
{
regerror(ret, ®, err_buf, sizeof(err_buf));
fprintf(stderr, "%s: regcom('%s')\n", err_buf, filename);
is_matched = FALSE;
return(is_matched);
}
for (matched_no = 0; matched_no < nmatch && pm[matched_no].rm_so != -1; ++ matched_no)
{
if( pm[matched_no].rm_so == 0
&& pm[matched_no].rm_eo == strlen(filename))
{
is_matched = TRUE;
break;
}
}
regfree(®);
return(is_matched);
}
//实现单个文件名与正则表达式的匹配之后,只需要遍历当前目录下的所有文件名,
//重复匹配。测试程序如下:
int main(int argc, char *argv[])
{
Boolean_e is_matched = FALSE;
uint16 file_no = 0;
DIR *dp = NULL;
struct dirent *dirp = NULL;
if((dp = opendir("."))== NULL)
{
printf("can't open the current directoty!\n");
exit(0);
}
while((dirp = readdir(dp))!=NULL)
{
file_no++;
is_matched = is_string_matched_regex(dirp->d_name, argv[1]);
#ifdef DEBUG
if( is_matched == TRUE )
{
printf("file_no:%d---filename:%s\n",file_no, dirp->d_name);
}
#endif
}
closedir(dp);
return 0;
}