summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authorknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-27 17:45:19 +0000
committerknu <knu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2001-05-27 17:45:19 +0000
commit6b0bfa981a72c8bc8e7ecd6626876d04ed5d88fb (patch)
treedd4134c6b3cf97f8e2dcd4b363caf4ee04052595 /dir.c
parent6f0e35087275bd8ff2f117ed5183ac73a98593f5 (diff)
* dir.c (rb_glob_helper): teach has_magic() to handle flags and
get rb_glob_helper to properly support FNM_NOESCAPE. * dir.c (fnmatch): fix a bug when FNM_PATHNAME and FNM_PERIOD are specified at the same time. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_6@1469 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/dir.c b/dir.c
index e8f1fdcc95..187ae1896e 100644
--- a/dir.c
+++ b/dir.c
@@ -130,8 +130,9 @@ range(pat, test, flags)
return 0;
}
+#define ISDIRSEP(c) (pathname && isdirsep(c))
#define PERIOD(s) (period && *(s) == '.' && \
- ((s) == string || pathname && isdirsep(*(s))))
+ ((s) == string || ISDIRSEP((s)[-1])))
static int
fnmatch(pat, string, flags)
const char *pat;
@@ -149,7 +150,7 @@ fnmatch(pat, string, flags)
while (c = *pat++) {
switch (c) {
case '?':
- if (!*s || pathname && isdirsep(*s) || PERIOD(s))
+ if (!*s || ISDIRSEP(*s) || PERIOD(s))
return FNM_NOMATCH;
s++;
break;
@@ -166,7 +167,7 @@ fnmatch(pat, string, flags)
else
return 0;
}
- else if (pathname && isdirsep(c)) {
+ else if (ISDIRSEP(c)) {
s = find_dirsep(s);
if (s)
break;
@@ -180,14 +181,14 @@ fnmatch(pat, string, flags)
if ((c == '[' || downcase(*s) == test) &&
!fnmatch(pat, s, flags & ~FNM_PERIOD))
return 0;
- else if (pathname && isdirsep(*s))
+ else if (ISDIRSEP(*s))
break;
s++;
}
return FNM_NOMATCH;
case '[':
- if (!*s || pathname && isdirsep(*s) || PERIOD(s))
+ if (!*s || ISDIRSEP(*s) || PERIOD(s))
return FNM_NOMATCH;
pat = range(pat, *s, flags);
if (!pat)
@@ -211,7 +212,7 @@ fnmatch(pat, string, flags)
default:
#if defined DOSISH
- if (pathname && isdirsep(c) && isdirsep(*s))
+ if (ISDIRSEP(c) && isdirsep(*s))
;
else
#endif
@@ -494,12 +495,14 @@ dir_s_rmdir(obj, dir)
/* Return nonzero if S has any special globbing chars in it. */
static int
-has_magic(s, send)
+has_magic(s, send, flags)
char *s, *send;
+ int flags;
{
register char *p = s;
register char c;
int open = 0;
+ int escape = !(flags & FNM_NOESCAPE);
while ((c = *p++) != '\0') {
switch (c) {
@@ -516,7 +519,7 @@ has_magic(s, send)
continue;
case '\\':
- if (*p++ == '\0')
+ if (escape && *p++ == '\0')
return Qfalse;
}
@@ -566,16 +569,16 @@ extract_elem(path)
#endif
void
-rb_glob_helper(path, flag, func, arg)
+rb_glob_helper(path, flags, func, arg)
char *path;
- int flag;
+ int flags;
void (*func)();
VALUE arg;
{
struct stat st;
char *p, *m;
- if (!has_magic(path, 0)) {
+ if (!has_magic(path, 0, flags)) {
if (rb_sys_stat(path, &st) == 0) {
(*func)(path, arg);
}
@@ -586,7 +589,7 @@ rb_glob_helper(path, flag, func, arg)
while (p) {
if (*p == '/') p++;
m = strchr(p, '/');
- if (has_magic(p, m)) {
+ if (has_magic(p, m, flags)) {
char *dir, *base, *magic, *buf;
DIR *dirp;
struct dirent *dp;
@@ -606,7 +609,7 @@ rb_glob_helper(path, flag, func, arg)
recursive = 1;
buf = ALLOC_N(char, strlen(base)+strlen(m)+3);
sprintf(buf, "%s%s%s", base, (*base)?"":".", m);
- rb_glob_helper(buf, flag, func, arg);
+ rb_glob_helper(buf, flags, func, arg);
free(buf);
}
if (rb_sys_stat(dir, &st) < 0) {
@@ -644,12 +647,12 @@ rb_glob_helper(path, flag, func, arg)
if (S_ISDIR(st.st_mode)) {
strcat(buf, "/**");
strcat(buf, m);
- rb_glob_helper(buf, flag, func, arg);
+ rb_glob_helper(buf, flags, func, arg);
}
free(buf);
continue;
}
- if (fnmatch(magic, dp->d_name, flag) == 0) {
+ if (fnmatch(magic, dp->d_name, flags) == 0) {
buf = ALLOC_N(char, strlen(base)+NAMLEN(dp)+2);
sprintf(buf, "%s%s%s", base, (BASE)?"/":"", dp->d_name);
if (!m) {
@@ -674,7 +677,7 @@ rb_glob_helper(path, flag, func, arg)
char *t = ALLOC_N(char, len+mlen+1);
sprintf(t, "%s%s", link->path, m);
- rb_glob_helper(t, flag, func, arg);
+ rb_glob_helper(t, flags, func, arg);
free(t);
}
tmp = link;
@@ -788,7 +791,7 @@ dir_s_glob(dir, str)
VALUE dir, str;
{
char *p, *pend;
- char buffer[MAXPATHLEN], *buf = buffer;
+ char buffer[MAXPATHLEN], *buf;
char *t;
int nest;
VALUE ary = 0;
@@ -799,6 +802,8 @@ dir_s_glob(dir, str)
}
if (RSTRING(str)->len >= MAXPATHLEN)
buf = xmalloc(RSTRING(str)->len + 1);
+ else
+ buf = buffer;
p = RSTRING(str)->ptr;
pend = p + RSTRING(str)->len;