glob-susv3.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <features.h>
  7. #define BUILD_GLOB64
  8. #include <glob.h>
  9. #include <fnmatch.h>
  10. #include <sys/stat.h>
  11. #include <dirent.h>
  12. #include <limits.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include <errno.h>
  16. #include <stddef.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. struct match
  20. {
  21. struct match *next;
  22. char name[1];
  23. };
  24. #ifdef BUILD_GLOB64
  25. extern int __glob_is_literal(const char *p, int useesc) attribute_hidden;
  26. extern int __glob_append(struct match **tail, const char *name, size_t len, int mark) attribute_hidden;
  27. extern int __glob_ignore_err(const char *path, int err) attribute_hidden;
  28. extern void __glob_freelist(struct match *head) attribute_hidden;
  29. extern int __glob_sort(const void *a, const void *b) attribute_hidden;
  30. extern int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail) attribute_hidden;
  31. #endif
  32. # define stat stat64
  33. # define readdir_r readdir64_r
  34. # define dirent dirent64
  35. # define struct_stat struct stat64
  36. /* keep only one copy of these */
  37. #ifndef __GLOB64
  38. # ifndef BUILD_GLOB64
  39. static
  40. # endif
  41. int __glob_is_literal(const char *p, int useesc)
  42. {
  43. int bracket = 0;
  44. for (; *p; p++) {
  45. switch (*p) {
  46. case '\\':
  47. if (!useesc) break;
  48. case '?':
  49. case '*':
  50. return 0;
  51. case '[':
  52. bracket = 1;
  53. break;
  54. case ']':
  55. if (bracket) return 0;
  56. break;
  57. }
  58. }
  59. return 1;
  60. }
  61. # ifndef BUILD_GLOB64
  62. static
  63. # endif
  64. int __glob_append(struct match **tail, const char *name, size_t len, int mark)
  65. {
  66. struct match *new = malloc(sizeof(struct match) + len + 1);
  67. if (!new) return -1;
  68. (*tail)->next = new;
  69. new->next = NULL;
  70. strcpy(new->name, name);
  71. if (mark) strcat(new->name, "/");
  72. *tail = new;
  73. return 0;
  74. }
  75. # ifndef BUILD_GLOB64
  76. static
  77. # endif
  78. int __glob_match_in_dir(const char *d, const char *p, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
  79. {
  80. DIR *dir;
  81. long long de_buf[(sizeof(struct dirent) + NAME_MAX + sizeof(long long))/sizeof(long long)];
  82. struct dirent *de;
  83. char pat[strlen(p)+1];
  84. char *p2;
  85. size_t l = strlen(d);
  86. int literal;
  87. int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) | FNM_PERIOD;
  88. int error;
  89. if ((p2 = strchr(p, '/'))) {
  90. strcpy(pat, p);
  91. pat[p2-p] = 0;
  92. for (; *p2 == '/'; p2++);
  93. p = pat;
  94. }
  95. literal = __glob_is_literal(p, !(flags & GLOB_NOESCAPE));
  96. if (*d == '/' && !*(d+1)) l = 0;
  97. /* rely on opendir failing for nondirectory objects */
  98. dir = opendir(*d ? d : ".");
  99. error = errno;
  100. if (!dir) {
  101. /* this is not an error -- we let opendir call stat for us */
  102. if (error == ENOTDIR) return 0;
  103. if (error == EACCES && !*p) {
  104. struct_stat st;
  105. if (!stat(d, &st) && S_ISDIR(st.st_mode)) {
  106. if (__glob_append(tail, d, l, l))
  107. return GLOB_NOSPACE;
  108. return 0;
  109. }
  110. }
  111. if (errfunc(d, error) || (flags & GLOB_ERR))
  112. return GLOB_ABORTED;
  113. return 0;
  114. }
  115. if (!*p) {
  116. error = __glob_append(tail, d, l, l) ? GLOB_NOSPACE : 0;
  117. closedir(dir);
  118. return error;
  119. }
  120. while (!(error = readdir_r(dir, (void *)de_buf, &de)) && de) {
  121. char namebuf[l+de->d_reclen+2], *name = namebuf;
  122. if (!literal && fnmatch(p, de->d_name, fnm_flags))
  123. continue;
  124. if (literal && strcmp(p, de->d_name))
  125. continue;
  126. if (p2 && de->d_type && !S_ISDIR(de->d_type<<12) && !S_ISLNK(de->d_type<<12))
  127. continue;
  128. if (*d) {
  129. memcpy(name, d, l);
  130. name[l] = '/';
  131. strcpy(name+l+1, de->d_name);
  132. } else {
  133. name = de->d_name;
  134. }
  135. if (p2) {
  136. if ((error = __glob_match_in_dir(name, p2, flags, errfunc, tail))) {
  137. closedir(dir);
  138. return error;
  139. }
  140. } else {
  141. int mark = 0;
  142. if (flags & GLOB_MARK) {
  143. if (de->d_type)
  144. mark = S_ISDIR(de->d_type<<12);
  145. else {
  146. struct_stat st;
  147. stat(name, &st);
  148. mark = S_ISDIR(st.st_mode);
  149. }
  150. }
  151. if (__glob_append(tail, name, l+de->d_reclen+1, mark)) {
  152. closedir(dir);
  153. return GLOB_NOSPACE;
  154. }
  155. }
  156. }
  157. closedir(dir);
  158. if (error && (errfunc(d, error) || (flags & GLOB_ERR)))
  159. return GLOB_ABORTED;
  160. return 0;
  161. }
  162. # ifndef BUILD_GLOB64
  163. static
  164. # endif
  165. int __glob_ignore_err(const char * path attribute_unused,
  166. int err attribute_unused)
  167. {
  168. return 0;
  169. }
  170. # ifndef BUILD_GLOB64
  171. static
  172. # endif
  173. void __glob_freelist(struct match *head)
  174. {
  175. struct match *match, *next;
  176. for (match=head->next; match; match=next) {
  177. next = match->next;
  178. free(match);
  179. }
  180. }
  181. # ifndef BUILD_GLOB64
  182. static
  183. # endif
  184. int __glob_sort(const void *a, const void *b)
  185. {
  186. return strcmp(*(const char **)a, *(const char **)b);
  187. }
  188. #endif /* !__GLOB64 */
  189. int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
  190. {
  191. const char *p=pat, *d;
  192. struct match head = { .next = NULL }, *tail = &head;
  193. size_t cnt, i;
  194. size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
  195. int error = 0;
  196. if (*p == '/') {
  197. for (; *p == '/'; p++);
  198. d = "/";
  199. } else {
  200. d = "";
  201. }
  202. if (!errfunc) errfunc = __glob_ignore_err;
  203. if (!(flags & GLOB_APPEND)) {
  204. g->gl_offs = offs;
  205. g->gl_pathc = 0;
  206. g->gl_pathv = NULL;
  207. }
  208. if (*p) error = __glob_match_in_dir(d, p, flags, errfunc, &tail);
  209. if (error == GLOB_NOSPACE) {
  210. __glob_freelist(&head);
  211. return error;
  212. }
  213. for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
  214. if (!cnt) {
  215. if (flags & GLOB_NOCHECK) {
  216. tail = &head;
  217. if (__glob_append(&tail, pat, strlen(pat), 0))
  218. return GLOB_NOSPACE;
  219. cnt++;
  220. } else
  221. return GLOB_NOMATCH;
  222. }
  223. if (flags & GLOB_APPEND) {
  224. char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
  225. if (!pathv) {
  226. __glob_freelist(&head);
  227. return GLOB_NOSPACE;
  228. }
  229. g->gl_pathv = pathv;
  230. offs += g->gl_pathc;
  231. } else {
  232. g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
  233. if (!g->gl_pathv) {
  234. __glob_freelist(&head);
  235. return GLOB_NOSPACE;
  236. }
  237. for (i=0; i<offs; i++)
  238. g->gl_pathv[i] = NULL;
  239. }
  240. for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
  241. g->gl_pathv[offs + i] = tail->name;
  242. g->gl_pathv[offs + i] = NULL;
  243. g->gl_pathc += cnt;
  244. if (!(flags & GLOB_NOSORT))
  245. qsort(g->gl_pathv+offs, cnt, sizeof(char *), __glob_sort);
  246. return error;
  247. }
  248. #ifdef __GLOB64
  249. libc_hidden_def(glob64)
  250. #else
  251. libc_hidden_def(glob)
  252. #endif
  253. void globfree(glob_t *g)
  254. {
  255. size_t i;
  256. for (i=0; i<g->gl_pathc; i++)
  257. free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
  258. free(g->gl_pathv);
  259. g->gl_pathc = 0;
  260. g->gl_pathv = NULL;
  261. }
  262. #ifdef __GLOB64
  263. libc_hidden_def(globfree64)
  264. #else
  265. libc_hidden_def(globfree)
  266. #endif