-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch_glob.c
53 lines (42 loc) · 1.45 KB
/
search_glob.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glob.h>
#include <regex.h>
#include <stdbool.h>
#include "search_glob.h"
int search_glob_results(glob_t glob_result, int scan_to_check) {
int isREFHOT = 0;
regex_t regex_digit, regex_string;
int hasDigit, hasString;
char *pattern_digit = malloc(6*sizeof(char));
sprintf(pattern_digit, "%d", scan_to_check); //scanID
char *pattern_string = "REF"; //scan_type
// Compile regular expressions
hasDigit = regcomp(®ex_digit, pattern_digit, REG_EXTENDED);
hasString = regcomp(®ex_string, pattern_string, REG_EXTENDED);
// Iterate over the glob_result.gl_pathv[] array
for (int i = 0; i<glob_result.gl_pathc; i++) {
const char *filename = glob_result.gl_pathv[i];
// Check if filename contains 5-digit integer scanID
hasDigit = regexec(®ex_digit, filename, 0, NULL, 0);
// Check if filename contains 3-character string scan_type
hasString = regexec(®ex_string, filename, 0, NULL, 0);
// If both patterns match, AND the file is a REF, it's a REF HOT!
if (!hasDigit && !hasString) {
isREFHOT = 1;
}
}
// Free compiled regular expressions
regfree(®ex_digit);
regfree(®ex_string);
return isREFHOT;
}
bool file_exists(const char *filename) {
FILE *file = fopen(filename, "r");
if (file) {
fclose(file);
return true;
}
return false;
}