Guest User

Untitled

a guest
Jan 14th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. void expandWC( char * prefix, char *suffix){
  2. int isDir = 0;
  3. if(prefix[0] == 0){
  4. if(suffix[0] == '/'){
  5. isDir = 1;
  6. }
  7. }
  8.  
  9. if(*suffix == 0) {
  10. if(numFiles == maxFiles) {
  11. maxFiles *= 2;
  12.  
  13. files = (char **)realloc(files, maxFiles*sizeof(char*));
  14. //assert(files != NULL);
  15. }
  16. files[numFiles] = strdup(prefix);
  17. numFiles++;
  18. return;
  19. }
  20.  
  21. char * s = strchr(suffix + isDir, '/' );
  22. char component[MAXFILENAME];
  23.  
  24. for(int i = 0; i < MAXFILENAME; i++) {//it cries if you don't clear it
  25. component[i] = '\0';
  26. }
  27.  
  28. if (s!=NULL){ // Copy up to the first /
  29. strncpy(component, (suffix + isDir) , (s-suffix - isDir));
  30. suffix = s + 1;
  31. }
  32.  
  33. else { // Last part of path. Copy whole thing.
  34. strcpy(component, suffix);
  35. suffix = suffix + strlen(suffix);
  36. }
  37.  
  38. // Now we need to expand the component
  39.  
  40. char newPrefix[MAXFILENAME];
  41.  
  42. if ( !strchr(component, '*') && !strchr(component, '&')) {
  43. // component does not have wildcards
  44. sprintf(newPrefix, "%s/%s" , prefix, component);
  45. expandWC(newPrefix, suffix);
  46. return;
  47. }
  48.  
  49. //reg-ex up in this b*tch
  50. char *reg = (char *)malloc(2*strlen(suffix)+10);
  51. memset(reg, 0, (2*strlen(suffix)+10));
  52. char *c = component;
  53. char *r = reg;
  54. *r = '^';//get this regex party started
  55. r++;
  56. while(*c){
  57. if(*c == '*'){
  58. *r = '.';
  59. r++;
  60. *r = '*';
  61. r++;
  62. }
  63. else if(*c == '?'){
  64. *r = '.';
  65. r++;
  66. }
  67. else if(*c == '.'){
  68. *r = '\\';
  69. r++;
  70. *r = '.';
  71. r++;
  72. }
  73. else{
  74. *r = *c;
  75. r ++;
  76. }
  77. c++;
  78. }
  79. *r = '$';
  80. r++;
  81. *r = 0;//Party's over, go home
  82.  
  83. char * expbuf = compile(reg, 0, 0);
  84. char * dir;
  85. // If prefix is empty then list current directory
  86. if (!strcmp(prefix, "")){
  87. if(isDir == 1){
  88. dir = strdup("/");
  89. }
  90. else{
  91. dir= strdup(".");
  92. }
  93. }
  94.  
  95. else{
  96. dir=strdup(prefix);
  97. }
  98. int isHidden = 0;
  99. DIR * d = opendir(dir);
  100.  
  101. if (d==NULL) {
  102. return;
  103. }
  104.  
  105. if(prefix == NULL){
  106. prefix = strdup("");
  107. }
  108.  
  109. if(reg[1] == '\\' && reg[2] == ','){
  110. isHidden = 1;
  111. }
  112. else{
  113. isHidden = 0;
  114. }
  115.  
  116. struct dirent *ent;
  117. while ((ent = readdir(d))!= NULL) {
  118. // Check if name matches
  119. if (advance(ent->d_name, expbuf)) {
  120. wcDone = 1;
  121.  
  122. if(isHidden == 1){
  123. if(strcmp(prefix, "") || isDir == 1){
  124. sprintf(newPrefix, "%s/%s" , prefix, ent->d_name);
  125. }
  126. else{
  127. sprintf(newPrefix, "%s" , ent->d_name);
  128. }
  129. expandWC(newPrefix,suffix);
  130. }
  131. else{
  132. if(ent->d_name[0] != '.'){//removing dirs/files starting with a .
  133. if(strcmp(prefix, "") || isDir == 1){
  134. sprintf(newPrefix, "%s/%s" , prefix, ent->d_name);
  135. }
  136. else{
  137. sprintf(newPrefix, "%s" , prefix, ent->d_name);
  138. }
  139. expandWC(newPrefix,suffix);
  140. }
  141. }
  142. }
  143. }
  144. closedir(d);
  145. free(dir);
  146. free(reg);//free reg
  147. }
Add Comment
Please, Sign In to add comment