wildcard.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 -1;
  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 -1;
  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 -1;
  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 = -1;
  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. newarray[i] = malloc(strlen(array[i]) * sizeof (char));
  115. strcpy(newarray[i], array[i]);
  116. free(array[i]);
  117. }
  118. //Ajout des nouveaux elements
  119. for (int j = 0; j < insertsize; j++, i++) {
  120. newarray[i] = malloc(strlen(insert[j]) * sizeof (char));
  121. strcpy(newarray[i], insert[j]);
  122. free(insert[j]);
  123. }
  124. //Ajout fin
  125. for (int j = pos + 1; j < arraysize; j++, i++) {
  126. newarray[i] = malloc(strlen(array[j]) * sizeof (char));
  127. strcpy(newarray[i], array[j]);
  128. free(array[j]);
  129. }
  130. //Nettoyage et changement tableau
  131. free(array[pos]);
  132. free(array);
  133. free(insert);
  134. //Indique la nouvelle taille
  135. if (newsize != NULL) {
  136. *newsize = size;
  137. }
  138. //Retourne le nombre d'element
  139. return newarray;
  140. }