-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwild_characters.cpp
35 lines (35 loc) · 1.22 KB
/
wild_characters.cpp
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
#include "wild_proto.hpp"
#include <glob.h>
#include <string.h>
vector<char *> search_for_wild(vector<char *> cstrings)
{
glob_t glob_result;
int flag;
for (int i = 0; i < cstrings.size(); i++) // if a token contains any of the following characters then glob is called returning all the matching names it finds
{
flag = 0;
if (strchr(cstrings[i], '*') != NULL || strchr(cstrings[i], '?') != NULL || (strchr(cstrings[i], '[') != NULL && strchr(cstrings[i], ']') != NULL) || (strchr(cstrings[i], '{') != NULL && strchr(cstrings[i], '}') != NULL))
{
if (glob(cstrings[i], GLOB_NOCHECK | GLOB_TILDE, NULL, &glob_result) != 0)
{
printf("Error: Could not expand wildcards in command\n");
exit(1);
}
else
{
flag = 1;
}
}
if (flag == 1)
{ // found wild character
cstrings.erase(cstrings.begin() + i);
int temp = i;
for (int r = 0; r < glob_result.gl_pathc; r++)
{
cstrings.insert(cstrings.begin() + temp, glob_result.gl_pathv[r]);
temp++;
}
}
}
return cstrings;
}