From ee951fb2cd14b7c2a9d40dbb9f8b9e772614f15b Mon Sep 17 00:00:00 2001 From: Aethor Date: Sun, 28 May 2017 22:02:05 +0200 Subject: [PATCH] non-recursive globbing --- fish_shell/fish_globbing.c | 93 ++++++++++++++++++++++++++++++++++---- fish_shell/fish_globbing.h | 6 ++- fish_shell/fish_utils.c | 2 +- 3 files changed, 90 insertions(+), 11 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 0e46653..6652e52 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -51,19 +51,26 @@ WordList * fishExpand(WordList *wordList) { 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; dirent* dir; @@ -75,10 +82,16 @@ WordList* getFiles(char* path){ 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 - addEndWordList(files, dir->d_name); + //dont read this, and if you do, do not complain, it's strcat's fault. you've been warned. + 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; + + } + +} + + diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index e226a19..8804afc 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -4,10 +4,12 @@ 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); +int wildcardedStringMatches(char* string1, char* string2); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index c164493..8a6c117 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -311,7 +311,7 @@ void printWordList(WordList* list){ } else{ - printf("fish : Warning : list is null. Are you stupid ?"); + fprintf(stderr, "fish : Warning : list is null. Are you stupid ?\n"); } }