wildcard.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. int wildcard(const char* seq, int size, char** result) {
  46. //Declaration variable
  47. struct dirent **namelist;
  48. int nbFile, nbRes = 0;
  49. //Verification parametre
  50. if (size < 1) {
  51. return ERR;
  52. }
  53. //Recup la liste des fichiers dans le dossier courant
  54. nbFile = scandir(".", &namelist, 0, alphasort);
  55. if (nbFile < 0) {
  56. perror("scandir() : ");
  57. return ERR;
  58. }
  59. //Parcours chaque fichier pour ajouter les resultats à result
  60. int i = 0;
  61. while (nbFile--) {
  62. //Si c'est bien un fichier (et non un dossier)
  63. if (namelist[nbFile]->d_type == DT_REG) {
  64. //Test par rapport au wildcard
  65. if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
  66. result[i] = malloc(strlen(namelist[nbFile]->d_name) * sizeof (char));
  67. strcpy(result[i++], namelist[nbFile]->d_name);
  68. nbRes++;
  69. }
  70. }
  71. //Nettoyage
  72. free(namelist[nbFile]);
  73. //Si il n'y a plus de place dans le tableau on s'arrete
  74. if (i == size) {
  75. break;
  76. }
  77. }
  78. free(namelist);
  79. //Retourne
  80. return nbRes;
  81. }
  82. char** insert_array(int pos, char** array, int arraysize, char** insert, int insertsize, int* newsize) {
  83. //Erreur parametre
  84. if (pos < 0 || arraysize < 1 || insertsize < 1 || pos >= arraysize) {
  85. if (newsize != NULL) {
  86. *newsize = ERR;
  87. }
  88. return NULL;
  89. }
  90. //Si une seul valeur à insérer
  91. if (insertsize == 1) {
  92. //Vide la chaine precedente
  93. free(array[pos]);
  94. //Remplace
  95. array[pos] = malloc(strlen(insert[0]) * sizeof (char));
  96. strcpy(array[pos], insert[0]);
  97. //Nettoyage
  98. free(insert[0]);
  99. free(insert);
  100. //Retourne le tableau
  101. if (newsize != NULL) {
  102. *newsize = arraysize;
  103. }
  104. return array;
  105. }
  106. //Sinon generation d'un nouveau tableau
  107. char ** newarray;
  108. int size = arraysize + insertsize - 1, i;
  109. newarray = malloc(sizeof (char*) * size);
  110. //Ajout des elements avant la postion
  111. for (i = 0; i < pos; i++) {
  112. //Si l'element est null
  113. if (array[i] == NULL) {
  114. newarray[i] = NULL;
  115. }
  116. //Sinon on le copie
  117. else {
  118. newarray[i] = malloc(strlen(array[i]) * sizeof (char));
  119. strcpy(newarray[i], array[i]);
  120. free(array[i]);
  121. }
  122. }
  123. //Ajout des nouveaux elements
  124. for (int j = 0; j < insertsize; j++, i++) {
  125. //Si l'element est null
  126. if (insert[j] == NULL) {
  127. newarray[i] = NULL;
  128. }
  129. //Sinon on le copie
  130. else {
  131. newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
  132. strcpy(newarray[i], insert[j]);
  133. free(insert[j]);
  134. }
  135. }
  136. //Ajout fin
  137. for (int j = pos + 1; j < arraysize; j++, i++) {
  138. //Si l'element est null
  139. if (array[j] == NULL) {
  140. newarray[i] = NULL;
  141. }
  142. //Sinon on le copie
  143. else {
  144. newarray[i] = malloc(strlen(array[j]) * sizeof (char));
  145. strcpy(newarray[i], array[j]);
  146. free(array[j]);
  147. }
  148. }
  149. //Nettoyage et changement tableau
  150. free(array[pos]);
  151. free(array);
  152. free(insert);
  153. //Indique la nouvelle taille
  154. if (newsize != NULL) {
  155. *newsize = size;
  156. }
  157. //Retourne le nombre d'element
  158. return newarray;
  159. }