mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* dir.c (glob_helper): teach has_magic() to handle flags and get
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/trunk@1468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
c764579e49
commit
4407373ef1
2 changed files with 30 additions and 17 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Mon May 28 02:20:38 2001 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* dir.c (glob_helper): teach has_magic() to handle flags and get
|
||||||
|
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.
|
||||||
|
|
||||||
Sat May 26 07:05:45 2001 Usaku Nakamura <usa@osb.att.ne.jp>
|
Sat May 26 07:05:45 2001 Usaku Nakamura <usa@osb.att.ne.jp>
|
||||||
|
|
||||||
* MANIFEST: add win32/dir.h .
|
* MANIFEST: add win32/dir.h .
|
||||||
|
|
39
dir.c
39
dir.c
|
@ -130,8 +130,9 @@ range(pat, test, flags)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ISDIRSEP(c) (pathname && isdirsep(c))
|
||||||
#define PERIOD(s) (period && *(s) == '.' && \
|
#define PERIOD(s) (period && *(s) == '.' && \
|
||||||
((s) == string || pathname && isdirsep(*(s))))
|
((s) == string || ISDIRSEP((s)[-1])))
|
||||||
static int
|
static int
|
||||||
fnmatch(pat, string, flags)
|
fnmatch(pat, string, flags)
|
||||||
const char *pat;
|
const char *pat;
|
||||||
|
@ -149,7 +150,7 @@ fnmatch(pat, string, flags)
|
||||||
while (c = *pat++) {
|
while (c = *pat++) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '?':
|
case '?':
|
||||||
if (!*s || pathname && isdirsep(*s) || PERIOD(s))
|
if (!*s || ISDIRSEP(*s) || PERIOD(s))
|
||||||
return FNM_NOMATCH;
|
return FNM_NOMATCH;
|
||||||
s++;
|
s++;
|
||||||
break;
|
break;
|
||||||
|
@ -166,7 +167,7 @@ fnmatch(pat, string, flags)
|
||||||
else
|
else
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else if (pathname && isdirsep(c)) {
|
else if (ISDIRSEP(c)) {
|
||||||
s = find_dirsep(s);
|
s = find_dirsep(s);
|
||||||
if (s)
|
if (s)
|
||||||
break;
|
break;
|
||||||
|
@ -180,14 +181,14 @@ fnmatch(pat, string, flags)
|
||||||
if ((c == '[' || downcase(*s) == test) &&
|
if ((c == '[' || downcase(*s) == test) &&
|
||||||
!fnmatch(pat, s, flags & ~FNM_PERIOD))
|
!fnmatch(pat, s, flags & ~FNM_PERIOD))
|
||||||
return 0;
|
return 0;
|
||||||
else if (pathname && isdirsep(*s))
|
else if (ISDIRSEP(*s))
|
||||||
break;
|
break;
|
||||||
s++;
|
s++;
|
||||||
}
|
}
|
||||||
return FNM_NOMATCH;
|
return FNM_NOMATCH;
|
||||||
|
|
||||||
case '[':
|
case '[':
|
||||||
if (!*s || pathname && isdirsep(*s) || PERIOD(s))
|
if (!*s || ISDIRSEP(*s) || PERIOD(s))
|
||||||
return FNM_NOMATCH;
|
return FNM_NOMATCH;
|
||||||
pat = range(pat, *s, flags);
|
pat = range(pat, *s, flags);
|
||||||
if (!pat)
|
if (!pat)
|
||||||
|
@ -211,7 +212,7 @@ fnmatch(pat, string, flags)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
#if defined DOSISH
|
#if defined DOSISH
|
||||||
if (pathname && isdirsep(c) && isdirsep(*s))
|
if (ISDIRSEP(c) && isdirsep(*s))
|
||||||
;
|
;
|
||||||
else
|
else
|
||||||
#endif
|
#endif
|
||||||
|
@ -522,12 +523,14 @@ dir_s_rmdir(obj, dir)
|
||||||
|
|
||||||
/* Return nonzero if S has any special globbing chars in it. */
|
/* Return nonzero if S has any special globbing chars in it. */
|
||||||
static int
|
static int
|
||||||
has_magic(s, send)
|
has_magic(s, send, flags)
|
||||||
char *s, *send;
|
char *s, *send;
|
||||||
|
int flags;
|
||||||
{
|
{
|
||||||
register char *p = s;
|
register char *p = s;
|
||||||
register char c;
|
register char c;
|
||||||
int open = 0;
|
int open = 0;
|
||||||
|
int escape = !(flags & FNM_NOESCAPE);
|
||||||
|
|
||||||
while ((c = *p++) != '\0') {
|
while ((c = *p++) != '\0') {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
|
@ -544,7 +547,7 @@ has_magic(s, send)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case '\\':
|
case '\\':
|
||||||
if (*p++ == '\0')
|
if (escape && *p++ == '\0')
|
||||||
return Qfalse;
|
return Qfalse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -610,16 +613,16 @@ remove_backslashes(p)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static void
|
static void
|
||||||
glob_helper(path, flag, func, arg)
|
glob_helper(path, flags, func, arg)
|
||||||
char *path;
|
char *path;
|
||||||
int flag;
|
int flags;
|
||||||
void (*func)();
|
void (*func)();
|
||||||
VALUE arg;
|
VALUE arg;
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
char *p, *m;
|
char *p, *m;
|
||||||
|
|
||||||
if (!has_magic(path, 0)) {
|
if (!has_magic(path, 0, flags)) {
|
||||||
remove_backslashes(path);
|
remove_backslashes(path);
|
||||||
if (stat(path, &st) == 0) {
|
if (stat(path, &st) == 0) {
|
||||||
(*func)(path, arg);
|
(*func)(path, arg);
|
||||||
|
@ -636,7 +639,7 @@ glob_helper(path, flag, func, arg)
|
||||||
while (p) {
|
while (p) {
|
||||||
if (*p == '/') p++;
|
if (*p == '/') p++;
|
||||||
m = strchr(p, '/');
|
m = strchr(p, '/');
|
||||||
if (has_magic(p, m)) {
|
if (has_magic(p, m, flags)) {
|
||||||
char *dir, *base, *magic, *buf;
|
char *dir, *base, *magic, *buf;
|
||||||
DIR *dirp;
|
DIR *dirp;
|
||||||
struct dirent *dp;
|
struct dirent *dp;
|
||||||
|
@ -662,7 +665,7 @@ glob_helper(path, flag, func, arg)
|
||||||
recursive = 1;
|
recursive = 1;
|
||||||
buf = ALLOC_N(char, strlen(base)+strlen(m)+3);
|
buf = ALLOC_N(char, strlen(base)+strlen(m)+3);
|
||||||
sprintf(buf, "%s%s", base, *base ? m : m+1);
|
sprintf(buf, "%s%s", base, *base ? m : m+1);
|
||||||
glob_helper(buf, flag, func, arg);
|
glob_helper(buf, flags, func, arg);
|
||||||
free(buf);
|
free(buf);
|
||||||
}
|
}
|
||||||
dirp = opendir(dir);
|
dirp = opendir(dir);
|
||||||
|
@ -696,12 +699,12 @@ glob_helper(path, flag, func, arg)
|
||||||
if (S_ISDIR(st.st_mode)) {
|
if (S_ISDIR(st.st_mode)) {
|
||||||
strcat(buf, "/**");
|
strcat(buf, "/**");
|
||||||
strcat(buf, m);
|
strcat(buf, m);
|
||||||
glob_helper(buf, flag, func, arg);
|
glob_helper(buf, flags, func, arg);
|
||||||
}
|
}
|
||||||
free(buf);
|
free(buf);
|
||||||
continue;
|
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);
|
buf = ALLOC_N(char, strlen(base)+NAMLEN(dp)+2);
|
||||||
sprintf(buf, "%s%s%s", base, (BASE)?"/":"", dp->d_name);
|
sprintf(buf, "%s%s%s", base, (BASE)?"/":"", dp->d_name);
|
||||||
if (!m) {
|
if (!m) {
|
||||||
|
@ -727,7 +730,7 @@ glob_helper(path, flag, func, arg)
|
||||||
char *t = ALLOC_N(char, len+mlen+1);
|
char *t = ALLOC_N(char, len+mlen+1);
|
||||||
|
|
||||||
sprintf(t, "%s%s", link->path, m);
|
sprintf(t, "%s%s", link->path, m);
|
||||||
glob_helper(t, flag, func, arg);
|
glob_helper(t, flags, func, arg);
|
||||||
free(t);
|
free(t);
|
||||||
}
|
}
|
||||||
tmp = link;
|
tmp = link;
|
||||||
|
@ -849,7 +852,7 @@ dir_s_glob(dir, str)
|
||||||
VALUE dir, str;
|
VALUE dir, str;
|
||||||
{
|
{
|
||||||
char *p, *pend;
|
char *p, *pend;
|
||||||
char buffer[MAXPATHLEN], *buf = buffer;
|
char buffer[MAXPATHLEN], *buf;
|
||||||
char *t;
|
char *t;
|
||||||
int nest;
|
int nest;
|
||||||
VALUE ary = 0;
|
VALUE ary = 0;
|
||||||
|
@ -860,6 +863,8 @@ dir_s_glob(dir, str)
|
||||||
}
|
}
|
||||||
if (RSTRING(str)->len >= MAXPATHLEN) {
|
if (RSTRING(str)->len >= MAXPATHLEN) {
|
||||||
buf = xmalloc(RSTRING(str)->len + 1);
|
buf = xmalloc(RSTRING(str)->len + 1);
|
||||||
|
} else {
|
||||||
|
buf = buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = RSTRING(str)->ptr;
|
p = RSTRING(str)->ptr;
|
||||||
|
|
Loading…
Add table
Reference in a new issue