From 8eabe265656468c311a86f8dacf502842779ac16 Mon Sep 17 00:00:00 2001 From: Aethor Date: Mon, 29 May 2017 17:25:31 +0200 Subject: [PATCH] =?UTF-8?q?Un=20globbing=20r=C3=A9cursif,=20mais=20pas=20d?= =?UTF-8?q?es=20masses?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fish_shell/fish_globbing.c | 83 ++++++++++++++++++++++++++++++++------ fish_shell/fish_globbing.h | 4 ++ fish_shell/fish_utils.c | 12 ++++++ fish_shell/fish_utils.h | 2 + 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/fish_shell/fish_globbing.c b/fish_shell/fish_globbing.c index 35a9ba6..6cc6efe 100644 --- a/fish_shell/fish_globbing.c +++ b/fish_shell/fish_globbing.c @@ -64,19 +64,79 @@ WordList* expandWord(char* word){ else{ - WordList* testList = splitWordIntoList(word, '/'); - printWordList(testList); - freeWordList(testList); + WordList* pathList = splitWordIntoList(word, '/'); + WordList* expandedArgsList = createWordList(); + if (expandedArgsList == NULL) crash(); + WordListElement* tempElement; + int i; - return getFiles(word, (char*) "*");//temporary - //return(); + if(pathList->size >= 1){ + tempElement = pathList->first; + for(i=0; i < pathList->size; i++){ + + printf("\nBASE PATH %s\n", tempElement->word); + char* tempPath = getPath(tempElement->word); + printf("\nPATH : %s\n", tempPath); + char* tempFileName = getFileName(tempElement->word); + printf("\nFILENAME : %s\n", tempFileName); + + concatWordList(expandedArgsList, getFiles(tempPath, tempFileName)); + printWordList(expandedArgsList); + tempElement = tempElement->next; + + free(tempPath); + free(tempFileName); + + } + } + + freeWordList(pathList); + + return expandedArgsList; + //return getFiles(word, (char*) "*");//temporary } } +char* getFileName(char* string){ + if(!stringContains(string, '/')){ + return string; + } + else{ + int i = 0; + while(string[i] != '/'){ + i++; + } + + return strndup(string + i + 1, strlen(string) - i); + } +} + + +//get path of a file +char* getPath(char* string){ + + if(!stringContains(string, '/')){ + return string; + } + else{ + + int i = strlen(string) - 1; + + while(i != -1 && string[i] != '/'){ + + i = i-1; + + } + + return strndup(string, i + 1); + + } + +} WordList* getFiles(char* path, char* wildcardedString){ @@ -85,6 +145,7 @@ WordList* getFiles(char* path, char* wildcardedString){ dirent* dir; WordList* files = createWordList(); + if(files == NULL) crash(); if((directory = opendir(path)) != NULL){ @@ -93,12 +154,7 @@ WordList* getFiles(char* path, char* wildcardedString){ if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..") && wildcardedStringMatches(wildcardedString, dir->d_name)){//sorry strcmp but I dont like you :( - //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); + char* filePath = trueStrcat(path, dir->d_name); addEndWordList(files, filePath); free(filePath); @@ -185,9 +241,10 @@ WordList* splitWordIntoList(char* string, char splitChar){ int i = 0; int mark = 0; - int finished = 0; - int firstEncounter = 0; + int finished = 0; //boolean + int firstEncounter = 0; //boolean WordList* newWordList = createWordList(); + if(newWordList == NULL) crash(); while(!finished){ diff --git a/fish_shell/fish_globbing.h b/fish_shell/fish_globbing.h index 74fc3b5..0bdaae4 100644 --- a/fish_shell/fish_globbing.h +++ b/fish_shell/fish_globbing.h @@ -14,4 +14,8 @@ int wildcardedStringMatches(char* string1, char* string2); WordList* splitWordIntoList(char* string, char splitchar); +char* getFileName(char* string); + +char* getPath(char* string); + #endif //FISH_FISH_GLOBBING_H diff --git a/fish_shell/fish_utils.c b/fish_shell/fish_utils.c index febdbe2..34cdc7b 100644 --- a/fish_shell/fish_utils.c +++ b/fish_shell/fish_utils.c @@ -331,3 +331,15 @@ int stringContains(char * string, char charToTest){ return 0; } + +//dont read this, and if you do, do not complain, it's strcat's fault. you've been warned. +char* trueStrcat(char* string1, char* string2){ + + char* newString = (char*) malloc(sizeof(char) * (strlen(string1) + strlen(string2)) + 1); + if(newString == NULL) crash(); + newString[0] = '\0'; + strcat(newString, string1); + strcat(newString, string2); + return newString; + +} diff --git a/fish_shell/fish_utils.h b/fish_shell/fish_utils.h index 91f458c..d38bca3 100644 --- a/fish_shell/fish_utils.h +++ b/fish_shell/fish_utils.h @@ -41,5 +41,7 @@ void printWordList(WordList* list); int stringContains(char* string, char charToTest); +char* trueStrcat(char* string1, char* string2); + #endif //FISH_FISH_UTILS_H