Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void expandWC( char * prefix, char *suffix){
- int isDir = 0;
- if(prefix[0] == 0){
- if(suffix[0] == '/'){
- isDir = 1;
- }
- }
- if(*suffix == 0) {
- if(numFiles == maxFiles) {
- maxFiles *= 2;
- files = (char **)realloc(files, maxFiles*sizeof(char*));
- //assert(files != NULL);
- }
- files[numFiles] = strdup(prefix);
- numFiles++;
- return;
- }
- char * s = strchr(suffix + isDir, '/' );
- char component[MAXFILENAME];
- for(int i = 0; i < MAXFILENAME; i++) {//it cries if you don't clear it
- component[i] = '\0';
- }
- if (s!=NULL){ // Copy up to the first /
- strncpy(component, (suffix + isDir) , (s-suffix - isDir));
- suffix = s + 1;
- }
- else { // Last part of path. Copy whole thing.
- strcpy(component, suffix);
- suffix = suffix + strlen(suffix);
- }
- // Now we need to expand the component
- char newPrefix[MAXFILENAME];
- if ( !strchr(component, '*') && !strchr(component, '&')) {
- // component does not have wildcards
- sprintf(newPrefix, "%s/%s" , prefix, component);
- expandWC(newPrefix, suffix);
- return;
- }
- //reg-ex up in this b*tch
- char *reg = (char *)malloc(2*strlen(suffix)+10);
- memset(reg, 0, (2*strlen(suffix)+10));
- char *c = component;
- char *r = reg;
- *r = '^';//get this regex party started
- r++;
- while(*c){
- if(*c == '*'){
- *r = '.';
- r++;
- *r = '*';
- r++;
- }
- else if(*c == '?'){
- *r = '.';
- r++;
- }
- else if(*c == '.'){
- *r = '\\';
- r++;
- *r = '.';
- r++;
- }
- else{
- *r = *c;
- r ++;
- }
- c++;
- }
- *r = '$';
- r++;
- *r = 0;//Party's over, go home
- char * expbuf = compile(reg, 0, 0);
- char * dir;
- // If prefix is empty then list current directory
- if (!strcmp(prefix, "")){
- if(isDir == 1){
- dir = strdup("/");
- }
- else{
- dir= strdup(".");
- }
- }
- else{
- dir=strdup(prefix);
- }
- int isHidden = 0;
- DIR * d = opendir(dir);
- if (d==NULL) {
- return;
- }
- if(prefix == NULL){
- prefix = strdup("");
- }
- if(reg[1] == '\\' && reg[2] == ','){
- isHidden = 1;
- }
- else{
- isHidden = 0;
- }
- struct dirent *ent;
- while ((ent = readdir(d))!= NULL) {
- // Check if name matches
- if (advance(ent->d_name, expbuf)) {
- wcDone = 1;
- if(isHidden == 1){
- if(strcmp(prefix, "") || isDir == 1){
- sprintf(newPrefix, "%s/%s" , prefix, ent->d_name);
- }
- else{
- sprintf(newPrefix, "%s" , ent->d_name);
- }
- expandWC(newPrefix,suffix);
- }
- else{
- if(ent->d_name[0] != '.'){//removing dirs/files starting with a .
- if(strcmp(prefix, "") || isDir == 1){
- sprintf(newPrefix, "%s/%s" , prefix, ent->d_name);
- }
- else{
- sprintf(newPrefix, "%s" , prefix, ent->d_name);
- }
- expandWC(newPrefix,suffix);
- }
- }
- }
- }
- closedir(d);
- free(dir);
- free(reg);//free reg
- }
Add Comment
Please, Sign In to add comment