|
@@ -18,12 +18,45 @@
|
|
#include "error.h"
|
|
#include "error.h"
|
|
#include "wildcard.h"
|
|
#include "wildcard.h"
|
|
|
|
|
|
-int wildcard_result(const char* seq) {
|
|
|
|
|
|
+/* --- Fonctions privées --- */
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Determine la position de separation entre le chemin et la wildcard
|
|
|
|
+ * @param char* La chaine
|
|
|
|
+ * @return int Position dans la chaine
|
|
|
|
+ */
|
|
|
|
+int wilcard_begin(const char* seq) {
|
|
|
|
+ int pos = 0, compteur = 1;
|
|
|
|
+ while (*seq) {
|
|
|
|
+ if (*seq == '/') {
|
|
|
|
+ pos = compteur;
|
|
|
|
+ }
|
|
|
|
+ seq++;
|
|
|
|
+ compteur++;
|
|
|
|
+ }
|
|
|
|
+ return pos;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* --- Fonctions publiques --- */
|
|
|
|
+int wildcard_result(char* seq) {
|
|
//Declaration variable
|
|
//Declaration variable
|
|
struct dirent **namelist;
|
|
struct dirent **namelist;
|
|
- int nbFile, nbRes = 0;
|
|
|
|
|
|
+ int nbFile, nbRes = 0, wcpos;
|
|
|
|
+ char* wc, *path;
|
|
|
|
+ //Trouve la wildcard
|
|
|
|
+ wcpos = wilcard_begin(seq);
|
|
|
|
+ if (!wcpos) {
|
|
|
|
+ path = malloc(sizeof (char) * 2);
|
|
|
|
+ strcpy(path, ".");
|
|
|
|
+ wc = seq;
|
|
|
|
+ } else {
|
|
|
|
+ path = malloc(sizeof (char) * wcpos);
|
|
|
|
+ memset(path, 0, wcpos);
|
|
|
|
+ strncpy(path, seq, wcpos - 1); //Pour ne pas prendre le / final
|
|
|
|
+ wc = seq + wcpos;
|
|
|
|
+ }
|
|
//Recup la liste des fichiers dans le dossier courant
|
|
//Recup la liste des fichiers dans le dossier courant
|
|
- nbFile = scandir(".", &namelist, 0, alphasort);
|
|
|
|
|
|
+ nbFile = scandir(path, &namelist, 0, alphasort);
|
|
if (nbFile < 0) {
|
|
if (nbFile < 0) {
|
|
addperror("Erreur scandir()");
|
|
addperror("Erreur scandir()");
|
|
return ERR;
|
|
return ERR;
|
|
@@ -33,9 +66,9 @@ int wildcard_result(const char* seq) {
|
|
//Si c'est bien un fichier (et non un dossier)
|
|
//Si c'est bien un fichier (et non un dossier)
|
|
if (namelist[nbFile]->d_type == DT_REG) {
|
|
if (namelist[nbFile]->d_type == DT_REG) {
|
|
//Test par rapport au wildcard
|
|
//Test par rapport au wildcard
|
|
- if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
|
|
|
|
|
|
+ if (fnmatch(wc, namelist[nbFile]->d_name, 0) == 0) {
|
|
//Regarde si le resultat est bien different de la wildcard
|
|
//Regarde si le resultat est bien different de la wildcard
|
|
- if (strcmp(seq, namelist[nbFile]->d_name) != 0) {
|
|
|
|
|
|
+ if (strcmp(wc, namelist[nbFile]->d_name) != 0) {
|
|
nbRes++;
|
|
nbRes++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -47,16 +80,29 @@ int wildcard_result(const char* seq) {
|
|
return nbRes;
|
|
return nbRes;
|
|
}
|
|
}
|
|
|
|
|
|
-int wildcard(const char* seq, int size, char** result) {
|
|
|
|
|
|
+int wildcard(char* seq, int size, char** result) {
|
|
//Declaration variable
|
|
//Declaration variable
|
|
struct dirent **namelist;
|
|
struct dirent **namelist;
|
|
- int nbFile, nbRes = 0;
|
|
|
|
|
|
+ int nbFile, nbRes = 0, wcpos;
|
|
|
|
+ char* wc, *path;
|
|
//Verification parametre
|
|
//Verification parametre
|
|
if (size < 1) {
|
|
if (size < 1) {
|
|
- return ERR;
|
|
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+ //Trouve la wildcard
|
|
|
|
+ wcpos = wilcard_begin(seq);
|
|
|
|
+ if (!wcpos) {
|
|
|
|
+ path = malloc(sizeof (char) * 2);
|
|
|
|
+ strcpy(path, ".");
|
|
|
|
+ wc = seq;
|
|
|
|
+ } else {
|
|
|
|
+ path = malloc(sizeof (char) * wcpos);
|
|
|
|
+ memset(path, 0, wcpos);
|
|
|
|
+ strncpy(path, seq, wcpos - 1);
|
|
|
|
+ wc = seq + wcpos;
|
|
}
|
|
}
|
|
//Recup la liste des fichiers dans le dossier courant
|
|
//Recup la liste des fichiers dans le dossier courant
|
|
- nbFile = scandir(".", &namelist, 0, alphasort);
|
|
|
|
|
|
+ nbFile = scandir(path, &namelist, 0, alphasort);
|
|
if (nbFile < 0) {
|
|
if (nbFile < 0) {
|
|
addperror("Erreur scandir()");
|
|
addperror("Erreur scandir()");
|
|
return ERR;
|
|
return ERR;
|
|
@@ -67,9 +113,17 @@ int wildcard(const char* seq, int size, char** result) {
|
|
//Si c'est bien un fichier (et non un dossier)
|
|
//Si c'est bien un fichier (et non un dossier)
|
|
if (namelist[nbFile]->d_type == DT_REG) {
|
|
if (namelist[nbFile]->d_type == DT_REG) {
|
|
//Test par rapport au wildcard
|
|
//Test par rapport au wildcard
|
|
- if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
|
|
|
|
- result[i] = malloc(strlen(namelist[nbFile]->d_name) * sizeof (char));
|
|
|
|
- strcpy(result[i++], namelist[nbFile]->d_name);
|
|
|
|
|
|
+ if (fnmatch(wc, namelist[nbFile]->d_name, 0) == 0) {
|
|
|
|
+ //Si il y a un chemin
|
|
|
|
+ if (wcpos) {
|
|
|
|
+ result[i] = malloc(sizeof (char) * (strlen(path) + strlen(namelist[nbFile]->d_name) + 2));
|
|
|
|
+ memset(result[i], 0, strlen(path) + strlen(namelist[nbFile]->d_name) + 2);
|
|
|
|
+ sprintf(result[i++], "%s/%s", path, namelist[nbFile]->d_name);
|
|
|
|
+ } else {
|
|
|
|
+ result[i] = malloc(sizeof (char) * (strlen(namelist[nbFile]->d_name) + 1));
|
|
|
|
+ memset(result[i], 0, strlen(namelist[nbFile]->d_name) + 1);
|
|
|
|
+ sprintf(result[i++], "%s", namelist[nbFile]->d_name);
|
|
|
|
+ }
|
|
nbRes++;
|
|
nbRes++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -118,8 +172,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
|
|
//Si l'element est null
|
|
//Si l'element est null
|
|
if (array[i] == NULL) {
|
|
if (array[i] == NULL) {
|
|
newarray[i] = NULL;
|
|
newarray[i] = NULL;
|
|
- }
|
|
|
|
- //Sinon on le copie
|
|
|
|
|
|
+ } //Sinon on le copie
|
|
else {
|
|
else {
|
|
newarray[i] = malloc(strlen(array[i]) * sizeof (char));
|
|
newarray[i] = malloc(strlen(array[i]) * sizeof (char));
|
|
strcpy(newarray[i], array[i]);
|
|
strcpy(newarray[i], array[i]);
|
|
@@ -131,8 +184,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
|
|
//Si l'element est null
|
|
//Si l'element est null
|
|
if (insert[j] == NULL) {
|
|
if (insert[j] == NULL) {
|
|
newarray[i] = NULL;
|
|
newarray[i] = NULL;
|
|
- }
|
|
|
|
- //Sinon on le copie
|
|
|
|
|
|
+ } //Sinon on le copie
|
|
else {
|
|
else {
|
|
newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
|
|
newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
|
|
strcpy(newarray[i], insert[j]);
|
|
strcpy(newarray[i], insert[j]);
|
|
@@ -144,8 +196,7 @@ char** insert_array(int pos, char** array, int arraysize, char** insert, int ins
|
|
//Si l'element est null
|
|
//Si l'element est null
|
|
if (array[j] == NULL) {
|
|
if (array[j] == NULL) {
|
|
newarray[i] = NULL;
|
|
newarray[i] = NULL;
|
|
- }
|
|
|
|
- //Sinon on le copie
|
|
|
|
|
|
+ } //Sinon on le copie
|
|
else {
|
|
else {
|
|
newarray[i] = malloc(strlen(array[j]) * sizeof (char));
|
|
newarray[i] = malloc(strlen(array[j]) * sizeof (char));
|
|
strcpy(newarray[i], array[j]);
|
|
strcpy(newarray[i], array[j]);
|