File: match.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,368 kB
  • sloc: ansic: 15,820; perl: 674; sh: 64; makefile: 26
file content (240 lines) | stat: -rw-r--r-- 6,200 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* path/match.c - Filename matching utility.
 * Copyright (C) 2001,2005  Bruce Guenter <bruce@untroubled.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */
#include <errno.h>
#include <string.h>
#include <sys/stat.h>

#include "sysdeps.h"
#include "str.h"
#include "striter.h"
#include "path.h"
#include "msg.h"

static str tmplist;
static str tmpitem;

static int exists(const char* path)
{
  struct stat st;
  if (stat(path, &st) == 0) return 1;
  if (errno == ENOENT || errno == EACCES || errno == ENOTDIR) return 0;
  return -1;
}

static int match_first(int absolute, const str* part, str* result,
		       unsigned options)
{
  DIR* dir;
  direntry* entry;
  int count = 0;
  if (!str_truncate(result, 0)) return -1;
  if (has_magic(part->s)) {
    if ((dir = opendir(absolute ? "/" : ".")) == 0) return -1;
    while ((entry = readdir(dir)) != 0) {
      if (fnmatch(entry->d_name, part->s, options)) {
	if (absolute) if (!str_catc(result, '/')) return -1;
	if (!str_catb(result, entry->d_name, strlen(entry->d_name)+1)) {
	  closedir(dir);
	  return -1;
	}
	++count;
      }
    }
    closedir(dir);
    return count;
  }
  else {
    switch (exists(part->s)) {
    case 1: str_copyb(result, part->s, part->len + 1); return 1;
    case 0: return 0;
    default: return -1;
    }
  }
}

static int match_next_magic(const str* part, str* result, unsigned options)
{
  DIR* dir;
  direntry* entry;
  striter path;
  int count;
  count = 0;
  if (!str_copy(&tmplist, result)) return -1;
  if (!str_truncate(result, 0)) return -1;
  striter_loop(&path, &tmplist, 0) {
    if ((dir = opendir(path.startptr)) == 0) continue;
    while ((entry = readdir(dir)) != 0) {
      if (fnmatch(entry->d_name, part->s, options)) {
	if (!str_cats(result, path.startptr) ||
	    !str_catc(result, '/') ||
	    !str_catb(result, entry->d_name, strlen(entry->d_name)+1)) {
	  closedir(dir);
	  return -1;
	}
	++count;
      }
    }
    closedir(dir);
  }
  return count;
}

static int match_next_nomagic(const str* part, str* result, unsigned options)
{
  striter path;
  int count = 0;
  if (!fnmatch(part->s, part->s, options)) return 0;
  if (!str_copy(&tmplist, result)) return -1;
  if (!str_truncate(result, 0)) return -1;
  striter_loop(&path, &tmplist, 0) {
    if (!str_copyiter(&tmpitem, &path) ||
	!str_catc(&tmpitem, '/') ||
	!str_cat(&tmpitem, part)) return -1;
    switch (exists(tmpitem.s)) {
    case 1: str_catb(result, tmpitem.s, tmpitem.len+1); ++count; continue;
    case 0: continue;
    default: return -1;
    }
  }
  return count;
}

static int match_next(const str* part, str* result, unsigned options)
{
  return has_magic(part->s) ?
    match_next_magic(part, result, options) :
    match_next_nomagic(part, result, options);
}

/** Matches the pattern against existing files.

This function produces a list of existing files in \c result (in the
current directory) that match the given pattern.  If \c options has \c
PATH_MATCH_DOTFILES set, the result may contain paths starting with ".".
*/
int path_match(const char* pattern, str* result, unsigned options)
{
  /* FIXME: make this stack allocated */
  static str part;
  
  int count;
  const char* patend;
  int absolute;
  const char* partend;
  
  absolute = pattern[0] == '/';
  patend = pattern + strlen(pattern);
  /* Note and skip any leading slashes */
  while (pattern < patend && *pattern == '/') ++pattern;
  if ((partend = strchr(pattern, '/')) == 0) partend = patend;

  if (!str_copyb(&part, pattern, partend-pattern)) return -1;
  if ((count = match_first(absolute, &part, result, options)) == -1) return -1;
  
  while (result->len > 0) {
    pattern = partend + 1;
    while (pattern < patend && *pattern == '/') ++pattern;
    if (pattern > patend) break;
    if ((partend = strchr(pattern, '/')) == 0) partend = patend;
    if (!str_copyb(&part, pattern, partend-pattern)) return -1;
    if ((count = match_next(&part, result, options)) == -1) return -1;
  }
  if (count == 0 && exists(pattern)) {
    if (!str_copys(result, pattern)) return -1;
    return 1;
  }
  if (!str_sort(result, 0, count, 0)) return -1;
  str_free(&tmplist);
  return count;
}

#ifdef SELFTEST_MAIN
#include <unistd.h>
static void match(const char* pattern, unsigned options)
{
  static str s;
  striter i;
  obuf_puts(&outbuf, pattern);
  obuf_putc(&outbuf, ' ');
  obuf_putu(&outbuf, options);
  obuf_puts(&outbuf, " => ");
  obuf_puti(&outbuf, path_match(pattern, &s, options));
  NL();
  for (striter_start(&i, &s, 0);
       striter_valid(&i);
       striter_advance(&i)) {
    obuf_putiter(&outbuf, &i);
    NL();
  }
}
MAIN
{
  match("*", 0);
  match("*.o", 0);
  match("*.o*", 0);
  match("*.[eo]", 0);
  match("t*", 0);
  match("*.o[u]*", 0);

  mkdir("[test]", 0777);
  close(open("[test]/.file", O_WRONLY | O_CREAT | O_TRUNC, 0666));
  close(open("[test]/[f2]", O_WRONLY | O_CREAT | O_TRUNC, 0666));
  match("[test]", 0);
  match("[test]/*", PATH_MATCH_DOTFILES);
  match("[test]/.file", 0);
  match("[test]/[f2]", 0);
  match("*/*", 0);
  match("*/*", PATH_MATCH_DOTFILES);
  match("*/[f2]", 0);
  unlink("[test]/.file");
  unlink("[test]/[f2]");
  rmdir("[test]");
}
#endif
#ifdef SELFTEST_EXP
* 0 => 3
test
test.o
test.out
*.o 0 => 1
test.o
*.o* 0 => 2
test.o
test.out
*.[eo] 0 => 1
test.o
t* 0 => 3
test
test.o
test.out
*.o[u]* 0 => 1
test.out
[test] 0 => 1
[test]
[test]/* 1 => 0
[test]/.file 0 => 1
[test]/.file
[test]/[f2] 0 => 1
[test]/[f2]
*/* 0 => 1
[test]/[f2]
*/* 1 => 2
[test]/.file
[test]/[f2]
*/[f2] 0 => 0
#endif