wildcard.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "error.h"
  17. #include "wildcard.h"
  18. int wildcard_result(const char* seq) {
  19. //Declaration variable
  20. struct dirent **namelist;
  21. int nbFile, nbRes = 0;
  22. //Recup la liste des fichiers dans le dossier courant
  23. nbFile = scandir(".", &namelist, 0, alphasort);
  24. if (nbFile < 0) {
  25. addperror("Erreur scandir()");
  26. return ERR;
  27. }
  28. //Parcours chaque fichier pour compter le nombre de resultat
  29. while (nbFile--) {
  30. //Si c'est bien un fichier (et non un dossier)
  31. if (namelist[nbFile]->d_type == DT_REG) {
  32. //Test par rapport au wildcard
  33. if (fnmatch(seq, namelist[nbFile]->d_name, 0) == 0) {
  34. //Regarde si le resultat est bien different de la wildcard
  35. if (strcmp(seq, namelist[nbFile]->d_name) != 0) {
  36. nbRes++;
  37. }
  38. }
  39. }
  40. free(namelist[nbFile]);
  41. }
  42. free(namelist);
  43. //Retour
  44. return nbRes;
  45. }
  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. addperror("Erreur 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. char** insert_array(int pos, char** array, int arraysize, char** insert, int insertsize, int* newsize) {
  84. //Erreur parametre
  85. if (pos < 0 || arraysize < 1 || insertsize < 1 || pos >= arraysize) {
  86. if (newsize != NULL) {
  87. *newsize = ERR;
  88. }
  89. return NULL;
  90. }
  91. //Si une seul valeur à insérer
  92. if (insertsize == 1) {
  93. //Vide la chaine precedente
  94. free(array[pos]);
  95. //Remplace
  96. array[pos] = malloc(strlen(insert[0]) * sizeof (char));
  97. strcpy(array[pos], insert[0]);
  98. //Nettoyage
  99. free(insert[0]);
  100. free(insert);
  101. //Retourne le tableau
  102. if (newsize != NULL) {
  103. *newsize = arraysize;
  104. }
  105. return array;
  106. }
  107. //Sinon generation d'un nouveau tableau
  108. char ** newarray;
  109. int size = arraysize + insertsize - 1, i;
  110. newarray = malloc(sizeof (char*) * size);
  111. //Ajout des elements avant la postion
  112. for (i = 0; i < pos; i++) {
  113. //Si l'element est null
  114. if (array[i] == NULL) {
  115. newarray[i] = NULL;
  116. }
  117. //Sinon on le copie
  118. else {
  119. newarray[i] = malloc(strlen(array[i]) * sizeof (char));
  120. strcpy(newarray[i], array[i]);
  121. free(array[i]);
  122. }
  123. }
  124. //Ajout des nouveaux elements
  125. for (int j = 0; j < insertsize; j++, i++) {
  126. //Si l'element est null
  127. if (insert[j] == NULL) {
  128. newarray[i] = NULL;
  129. }
  130. //Sinon on le copie
  131. else {
  132. newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
  133. strcpy(newarray[i], insert[j]);
  134. free(insert[j]);
  135. }
  136. }
  137. //Ajout fin
  138. for (int j = pos + 1; j < arraysize; j++, i++) {
  139. //Si l'element est null
  140. if (array[j] == NULL) {
  141. newarray[i] = NULL;
  142. }
  143. //Sinon on le copie
  144. else {
  145. newarray[i] = malloc(strlen(array[j]) * sizeof (char));
  146. strcpy(newarray[i], array[j]);
  147. free(array[j]);
  148. }
  149. }
  150. //Nettoyage et changement tableau
  151. free(array[pos]);
  152. free(array);
  153. free(insert);
  154. //Indique la nouvelle taille
  155. if (newsize != NULL) {
  156. *newsize = size;
  157. }
  158. //Retourne le nombre d'element
  159. return newarray;
  160. }