1
0
mirror of https://gitlab.com/klmp200/fish.git synced 2024-06-18 04:31:58 +00:00
fish/fish_shell/fish_globbing.c

108 lines
1.9 KiB
C
Raw Normal View History

2017-05-15 12:32:41 +00:00
// Created by Aethor
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
2017-05-16 05:32:19 +00:00
#include <string.h>
2017-05-15 12:32:41 +00:00
#include "fish_core.h"
#include "fish_globbing.h"
2017-05-16 05:32:19 +00:00
WordList * fishExpand(WordList *wordList) {
2017-05-15 12:32:41 +00:00
2017-05-16 05:32:19 +00:00
if(wordList->size > 1){
2017-05-15 12:32:41 +00:00
2017-05-27 15:36:55 +00:00
int i;
WordList* newWordList = createWordList();// creating the list to return
2017-05-15 12:32:41 +00:00
2017-05-27 15:36:55 +00:00
if(newWordList == NULL){//crash when the allocation is unsuccessful
2017-05-16 05:32:19 +00:00
crash();
2017-05-27 15:36:55 +00:00
}
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
addEndWordList(newWordList, wordList->first->word);//copy the command into the returning word list
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
WordListElement* tempElement = wordList->first->next; //temporary nav element
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
for(i=1; i<wordList->size; i++){
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
//TODO : optimize the stringContains() function to test for a list of characters
//test if we have to expand a string or not, for optimization purposes
if(stringContains(tempElement->word, '*') || stringContains(tempElement->word, '?')){
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
concatWordList(newWordList, expandWord(tempElement->word));
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
}
//If we dont have to expand, add the current word unchanged to the new list
else{
addEndWordList(newWordList, tempElement->word);
}
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
tempElement = tempElement->next;
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
}
freeWordList(wordList);
2017-05-16 05:32:19 +00:00
return newWordList;
2017-05-27 15:36:55 +00:00
2017-05-16 05:32:19 +00:00
}
else return wordList;
2017-05-15 12:32:41 +00:00
2017-05-27 15:36:55 +00:00
}
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
WordList* expandWord(char* word){
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
WordList* wordList = createWordList();
addEndWordList(wordList, word);
return wordList;
2017-05-16 05:32:19 +00:00
2017-05-27 15:36:55 +00:00
}
2017-05-16 05:32:19 +00:00
2017-05-15 12:32:41 +00:00
WordArray * getFiles(char* path){
DIR* directory;
dirent* dir;
int i = 0;
2017-05-16 05:32:19 +00:00
2017-05-15 12:32:41 +00:00
WordArray* files = (WordArray*) malloc(sizeof(WordArray));
if((directory = opendir(path)) != NULL){
while((dir = readdir(directory)) != NULL){
i++;
}
files->words = (char **) malloc(sizeof(char*) * (i + 1));
closedir(directory);
directory = opendir(path);
i = 0;
while((dir = readdir(directory)) != NULL){
2017-05-16 05:32:19 +00:00
if(!strcmp(dir->d_name, ".") && !strcmp(dir->d_name, "..")){
2017-05-15 12:32:41 +00:00
2017-05-15 17:10:46 +00:00
printf("%s\n", dir->d_name);
files->words[i] = dir->d_name;
i++;
files->size++;
2017-05-16 05:32:19 +00:00
}
2017-05-15 17:10:46 +00:00
2017-05-15 12:32:41 +00:00
}
}
return files;
}