Start 1.33.0 development cycle
[oweals/busybox.git] / archival / libarchive / find_list_entry.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Copyright (C) 2002 by Glenn McGrath
4  *
5  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
6  */
7 #include <fnmatch.h>
8 #include "libbb.h"
9 #include "bb_archive.h"
10
11 /* Find a string in a shell pattern list */
12 const llist_t* FAST_FUNC find_list_entry(const llist_t *list, const char *filename)
13 {
14         while (list) {
15                 if (fnmatch(list->data, filename, 0) == 0) {
16                         return list;
17                 }
18                 list = list->link;
19         }
20         return NULL;
21 }
22
23 /* Same, but compares only path components present in pattern
24  * (extra trailing path components in filename are assumed to match)
25  */
26 const llist_t* FAST_FUNC find_list_entry2(const llist_t *list, const char *filename)
27 {
28         char buf[PATH_MAX];
29         int pattern_slash_cnt;
30         const char *c;
31         char *d;
32
33         while (list) {
34                 c = list->data;
35                 pattern_slash_cnt = 0;
36                 while (*c)
37                         if (*c++ == '/') pattern_slash_cnt++;
38                 c = filename;
39                 d = buf;
40                 /* paranoia is better than buffer overflows */
41                 while (*c && d != buf + sizeof(buf)-1) {
42                         if (*c == '/' && --pattern_slash_cnt < 0)
43                                 break;
44                         *d++ = *c++;
45                 }
46                 *d = '\0';
47                 if (fnmatch(list->data, buf, 0) == 0) {
48                         return list;
49                 }
50                 list = list->link;
51         }
52         return NULL;
53 }