2017-05-15 12:32:41 +00:00
|
|
|
// Created by Aethor
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include "fish_core.h"
|
|
|
|
#include "fish_globbing.h"
|
|
|
|
|
2017-05-15 14:53:56 +00:00
|
|
|
WordList * fishExpand(WordList *wordArray) {
|
2017-05-15 12:32:41 +00:00
|
|
|
|
2017-05-15 17:10:46 +00:00
|
|
|
wordArray = expandInDir("./", "*");
|
2017-05-15 12:32:41 +00:00
|
|
|
|
|
|
|
return wordArray;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
WordArray * getFiles(char* path){
|
|
|
|
|
|
|
|
DIR* directory;
|
|
|
|
dirent* dir;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
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-15 17:10:46 +00:00
|
|
|
/*if(dir->d_name != "." && 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-15 12:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
int comparator(char* string1, char* string2){//TODO
|
2017-05-15 12:32:41 +00:00
|
|
|
|
2017-05-15 13:03:36 +00:00
|
|
|
int i = 0;
|
2017-05-15 14:34:40 +00:00
|
|
|
char tempIChar;
|
|
|
|
int j = 0;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
if(string1 != NULL && string2 != NULL){
|
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
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] != '?'){
|
2017-05-15 13:03:36 +00:00
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
return 0;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
2017-05-15 14:34:40 +00:00
|
|
|
j++;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
if(string1[i] == '\0' && string2[j] == '\0'){
|
2017-05-15 13:03:36 +00:00
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
return 1;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
|
2017-05-15 14:34:40 +00:00
|
|
|
return 0;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
printf("warning : fuck you, strings are considered null");
|
|
|
|
crash();
|
2017-05-15 14:34:40 +00:00
|
|
|
return 0;
|
2017-05-15 13:03:36 +00:00
|
|
|
|
|
|
|
}
|
2017-05-15 12:32:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-15 17:10:46 +00:00
|
|
|
WordList* expandInDir(char* dir, char* toExpand){
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
WordList* list = createWordList();
|
|
|
|
WordArray* files = getFiles(dir);
|
|
|
|
|
|
|
|
for(i=0; i<files->size;i++){
|
|
|
|
|
|
|
|
if(comparator(toExpand, files->words[i])){
|
|
|
|
|
|
|
|
addWordList(list, files->words[i]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-15 12:32:41 +00:00
|
|
|
|