glob.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <glob.h>
  2. #include <fnmatch.h>
  3. #include <sys/stat.h>
  4. #include <dirent.h>
  5. #include <limits.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <errno.h>
  9. #include <stddef.h>
  10. #include <unistd.h>
  11. #include <stdio.h>
  12. #include "libc.h"
  13. struct match
  14. {
  15. struct match *next;
  16. char name[1];
  17. };
  18. static int is_literal(const char *p, int useesc)
  19. {
  20. int bracket = 0;
  21. for (; *p; p++) {
  22. switch (*p) {
  23. case '\\':
  24. if (!useesc) break;
  25. case '?':
  26. case '*':
  27. return 0;
  28. case '[':
  29. bracket = 1;
  30. break;
  31. case ']':
  32. if (bracket) return 0;
  33. break;
  34. }
  35. }
  36. return 1;
  37. }
  38. static int append(struct match **tail, const char *name, size_t len, int mark)
  39. {
  40. struct match *new = malloc(sizeof(struct match) + len + 1);
  41. if (!new) return -1;
  42. (*tail)->next = new;
  43. new->next = NULL;
  44. strcpy(new->name, name);
  45. if (mark) strcat(new->name, "/");
  46. *tail = new;
  47. return 0;
  48. }
  49. static int match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
  50. {
  51. DIR *dir;
  52. long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
  53. struct dirent *de;
  54. char pat[strlen(p)+1];
  55. char *p2;
  56. size_t l = strlen(d);
  57. int literal;
  58. int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
  59. int error;
  60. if ((p2 = strchr(p, '/'))) {
  61. strcpy(pat, p);
  62. pat[p2-p] = 0;
  63. for (; *p2 == '/'; p2++);
  64. p = pat;
  65. }
  66. literal = is_literal(p, !(flags & GLOB_NOESCAPE));
  67. if (*d == '/' && !*(d+1)) l = 0;
  68. /* rely on opendir failing for nondirectory objects */
  69. dir = opendir(*d ? d : ".");
  70. error = errno;
  71. if (!dir) {
  72. /* this is not an error -- we let opendir call stat for us */
  73. if (error == ENOTDIR) return 0;
  74. if (error == EACCES && !*p) {
  75. struct stat st;
  76. if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
  77. if (append(tail, d, l, l))
  78. return GLOB_NOSPACE;
  79. return 0;
  80. }
  81. }
  82. if (errfunc(d, error) || (flags & GLOB_ERR))
  83. return GLOB_ABORTED;
  84. return 0;
  85. }
  86. if (!*p) {
  87. error = append(tail, d, l, l) ? GLOB_NOSPACE : 0;
  88. closedir(dir);
  89. return error;
  90. }
  91. while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
  92. char namebuf[l+de->d_reclen+2], *name = namebuf;
  93. if (!literal && fnmatch(p, de->d_name, fnm_flags))
  94. continue;
  95. if (literal && strcmp(p, de->d_name))
  96. continue;
  97. if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
  98. continue;
  99. if (*d) {
  100. memcpy(name, d, l);
  101. name[l] = '/';
  102. strcpy(name+l+1, de->d_name);
  103. } else {
  104. name = de->d_name;
  105. }
  106. if (p2) {
  107. if ((error = match_in_dir(name, p2, flags, errfunc, tail))) {
  108. closedir(dir);
  109. return error;
  110. }
  111. } else {
  112. int mark = 0;
  113. if (flags & GLOB_MARK) {
  114. if (de->d_type)
  115. mark = S_ISDIR(de->d_type<<12);
  116. else {
  117. struct stat st;
  118. stat(name, &st);
  119. mark = S_ISDIR(st.st_mode);
  120. }
  121. }
  122. if (append(tail, name, l+de->d_reclen+1, mark)) {
  123. closedir(dir);
  124. return GLOB_NOSPACE;
  125. }
  126. }
  127. }
  128. closedir(dir);
  129. if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
  130. return GLOB_ABORTED;
  131. return 0;
  132. }
  133. static int ignore_err(const char *path, int err)
  134. {
  135. return 0;
  136. }
  137. static void freelist(struct match *head)
  138. {
  139. struct match *match, *next;
  140. for (match=head->next; match; match=next) {
  141. next = match->next;
  142. free(match);
  143. }
  144. }
  145. static int sort(const void *a, const void *b)
  146. {
  147. return strcmp(*(const char **)a, *(const char **)b);
  148. }
  149. int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
  150. {
  151. const char *p=pat, *d;
  152. struct match head = { .next = NULL }, *tail = &head;
  153. size_t cnt, i;
  154. size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
  155. int error = 0;
  156. if (*p == '/') {
  157. for (; *p == '/'; p++);
  158. d = "/";
  159. } else {
  160. d = "";
  161. }
  162. if (!errfunc) errfunc = ignore_err;
  163. if (!(flags & GLOB_APPEND)) {
  164. g->gl_offs = offs;
  165. g->gl_pathc = 0;
  166. g->gl_pathv = NULL;
  167. }
  168. if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
  169. if (error == GLOB_NOSPACE) {
  170. freelist(&head);
  171. return error;
  172. }
  173. for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
  174. if (!cnt) {
  175. if (flags & GLOB_NOCHECK) {
  176. tail = &head;
  177. if (append(&tail, pat, strlen(pat), 0))
  178. return GLOB_NOSPACE;
  179. cnt++;
  180. } else
  181. return GLOB_NOMATCH;
  182. }
  183. if (flags & GLOB_APPEND) {
  184. char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
  185. if (!pathv) {
  186. freelist(&head);
  187. return GLOB_NOSPACE;
  188. }
  189. g->gl_pathv = pathv;
  190. offs += g->gl_pathc;
  191. } else {
  192. g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
  193. if (!g->gl_pathv) {
  194. freelist(&head);
  195. return GLOB_NOSPACE;
  196. }
  197. for (i=0; i<offs; i++)
  198. g->gl_pathv[i] = NULL;
  199. }
  200. for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
  201. g->gl_pathv[offs + i] = tail->name;
  202. g->gl_pathv[offs + i] = NULL;
  203. g->gl_pathc += cnt;
  204. if (!(flags & GLOB_NOSORT))
  205. qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
  206. return error;
  207. }
  208. void globfree(glob_t *g)
  209. {
  210. size_t i;
  211. for (i=0; i<g->gl_pathc; i++)
  212. free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
  213. free(g->gl_pathv);
  214. g->gl_pathc = 0;
  215. g->gl_pathv = NULL;
  216. }
  217. LFS64(glob);
  218. LFS64(globfree);