wildcard.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * File: wildcard.c
  3. * Author: Arthur Brandao
  4. *
  5. * Created on 7 novembre 2018
  6. */
  7. #define _DEFAULT_SOURCE
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <dirent.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <unistd.h>
  14. #include <fnmatch.h>
  15. #include <string.h>
  16. #include "wildcard.h"
  17. int wildcard_result(const char* seq) {
  18. //Declaration variable
  19. struct dirent **namelist;
  20. int nbFile, nbRes = 0;
  21. //Recup la liste des fichiers dans le dossier courant
  22. nbFile = scandir(".", &namelist, 0, alphasort);
  23. if (nbFile < 0) {
  24. perror("scandir() : ");
  25. return ERR;
  26. }
  27. //Parcours chaque fichier pour compter le nombre de resultat
  28. while (nbFile--) {
  29. //Si c'est bien un fichier (et non un dossier)
  30. if (namelist[nbFile]->d_type == DT_REG) {
  31. //Test par rapport au wildcard
  32. if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
  33. //Regarde si le resultat est bien different de la wildcard
  34. if (strcmp(seq, namelist[nbFile]->d_name) != 0) {
  35. nbRes++;
  36. }
  37. }
  38. }
  39. free(namelist[nbFile]);
  40. }
  41. free(namelist);
  42. //Retour
  43. return nbRes;
  44. }
  45. //Sequence + taille du tableau result + tableau de retour vide
  46. int wildcard(const char* seq, int size, char** result) {
  47. //Declaration variable
  48. struct dirent **namelist;
  49. int nbFile, nbRes = 0;
  50. //Verification parametre
  51. if (size < 1) {
  52. return ERR;
  53. }
  54. //Recup la liste des fichiers dans le dossier courant
  55. nbFile = scandir(".", &namelist, 0, alphasort);
  56. if (nbFile < 0) {
  57. perror("scandir() : ");
  58. return ERR;
  59. }
  60. //Parcours chaque fichier pour ajouter les resultats à result
  61. int i = 0;
  62. while (nbFile--) {
  63. //Si c'est bien un fichier (et non un dossier)
  64. if (namelist[nbFile]->d_type == DT_REG) {
  65. //Test par rapport au wildcard
  66. if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
  67. result[i] = malloc(strlen(namelist[nbFile]->d_name) * sizeof (char));
  68. strcpy(result[i++], namelist[nbFile]->d_name);
  69. nbRes++;
  70. }
  71. }
  72. //Nettoyage
  73. free(namelist[nbFile]);
  74. //Si il n'y a plus de place dans le tableau on s'arrete
  75. if (i == size) {
  76. break;
  77. }
  78. }
  79. free(namelist);
  80. //Retourne
  81. return nbRes;
  82. }
  83. //La position de la valeur à remplacer, le tableau dans lequel il faut remplacer, sa taille, le tableau à insérer, sa taille, la nouvelle taille
  84. char** insert_array(int pos, char** array, int arraysize, char** insert, int insertsize, int* newsize) {
  85. //Erreur parametre
  86. if (pos < 0 || arraysize < 1 || insertsize < 1 || pos >= arraysize) {
  87. if (newsize != NULL) {
  88. *newsize = ERR;
  89. }
  90. return NULL;
  91. }
  92. //Si une seul valeur à insérer
  93. if (insertsize == 1) {
  94. //Vide la chaine precedente
  95. free(array[pos]);
  96. //Remplace
  97. array[pos] = malloc(strlen(insert[0]) * sizeof (char));
  98. strcpy(array[pos], insert[0]);
  99. //Nettoyage
  100. free(insert[0]);
  101. free(insert);
  102. //Retourne le tableau
  103. if (newsize != NULL) {
  104. *newsize = arraysize;
  105. }
  106. return array;
  107. }
  108. //Sinon generation d'un nouveau tableau
  109. char ** newarray;
  110. int size = arraysize + insertsize - 1, i;
  111. newarray = malloc(sizeof (char*) * size);
  112. //Ajout des elements avant la postion
  113. for (i = 0; i < pos; i++) {
  114. //Si l'element est null
  115. if (array[i] == NULL) {
  116. newarray[i] = NULL;
  117. }
  118. //Sinon on le copie
  119. else {
  120. newarray[i] = malloc(strlen(array[i]) * sizeof (char));
  121. strcpy(newarray[i], array[i]);
  122. free(array[i]);
  123. }
  124. }
  125. //Ajout des nouveaux elements
  126. for (int j = 0; j < insertsize; j++, i++) {
  127. //Si l'element est null
  128. if (insert[j] == NULL) {
  129. newarray[i] = NULL;
  130. }
  131. //Sinon on le copie
  132. else {
  133. newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
  134. strcpy(newarray[i], insert[j]);
  135. free(insert[j]);
  136. }
  137. }
  138. //Ajout fin
  139. for (int j = pos + 1; j < arraysize; j++, i++) {
  140. //Si l'element est null
  141. if (array[j] == NULL) {
  142. newarray[i] = NULL;
  143. }
  144. //Sinon on le copie
  145. else {
  146. newarray[i] = malloc(strlen(array[j]) * sizeof (char));
  147. strcpy(newarray[i], array[j]);
  148. free(array[j]);
  149. }
  150. }
  151. //Nettoyage et changement tableau
  152. free(array[pos]);
  153. free(array);
  154. free(insert);
  155. //Indique la nouvelle taille
  156. if (newsize != NULL) {
  157. *newsize = size;
  158. }
  159. //Retourne le nombre d'element
  160. return newarray;
  161. }