glob-susv3.c 6.5 KB

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