glob-susv3.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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, int err)
  172. {
  173. return 0;
  174. }
  175. # ifndef BUILD_GLOB64
  176. static
  177. # endif
  178. void __glob_freelist(struct match *head)
  179. {
  180. struct match *match, *next;
  181. for (match=head->next; match; match=next) {
  182. next = match->next;
  183. free(match);
  184. }
  185. }
  186. # ifndef BUILD_GLOB64
  187. static
  188. # endif
  189. int __glob_sort(const void *a, const void *b)
  190. {
  191. return strcmp(*(const char **)a, *(const char **)b);
  192. }
  193. #endif /* !__GLOB64 */
  194. int glob(const char *pat, int flags, int (*errfunc)(const char *path, int err), glob_t *g)
  195. {
  196. const char *p=pat, *d;
  197. struct match head = { .next = NULL }, *tail = &head;
  198. size_t cnt, i;
  199. size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
  200. int error = 0;
  201. if (*p == '/') {
  202. for (; *p == '/'; p++);
  203. d = "/";
  204. } else {
  205. d = "";
  206. }
  207. if (!errfunc) errfunc = __glob_ignore_err;
  208. if (!(flags & GLOB_APPEND)) {
  209. g->gl_offs = offs;
  210. g->gl_pathc = 0;
  211. g->gl_pathv = NULL;
  212. }
  213. if (*p) error = __glob_match_in_dir(d, p, flags, errfunc, &tail);
  214. if (error == GLOB_NOSPACE) {
  215. __glob_freelist(&head);
  216. return error;
  217. }
  218. for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
  219. if (!cnt) {
  220. if (flags & GLOB_NOCHECK) {
  221. tail = &head;
  222. if (__glob_append(&tail, pat, strlen(pat), 0))
  223. return GLOB_NOSPACE;
  224. cnt++;
  225. } else
  226. return GLOB_NOMATCH;
  227. }
  228. if (flags & GLOB_APPEND) {
  229. char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
  230. if (!pathv) {
  231. __glob_freelist(&head);
  232. return GLOB_NOSPACE;
  233. }
  234. g->gl_pathv = pathv;
  235. offs += g->gl_pathc;
  236. } else {
  237. g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
  238. if (!g->gl_pathv) {
  239. __glob_freelist(&head);
  240. return GLOB_NOSPACE;
  241. }
  242. for (i=0; i<offs; i++)
  243. g->gl_pathv[i] = NULL;
  244. }
  245. for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
  246. g->gl_pathv[offs + i] = tail->name;
  247. g->gl_pathv[offs + i] = NULL;
  248. g->gl_pathc += cnt;
  249. if (!(flags & GLOB_NOSORT))
  250. qsort(g->gl_pathv+offs, cnt, sizeof(char *), __glob_sort);
  251. return error;
  252. }
  253. #ifdef __GLOB64
  254. libc_hidden_def(glob64)
  255. #else
  256. libc_hidden_def(glob)
  257. #endif
  258. void globfree(glob_t *g)
  259. {
  260. size_t i;
  261. for (i=0; i<g->gl_pathc; i++)
  262. free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
  263. free(g->gl_pathv);
  264. g->gl_pathc = 0;
  265. g->gl_pathv = NULL;
  266. }
  267. #ifdef __GLOB64
  268. libc_hidden_def(globfree64)
  269. #else
  270. libc_hidden_def(globfree)
  271. #endif