non-recursive globbing

This commit is contained in:
Aethor 2017-05-28 22:02:05 +02:00
parent 2b5ad06977
commit ee951fb2cd
3 changed files with 90 additions and 11 deletions

View File

@ -51,19 +51,26 @@ WordList * fishExpand(WordList *wordList) {
WordList* expandWord(char* word){ WordList* expandWord(char* word){
printf("\n%s\n", word); if(!stringContains(word, '/')){
WordList* testList = getFiles((char*) "../"); return getFiles((char*) "./", word);
}
else{
return getFiles(word, "*");//temporary
//return();
}
printWordList(testList);
return testList;
} }
WordList* getFiles(char* path){ WordList* getFiles(char* path, char* wildcardedString){
DIR* directory; DIR* directory;
dirent* dir; dirent* dir;
@ -75,10 +82,16 @@ WordList* getFiles(char* path){
while((dir = readdir(directory)) != NULL){ while((dir = readdir(directory)) != NULL){
if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")){//sorry strcmp but I dont like you :( if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") && wildcardedStringMatches(wildcardedString, dir->d_name)){//sorry strcmp but I dont like you :(
printf("%s\n", dir->d_name);//test //dont read this, and if you do, do not complain, it's strcat's fault. you've been warned.
addEndWordList(files, dir->d_name); char* filePath = (char*) malloc(sizeof(char) * (strlen(path) + strlen(dir->d_name)) + 1);
if(filePath == NULL) crash();
filePath[0] = '\0';
strcat(filePath, path);
strcat(filePath, dir->d_name);
addEndWordList(files, filePath);
free(filePath);
} }
@ -93,3 +106,67 @@ WordList* getFiles(char* path){
} }
int wildcardedStringMatches(char* string1, char* string2){//TODO
int i = 0;
char tempIChar;
int j = 0;
if(string1 != NULL && string2 != NULL){
while(string1[i] != '\0' && string2[j] != '\0'){
if(string1[i] == '*'){
tempIChar = string1[i+1];
while(string2[j] != tempIChar){
j++;
if(string2[j] == '\0' && tempIChar == '\0'){
return 1;
}
}
i++;
}
if(string1[i] != string2[j] && string1[i] != '?'){
return 0;
}
i++;
j++;
}
if(string1[i] == '\0' && string2[j] == '\0'){
return 1;
}
else{
return 0;
}
}
else{
printf("warning : fuck you, strings are considered null");
crash();
return 0;
}
}

View File

@ -4,10 +4,12 @@
typedef struct dirent dirent; typedef struct dirent dirent;
WordList * fishExpand(WordList *wordArray); WordList* fishExpand(WordList* wordArray);
WordList* getFiles(char* path); WordList* getFiles(char* path, char* wildcardedString);
WordList* expandWord(char* word); WordList* expandWord(char* word);
int wildcardedStringMatches(char* string1, char* string2);
#endif //FISH_FISH_GLOBBING_H #endif //FISH_FISH_GLOBBING_H

View File

@ -311,7 +311,7 @@ void printWordList(WordList* list){
} }
else{ else{
printf("fish : Warning : list is null. Are you stupid ?"); fprintf(stderr, "fish : Warning : list is null. Are you stupid ?\n");
} }
} }