2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
dir.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Wed Jan 5 09:51:01 JST 1994
|
|
|
|
|
2003-01-16 10:34:03 +03:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
2000-05-01 13:42:38 +04:00
|
|
|
Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
|
2000-05-09 08:53:16 +04:00
|
|
|
Copyright (C) 2000 Information-technology Promotion Agency, Japan
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1998-01-16 15:19:22 +03:00
|
|
|
#include <sys/stat.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#if defined HAVE_DIRENT_H && !defined _WIN32
|
1998-01-16 15:13:05 +03:00
|
|
|
# include <dirent.h>
|
|
|
|
# define NAMLEN(dirent) strlen((dirent)->d_name)
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#elif defined HAVE_DIRECT_H && !defined _WIN32
|
1999-01-20 07:59:39 +03:00
|
|
|
# include <direct.h>
|
|
|
|
# define NAMLEN(dirent) strlen((dirent)->d_name)
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
# define dirent direct
|
|
|
|
# define NAMLEN(dirent) (dirent)->d_namlen
|
|
|
|
# if HAVE_SYS_NDIR_H
|
|
|
|
# include <sys/ndir.h>
|
|
|
|
# endif
|
|
|
|
# if HAVE_SYS_DIR_H
|
|
|
|
# include <sys/dir.h>
|
|
|
|
# endif
|
|
|
|
# if HAVE_NDIR_H
|
|
|
|
# include <ndir.h>
|
|
|
|
# endif
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
# ifdef _WIN32
|
2001-03-19 06:20:24 +03:00
|
|
|
# include "win32/dir.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifndef HAVE_STDLIB_H
|
1998-01-16 15:13:05 +03:00
|
|
|
char *getenv();
|
|
|
|
#endif
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifndef HAVE_STRING_H
|
|
|
|
char *strchr _((char*,char));
|
1999-01-20 07:59:39 +03:00
|
|
|
#endif
|
|
|
|
|
1999-10-21 11:52:15 +04:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2001-11-08 09:43:14 +03:00
|
|
|
#include "util.h"
|
|
|
|
|
2001-02-13 17:15:55 +03:00
|
|
|
#ifndef HAVE_LSTAT
|
2001-03-21 06:51:23 +03:00
|
|
|
#define lstat(path,st) stat(path,st)
|
2001-02-13 17:15:55 +03:00
|
|
|
#endif
|
|
|
|
|
1999-10-21 11:52:15 +04:00
|
|
|
#define FNM_NOESCAPE 0x01
|
|
|
|
#define FNM_PATHNAME 0x02
|
2002-03-13 13:11:09 +03:00
|
|
|
#define FNM_DOTMATCH 0x04
|
2001-06-13 14:51:39 +04:00
|
|
|
#define FNM_CASEFOLD 0x08
|
1999-10-21 11:52:15 +04:00
|
|
|
|
|
|
|
#define FNM_NOMATCH 1
|
|
|
|
#define FNM_ERROR 2
|
|
|
|
|
2001-02-08 12:19:27 +03:00
|
|
|
#define downcase(c) (nocase && ISUPPER(c) ? tolower(c) : (c))
|
1999-10-21 11:52:15 +04:00
|
|
|
|
2001-09-08 18:17:53 +04:00
|
|
|
#ifndef CharNext /* defined as CharNext[AW] on Windows. */
|
|
|
|
# if defined(DJGPP)
|
|
|
|
# define CharNext(p) ((p) + mblen(p, MB_CUR_MAX))
|
|
|
|
# else
|
|
|
|
# define CharNext(p) ((p) + 1)
|
|
|
|
# endif
|
|
|
|
#endif
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
|
1999-10-21 11:52:15 +04:00
|
|
|
#if defined DOSISH
|
|
|
|
#define isdirsep(c) ((c) == '/' || (c) == '\\')
|
|
|
|
static char *
|
|
|
|
find_dirsep(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
while (*s) {
|
|
|
|
if (isdirsep(*s))
|
|
|
|
return s;
|
2001-09-08 18:17:53 +04:00
|
|
|
s = CharNext(s);
|
1999-10-21 11:52:15 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define isdirsep(c) ((c) == '/')
|
|
|
|
#define find_dirsep(s) strchr(s, '/')
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static char *
|
|
|
|
range(pat, test, flags)
|
|
|
|
char *pat;
|
|
|
|
char test;
|
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
int not, ok = 0;
|
2001-06-13 14:51:39 +04:00
|
|
|
int nocase = flags & FNM_CASEFOLD;
|
1999-10-21 11:52:15 +04:00
|
|
|
int escape = !(flags & FNM_NOESCAPE);
|
|
|
|
|
|
|
|
not = *pat == '!' || *pat == '^';
|
|
|
|
if (not)
|
|
|
|
pat++;
|
|
|
|
|
|
|
|
test = downcase(test);
|
|
|
|
|
|
|
|
while (*pat) {
|
|
|
|
int cstart, cend;
|
|
|
|
cstart = cend = *pat++;
|
|
|
|
if (cstart == ']')
|
|
|
|
return ok == not ? 0 : pat;
|
|
|
|
else if (escape && cstart == '\\')
|
|
|
|
cstart = cend = *pat++;
|
|
|
|
if (*pat == '-' && pat[1] != ']') {
|
|
|
|
if (escape && pat[1] == '\\')
|
|
|
|
pat++;
|
|
|
|
cend = pat[1];
|
|
|
|
if (!cend)
|
|
|
|
return 0;
|
|
|
|
pat += 2;
|
|
|
|
}
|
|
|
|
if (downcase(cstart) <= test && test <= downcase(cend))
|
|
|
|
ok = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-05-27 21:24:24 +04:00
|
|
|
#define ISDIRSEP(c) (pathname && isdirsep(c))
|
1999-10-21 11:52:15 +04:00
|
|
|
#define PERIOD(s) (period && *(s) == '.' && \
|
2002-05-04 04:06:19 +04:00
|
|
|
((s) == string || ISDIRSEP((s)[-1])))
|
1999-10-21 11:52:15 +04:00
|
|
|
static int
|
|
|
|
fnmatch(pat, string, flags)
|
2001-05-11 09:24:59 +04:00
|
|
|
const char *pat;
|
|
|
|
const char *string;
|
1999-10-21 11:52:15 +04:00
|
|
|
int flags;
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
int test;
|
2001-05-11 09:24:59 +04:00
|
|
|
const char *s = string;
|
1999-10-21 11:52:15 +04:00
|
|
|
int escape = !(flags & FNM_NOESCAPE);
|
|
|
|
int pathname = flags & FNM_PATHNAME;
|
2002-03-13 13:11:09 +03:00
|
|
|
int period = !(flags & FNM_DOTMATCH);
|
2001-06-13 14:51:39 +04:00
|
|
|
int nocase = flags & FNM_CASEFOLD;
|
1999-10-21 11:52:15 +04:00
|
|
|
|
|
|
|
while (c = *pat++) {
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
2001-05-27 21:24:24 +04:00
|
|
|
if (!*s || ISDIRSEP(*s) || PERIOD(s))
|
1999-10-21 11:52:15 +04:00
|
|
|
return FNM_NOMATCH;
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
while ((c = *pat++) == '*')
|
|
|
|
;
|
|
|
|
|
|
|
|
if (PERIOD(s))
|
|
|
|
return FNM_NOMATCH;
|
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
if (pathname && find_dirsep(s))
|
|
|
|
return FNM_NOMATCH;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
2001-05-27 21:24:24 +04:00
|
|
|
else if (ISDIRSEP(c)) {
|
1999-10-21 11:52:15 +04:00
|
|
|
s = find_dirsep(s);
|
2002-03-25 09:18:07 +03:00
|
|
|
if (s) {
|
|
|
|
s++;
|
1999-10-21 11:52:15 +04:00
|
|
|
break;
|
2002-03-25 09:18:07 +03:00
|
|
|
}
|
1999-10-21 11:52:15 +04:00
|
|
|
return FNM_NOMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
test = escape && c == '\\' ? *pat : c;
|
|
|
|
test = downcase(test);
|
|
|
|
pat--;
|
|
|
|
while (*s) {
|
|
|
|
if ((c == '[' || downcase(*s) == test) &&
|
2002-03-13 13:11:09 +03:00
|
|
|
!fnmatch(pat, s, flags | FNM_DOTMATCH))
|
1999-10-21 11:52:15 +04:00
|
|
|
return 0;
|
2001-05-27 21:24:24 +04:00
|
|
|
else if (ISDIRSEP(*s))
|
1999-10-21 11:52:15 +04:00
|
|
|
break;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return FNM_NOMATCH;
|
|
|
|
|
|
|
|
case '[':
|
2001-05-27 21:24:24 +04:00
|
|
|
if (!*s || ISDIRSEP(*s) || PERIOD(s))
|
1999-10-21 11:52:15 +04:00
|
|
|
return FNM_NOMATCH;
|
|
|
|
pat = range(pat, *s, flags);
|
|
|
|
if (!pat)
|
|
|
|
return FNM_NOMATCH;
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\\':
|
|
|
|
if (escape
|
|
|
|
#if defined DOSISH
|
|
|
|
&& *pat && strchr("*?[\\", *pat)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
c = *pat;
|
|
|
|
if (!c)
|
|
|
|
c = '\\';
|
|
|
|
else
|
|
|
|
pat++;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
#if defined DOSISH
|
2001-05-27 21:24:24 +04:00
|
|
|
if (ISDIRSEP(c) && isdirsep(*s))
|
1999-10-21 11:52:15 +04:00
|
|
|
;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if(downcase(c) != downcase(*s))
|
|
|
|
return FNM_NOMATCH;
|
|
|
|
s++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !*s ? 0 : FNM_NOMATCH;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE rb_cDir;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data {
|
|
|
|
DIR *dir;
|
|
|
|
char *path;
|
|
|
|
};
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
|
|
|
free_dir(dir)
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dir;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
if (dir && dir->dir) closedir(dir->dir);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE dir_close _((VALUE));
|
|
|
|
|
* array.c (ary_alloc), dir.c (dir_s_alloc), eval.c (thgroup_s_alloc),
file.c (rb_stat_s_alloc), hash.c (hash_alloc), io.c (io_alloc),
object.c (rb_module_s_alloc, rb_class_allocate_instance),
re.c (match_alloc, rb_reg_s_alloc), string.c (str_alloc),
time.c (time_s_alloc), ext/digest/digest.c (rb_digest_base_alloc),
ext/tcltklib/tcltklib.c (ip_alloc),
ext/win32ole/win32ole.c (fole_s_allocate, fev_s_allocate)
: add prototype to get rid of VC++ warnings.
* ext/sdbm/init.c (fsdbm_alloc): allocator takes only one argument.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-21 21:02:01 +03:00
|
|
|
static VALUE dir_s_alloc _((VALUE));
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2001-10-03 11:19:19 +04:00
|
|
|
dir_s_alloc(klass)
|
2000-02-29 11:05:32 +03:00
|
|
|
VALUE klass;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
|
|
|
VALUE obj = Data_Make_Struct(klass, struct dir_data, 0, free_dir, dirp);
|
2000-02-29 11:05:32 +03:00
|
|
|
|
2001-09-19 10:54:11 +04:00
|
|
|
dirp->dir = NULL;
|
|
|
|
dirp->path = NULL;
|
2000-02-29 11:05:32 +03:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_initialize(dir, dirname)
|
|
|
|
VALUE dir, dirname;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(dirname);
|
2001-09-19 10:54:11 +04:00
|
|
|
Data_Get_Struct(dir, struct dir_data, dp);
|
|
|
|
if (dp->dir) closedir(dp->dir);
|
|
|
|
if (dp->path) free(dp->path);
|
|
|
|
dp->dir = NULL;
|
|
|
|
dp->path = NULL;
|
|
|
|
dp->dir = opendir(RSTRING(dirname)->ptr);
|
|
|
|
if (dp->dir == NULL) {
|
1998-01-16 15:13:05 +03:00
|
|
|
if (errno == EMFILE || errno == ENFILE) {
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_gc();
|
2001-09-19 10:54:11 +04:00
|
|
|
dp->dir = opendir(RSTRING(dirname)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2001-09-19 10:54:11 +04:00
|
|
|
if (dp->dir == NULL) {
|
1998-01-16 15:19:22 +03:00
|
|
|
rb_sys_fail(RSTRING(dirname)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2001-09-19 10:54:11 +04:00
|
|
|
dp->path = strdup(RSTRING(dirname)->ptr);
|
2000-05-24 08:34:26 +04:00
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
return dir;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
static VALUE
|
|
|
|
dir_s_open(klass, dirname)
|
|
|
|
VALUE klass, dirname;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dp;
|
|
|
|
VALUE dir = Data_Make_Struct(klass, struct dir_data, 0, free_dir, dp);
|
2000-05-25 09:55:12 +04:00
|
|
|
|
|
|
|
dir_initialize(dir, dirname);
|
2000-05-24 08:34:26 +04:00
|
|
|
if (rb_block_given_p()) {
|
2001-02-27 10:52:11 +03:00
|
|
|
return rb_ensure(rb_yield, dir, dir_close, dir);
|
2000-05-24 08:34:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return dir;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dir_closed()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eIOError, "closed directory");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-04-18 12:46:18 +04:00
|
|
|
#define GetDIR(obj, dirp) do {\
|
2001-09-19 10:54:11 +04:00
|
|
|
Data_Get_Struct(obj, struct dir_data, dirp);\
|
|
|
|
if (dirp->dir == NULL) dir_closed();\
|
2002-04-18 12:46:18 +04:00
|
|
|
} while (0)
|
2001-09-19 10:54:11 +04:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_path(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
|
|
|
struct dir_data *dirp;
|
|
|
|
|
|
|
|
GetDIR(dir, dirp);
|
|
|
|
if (!dirp->path) return Qnil;
|
|
|
|
return rb_str_new2(dirp->path);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
dir_read(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1999-01-20 07:59:39 +03:00
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
GetDIR(dir, dirp);
|
|
|
|
errno = 0;
|
2001-09-19 10:54:11 +04:00
|
|
|
dp = readdir(dirp->dir);
|
|
|
|
if (dp) {
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_tainted_str_new(dp->d_name, NAMLEN(dp));
|
2001-09-19 10:54:11 +04:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (errno == 0) { /* end of stream */
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_sys_fail(0);
|
|
|
|
}
|
|
|
|
return Qnil; /* not reached */
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
dir_each(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1998-01-16 15:13:05 +03:00
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
GetDIR(dir, dirp);
|
2001-09-19 10:54:11 +04:00
|
|
|
for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
|
2000-07-31 08:36:38 +04:00
|
|
|
rb_yield(rb_tainted_str_new(dp->d_name, NAMLEN(dp)));
|
2001-09-19 10:54:11 +04:00
|
|
|
if (dirp->dir == NULL) dir_closed();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_tell(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2000-09-21 13:31:00 +04:00
|
|
|
#ifdef HAVE_TELLDIR
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1999-08-13 09:45:20 +04:00
|
|
|
long pos;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
GetDIR(dir, dirp);
|
2001-09-19 10:54:11 +04:00
|
|
|
pos = telldir(dirp->dir);
|
1999-01-20 07:59:39 +03:00
|
|
|
return rb_int2inum(pos);
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_seek(dir, pos)
|
|
|
|
VALUE dir, pos;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-09-21 13:31:00 +04:00
|
|
|
#ifdef HAVE_SEEKDIR
|
1998-01-16 15:13:05 +03:00
|
|
|
GetDIR(dir, dirp);
|
2001-09-19 10:54:11 +04:00
|
|
|
seekdir(dirp->dir, NUM2INT(pos));
|
2002-01-21 10:44:06 +03:00
|
|
|
return dir;
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
rb_notimplement();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2002-01-21 10:44:06 +03:00
|
|
|
static VALUE
|
|
|
|
dir_set_pos(dir, pos)
|
|
|
|
VALUE dir, pos;
|
|
|
|
{
|
|
|
|
dir_seek(dir, pos);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
dir_rewind(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
GetDIR(dir, dirp);
|
2001-09-19 10:54:11 +04:00
|
|
|
rewinddir(dirp->dir);
|
1998-01-16 15:13:05 +03:00
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_close(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2001-09-19 10:54:11 +04:00
|
|
|
struct dir_data *dirp;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-09-19 10:54:11 +04:00
|
|
|
GetDIR(dir, dirp);
|
|
|
|
closedir(dirp->dir);
|
|
|
|
dirp->dir = NULL;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2001-03-16 11:30:09 +03:00
|
|
|
static void
|
|
|
|
dir_chdir(path)
|
|
|
|
const char *path;
|
|
|
|
{
|
|
|
|
if (chdir(path) < 0)
|
|
|
|
rb_sys_fail(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int chdir_blocking = 0;
|
2001-07-14 19:17:19 +04:00
|
|
|
static VALUE chdir_thread = Qnil;
|
2001-03-16 11:30:09 +03:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
chdir_restore(path)
|
2001-11-08 09:43:14 +03:00
|
|
|
char *path;
|
2001-03-16 11:30:09 +03:00
|
|
|
{
|
|
|
|
chdir_blocking--;
|
2001-07-14 19:17:19 +04:00
|
|
|
if (chdir_blocking == 0)
|
|
|
|
chdir_thread = Qnil;
|
2001-03-16 11:30:09 +03:00
|
|
|
dir_chdir(path);
|
2001-11-08 09:43:14 +03:00
|
|
|
free(path);
|
2001-03-16 11:30:09 +03:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
dir_s_chdir(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
2001-03-16 11:30:09 +03:00
|
|
|
VALUE path = Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
char *dist = "";
|
|
|
|
|
|
|
|
rb_secure(2);
|
1999-01-20 07:59:39 +03:00
|
|
|
if (rb_scan_args(argc, argv, "01", &path) == 1) {
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(path);
|
1998-01-16 15:13:05 +03:00
|
|
|
dist = RSTRING(path)->ptr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dist = getenv("HOME");
|
|
|
|
if (!dist) {
|
|
|
|
dist = getenv("LOGDIR");
|
2001-09-06 12:48:07 +04:00
|
|
|
if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-14 19:17:19 +04:00
|
|
|
if (chdir_blocking > 0) {
|
|
|
|
if (!rb_block_given_p() || rb_thread_current() != chdir_thread)
|
|
|
|
rb_warn("conflicting chdir during another chdir block");
|
|
|
|
}
|
2001-03-16 11:30:09 +03:00
|
|
|
|
|
|
|
if (rb_block_given_p()) {
|
2001-11-08 09:43:14 +03:00
|
|
|
char *cwd = my_getcwd();
|
2001-03-16 11:30:09 +03:00
|
|
|
chdir_blocking++;
|
2001-07-14 19:17:19 +04:00
|
|
|
if (chdir_thread == Qnil)
|
|
|
|
chdir_thread = rb_thread_current();
|
2001-03-16 11:30:09 +03:00
|
|
|
dir_chdir(dist);
|
|
|
|
return rb_ensure(rb_yield, path, chdir_restore, (VALUE)cwd);
|
|
|
|
}
|
|
|
|
dir_chdir(dist);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_getwd(dir)
|
|
|
|
VALUE dir;
|
|
|
|
{
|
2001-11-08 09:43:14 +03:00
|
|
|
char *path = my_getcwd();
|
|
|
|
VALUE cwd = rb_tainted_str_new2(path);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-11-08 09:43:14 +03:00
|
|
|
free(path);
|
|
|
|
return cwd;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_chroot(dir, path)
|
|
|
|
VALUE dir, path;
|
|
|
|
{
|
2000-08-07 09:05:04 +04:00
|
|
|
#if defined(HAVE_CHROOT) && !defined(__CHECKER__)
|
1998-01-16 15:13:05 +03:00
|
|
|
rb_secure(2);
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(path);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (chroot(RSTRING(path)->ptr) == -1)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_sys_fail(RSTRING(path)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
#else
|
|
|
|
rb_notimplement();
|
1999-01-20 07:59:39 +03:00
|
|
|
return Qnil; /* not reached */
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_mkdir(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE path, vmode;
|
|
|
|
int mode;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &path, &vmode) == 2) {
|
|
|
|
mode = NUM2INT(vmode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mode = 0777;
|
|
|
|
}
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(path);
|
1999-12-14 09:50:43 +03:00
|
|
|
rb_secure(2);
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#ifndef _WIN32
|
1998-01-16 15:13:05 +03:00
|
|
|
if (mkdir(RSTRING(path)->ptr, mode) == -1)
|
|
|
|
rb_sys_fail(RSTRING(path)->ptr);
|
|
|
|
#else
|
|
|
|
if (mkdir(RSTRING(path)->ptr) == -1)
|
|
|
|
rb_sys_fail(RSTRING(path)->ptr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return INT2FIX(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_rmdir(obj, dir)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE obj, dir;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2001-05-02 08:22:21 +04:00
|
|
|
SafeStringValue(dir);
|
1999-12-14 09:50:43 +03:00
|
|
|
rb_secure(2);
|
1998-01-16 15:19:22 +03:00
|
|
|
if (rmdir(RSTRING(dir)->ptr) < 0)
|
|
|
|
rb_sys_fail(RSTRING(dir)->ptr);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-03-07 11:37:59 +03:00
|
|
|
return INT2FIX(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
/* Return nonzero if S has any special globbing chars in it. */
|
|
|
|
static int
|
2001-05-27 21:24:24 +04:00
|
|
|
has_magic(s, send, flags)
|
1999-08-13 09:45:20 +04:00
|
|
|
char *s, *send;
|
2001-05-27 21:24:24 +04:00
|
|
|
int flags;
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
register char *p = s;
|
|
|
|
register char c;
|
|
|
|
int open = 0;
|
2001-05-27 21:24:24 +04:00
|
|
|
int escape = !(flags & FNM_NOESCAPE);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
while ((c = *p++) != '\0') {
|
|
|
|
switch (c) {
|
|
|
|
case '?':
|
|
|
|
case '*':
|
|
|
|
return Qtrue;
|
|
|
|
|
2001-07-02 12:46:28 +04:00
|
|
|
case '[': /* Only accept an open brace if there is a close */
|
|
|
|
open++; /* brace to match it. Bracket expressions must be */
|
1999-08-13 09:45:20 +04:00
|
|
|
continue; /* complete, according to Posix.2 */
|
|
|
|
case ']':
|
|
|
|
if (open)
|
|
|
|
return Qtrue;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\\':
|
2001-05-27 21:24:24 +04:00
|
|
|
if (escape && *p++ == '\0')
|
1999-08-13 09:45:20 +04:00
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send && p >= send) break;
|
|
|
|
}
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
extract_path(p, pend)
|
|
|
|
char *p, *pend;
|
|
|
|
{
|
|
|
|
char *alloc;
|
|
|
|
int len;
|
|
|
|
|
|
|
|
len = pend - p;
|
|
|
|
alloc = ALLOC_N(char, len+1);
|
|
|
|
memcpy(alloc, p, len);
|
2001-03-13 10:58:07 +03:00
|
|
|
if (len > 1 && pend[-1] == '/'
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#if defined DOSISH_DRIVE_LETTER
|
2001-03-20 17:50:43 +03:00
|
|
|
&& pend[-2] != ':'
|
2001-03-13 10:58:07 +03:00
|
|
|
#endif
|
|
|
|
) {
|
1999-08-13 09:45:20 +04:00
|
|
|
alloc[len-1] = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
alloc[len] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return alloc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char*
|
|
|
|
extract_elem(path)
|
|
|
|
char *path;
|
|
|
|
{
|
|
|
|
char *pend;
|
|
|
|
|
2000-09-25 11:00:55 +04:00
|
|
|
pend = strchr(path, '/');
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!pend) pend = path + strlen(path);
|
|
|
|
|
|
|
|
return extract_path(path, pend);
|
|
|
|
}
|
|
|
|
|
2001-02-14 08:52:06 +03:00
|
|
|
static void
|
2001-02-15 09:01:00 +03:00
|
|
|
remove_backslashes(p)
|
2001-02-14 08:52:06 +03:00
|
|
|
char *p;
|
|
|
|
{
|
|
|
|
char *pend = p + strlen(p);
|
|
|
|
char *t = p;
|
|
|
|
|
|
|
|
while (p < pend) {
|
|
|
|
if (*p == '\\') {
|
2001-05-16 13:05:54 +04:00
|
|
|
if (++p == pend) break;
|
2001-02-14 08:52:06 +03:00
|
|
|
}
|
|
|
|
*t++ = *p++;
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifndef S_ISDIR
|
|
|
|
# define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
struct glob_args {
|
|
|
|
void (*func) _((const char*, VALUE));
|
|
|
|
const char *c;
|
|
|
|
VALUE v;
|
|
|
|
};
|
|
|
|
|
2002-09-13 18:46:44 +04:00
|
|
|
static VALUE glob_func_caller _((VALUE));
|
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
static VALUE
|
2002-09-13 18:46:44 +04:00
|
|
|
glob_func_caller(val)
|
|
|
|
VALUE val;
|
2002-09-12 10:27:15 +04:00
|
|
|
{
|
2002-09-13 18:46:44 +04:00
|
|
|
struct glob_args *args = (struct glob_args *)val;
|
2002-09-12 10:27:15 +04:00
|
|
|
(*args->func)(args->c, args->v);
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
glob_call_func(func, path, arg)
|
|
|
|
void (*func) _((const char*, VALUE));
|
|
|
|
const char *path;
|
|
|
|
VALUE arg;
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
struct glob_args args;
|
|
|
|
|
|
|
|
args.func = func;
|
|
|
|
args.c = path;
|
|
|
|
args.v = arg;
|
|
|
|
|
|
|
|
rb_protect(glob_func_caller, (VALUE)&args, &status);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-09-06 12:48:07 +04:00
|
|
|
glob_helper(path, sub, flags, func, arg)
|
1999-08-13 09:45:20 +04:00
|
|
|
char *path;
|
2001-09-06 12:48:07 +04:00
|
|
|
char *sub;
|
2001-05-27 21:24:24 +04:00
|
|
|
int flags;
|
2001-07-14 19:17:19 +04:00
|
|
|
void (*func) _((const char*, VALUE));
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE arg;
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
char *p, *m;
|
2002-09-12 10:27:15 +04:00
|
|
|
int status = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2001-09-06 12:48:07 +04:00
|
|
|
p = sub ? sub : path;
|
|
|
|
if (!has_magic(p, 0, flags)) {
|
2002-05-11 05:53:48 +04:00
|
|
|
#if defined DOSISH
|
2001-02-15 09:01:00 +03:00
|
|
|
remove_backslashes(path);
|
2002-05-11 05:53:48 +04:00
|
|
|
#else
|
|
|
|
if (!(flags & FNM_NOESCAPE)) remove_backslashes(p);
|
|
|
|
#endif
|
2002-06-15 14:24:38 +04:00
|
|
|
if (lstat(path, &st) == 0) {
|
2002-09-12 10:27:15 +04:00
|
|
|
status = glob_call_func(func, path, arg);
|
|
|
|
if (status) return status;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2001-03-21 06:41:45 +03:00
|
|
|
else if (errno != ENOENT) {
|
2001-02-19 12:15:27 +03:00
|
|
|
/* In case stat error is other than ENOENT and
|
|
|
|
we may want to know what is wrong. */
|
|
|
|
rb_sys_warning(path);
|
|
|
|
}
|
2002-09-12 10:27:15 +04:00
|
|
|
return 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2002-09-12 10:27:15 +04:00
|
|
|
while (p && !status) {
|
2000-09-25 11:00:55 +04:00
|
|
|
if (*p == '/') p++;
|
|
|
|
m = strchr(p, '/');
|
2001-05-27 21:24:24 +04:00
|
|
|
if (has_magic(p, m, flags)) {
|
1999-11-25 12:03:08 +03:00
|
|
|
char *dir, *base, *magic, *buf;
|
1999-08-13 09:45:20 +04:00
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
1999-11-25 12:03:08 +03:00
|
|
|
int recursive = 0;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
struct d_link {
|
|
|
|
char *path;
|
|
|
|
struct d_link *next;
|
|
|
|
} *tmp, *link = 0;
|
|
|
|
|
|
|
|
base = extract_path(path, p);
|
|
|
|
if (path == p) dir = ".";
|
|
|
|
else dir = base;
|
|
|
|
|
1999-11-25 12:03:08 +03:00
|
|
|
magic = extract_elem(p);
|
2001-03-21 06:51:23 +03:00
|
|
|
if (stat(dir, &st) < 0) {
|
2001-03-21 06:41:45 +03:00
|
|
|
if (errno != ENOENT) rb_sys_warning(dir);
|
2001-02-13 08:09:11 +03:00
|
|
|
free(base);
|
2002-09-12 10:27:15 +04:00
|
|
|
free(magic);
|
2001-02-13 08:09:11 +03:00
|
|
|
break;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2001-02-13 08:09:11 +03:00
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2001-02-19 12:15:27 +03:00
|
|
|
if (m && strcmp(magic, "**") == 0) {
|
2001-09-06 12:48:07 +04:00
|
|
|
int n = strlen(base);
|
2001-02-19 12:15:27 +03:00
|
|
|
recursive = 1;
|
2001-09-06 12:48:07 +04:00
|
|
|
buf = ALLOC_N(char, n+strlen(m)+3);
|
2001-02-28 09:30:03 +03:00
|
|
|
sprintf(buf, "%s%s", base, *base ? m : m+1);
|
2002-09-12 10:27:15 +04:00
|
|
|
status = glob_helper(buf, buf+n, flags, func, arg);
|
2001-02-19 12:15:27 +03:00
|
|
|
free(buf);
|
2002-09-12 10:27:15 +04:00
|
|
|
if (status) goto finalize;
|
2001-02-19 12:15:27 +03:00
|
|
|
}
|
2001-03-19 06:20:24 +03:00
|
|
|
dirp = opendir(dir);
|
|
|
|
if (dirp == NULL) {
|
|
|
|
rb_sys_warning(dir);
|
|
|
|
free(base);
|
2002-09-12 10:27:15 +04:00
|
|
|
free(magic);
|
2001-03-19 06:20:24 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
|
|
|
else {
|
2001-02-19 12:15:27 +03:00
|
|
|
free(base);
|
2002-09-12 10:27:15 +04:00
|
|
|
free(magic);
|
2001-02-19 12:15:27 +03:00
|
|
|
break;
|
2001-02-13 08:09:11 +03:00
|
|
|
}
|
|
|
|
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#if defined DOSISH_DRIVE_LETTER
|
2001-03-13 10:58:07 +03:00
|
|
|
#define BASE (*base && !((isdirsep(*base) && !base[1]) || (base[1] == ':' && isdirsep(base[2]) && !base[3])))
|
|
|
|
#else
|
* configure.in, defines.h, dir.c, dir.h, dln.c, error.c,
eval.c, file.c, hash.c, io.c, main.c, missing.c,
process.c, ruby.c, rubysig.h, signal.c, st.c, util.c, util.h,
bcc/Makefile.sub, win32/Makefile.sub, win32/win32.h,
ext/Win32API/Win32API.c, ext/socket/getaddrinfo.c,
ext/socket/getnameinfo.c, ext/socket/socket.c,
ext/tcltklib/stubs.c
: replace "NT" with "_WIN32", add DOSISH_DRIVE_LETTER
* wince/exe.mak : delete \r at the end of lines.
* wince/mswince-ruby17.def : delete rb_obj_become
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3148 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2002-12-15 06:18:08 +03:00
|
|
|
#define BASE (*base && !(isdirsep(*base) && !base[1]))
|
2001-03-13 10:58:07 +03:00
|
|
|
#endif
|
2000-02-01 06:12:21 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
|
1999-11-25 12:03:08 +03:00
|
|
|
if (recursive) {
|
|
|
|
if (strcmp(".", dp->d_name) == 0 || strcmp("..", dp->d_name) == 0)
|
|
|
|
continue;
|
|
|
|
buf = ALLOC_N(char, strlen(base)+NAMLEN(dp)+strlen(m)+6);
|
2002-01-25 11:22:11 +03:00
|
|
|
sprintf(buf, "%s%s%s", base, (BASE) ? "/" : "", dp->d_name);
|
2001-02-19 12:15:27 +03:00
|
|
|
if (lstat(buf, &st) < 0) {
|
2001-03-21 06:41:45 +03:00
|
|
|
if (errno != ENOENT) rb_sys_warning(buf);
|
2003-03-04 10:04:11 +03:00
|
|
|
free(buf);
|
2001-02-19 12:15:27 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
2001-09-06 12:48:07 +04:00
|
|
|
char *t = buf+strlen(buf);
|
|
|
|
strcpy(t, "/**");
|
|
|
|
strcpy(t+3, m);
|
2002-09-12 10:27:15 +04:00
|
|
|
status = glob_helper(buf, t, flags, func, arg);
|
|
|
|
free(buf);
|
2002-09-25 18:52:37 +04:00
|
|
|
if (status) break;
|
2002-09-12 12:48:57 +04:00
|
|
|
continue;
|
2001-02-19 12:15:27 +03:00
|
|
|
}
|
1999-11-25 12:03:08 +03:00
|
|
|
free(buf);
|
|
|
|
continue;
|
|
|
|
}
|
2001-05-27 21:24:24 +04:00
|
|
|
if (fnmatch(magic, dp->d_name, flags) == 0) {
|
1999-11-25 12:03:08 +03:00
|
|
|
buf = ALLOC_N(char, strlen(base)+NAMLEN(dp)+2);
|
2002-01-25 11:22:11 +03:00
|
|
|
sprintf(buf, "%s%s%s", base, (BASE) ? "/" : "", dp->d_name);
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!m) {
|
2002-09-12 13:11:12 +04:00
|
|
|
status = glob_call_func(func, buf, arg);
|
2002-09-12 12:48:57 +04:00
|
|
|
free(buf);
|
2002-09-25 18:52:37 +04:00
|
|
|
if (status) break;
|
1999-08-13 09:45:20 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tmp = ALLOC(struct d_link);
|
1999-11-25 12:03:08 +03:00
|
|
|
tmp->path = buf;
|
1999-08-13 09:45:20 +04:00
|
|
|
tmp->next = link;
|
|
|
|
link = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
2002-09-25 18:52:37 +04:00
|
|
|
finalize:
|
1999-08-13 09:45:20 +04:00
|
|
|
free(base);
|
|
|
|
free(magic);
|
2001-03-21 06:41:45 +03:00
|
|
|
if (link) {
|
|
|
|
while (link) {
|
2002-09-12 10:27:15 +04:00
|
|
|
if (status == 0) {
|
|
|
|
if (stat(link->path, &st) == 0) {
|
|
|
|
if (S_ISDIR(st.st_mode)) {
|
|
|
|
int len = strlen(link->path);
|
|
|
|
int mlen = strlen(m);
|
|
|
|
char *t = ALLOC_N(char, len+mlen+1);
|
|
|
|
|
|
|
|
sprintf(t, "%s%s", link->path, m);
|
|
|
|
status = glob_helper(t, t+len, flags, func, arg);
|
|
|
|
free(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_sys_warning(link->path);
|
2001-03-21 06:41:45 +03:00
|
|
|
}
|
2001-02-19 12:15:27 +03:00
|
|
|
}
|
2002-04-10 13:58:39 +04:00
|
|
|
tmp = link;
|
|
|
|
link = link->next;
|
|
|
|
free(tmp->path);
|
|
|
|
free(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
2001-03-21 06:41:45 +03:00
|
|
|
break;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p = m;
|
|
|
|
}
|
2002-09-12 10:27:15 +04:00
|
|
|
return status;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-13 13:11:09 +03:00
|
|
|
static void
|
|
|
|
rb_glob2(path, flags, func, arg)
|
|
|
|
char *path;
|
|
|
|
int flags;
|
|
|
|
void (*func) _((const char*, VALUE));
|
|
|
|
VALUE arg;
|
|
|
|
{
|
2002-09-12 10:27:15 +04:00
|
|
|
int status = glob_helper(path, 0, flags, func, arg);
|
|
|
|
if (status) rb_jump_tag(status);
|
2002-03-13 13:11:09 +03:00
|
|
|
}
|
|
|
|
|
2000-11-20 04:24:28 +03:00
|
|
|
void
|
|
|
|
rb_glob(path, func, arg)
|
|
|
|
char *path;
|
2001-05-06 19:06:00 +04:00
|
|
|
void (*func) _((const char*, VALUE));
|
2000-11-20 04:24:28 +03:00
|
|
|
VALUE arg;
|
|
|
|
{
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_glob2(path, 0, func, arg);
|
2000-11-20 04:24:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-03-21 06:41:45 +03:00
|
|
|
rb_globi(path, func, arg)
|
2000-11-20 04:24:28 +03:00
|
|
|
char *path;
|
2001-07-14 19:17:19 +04:00
|
|
|
void (*func) _((const char*, VALUE));
|
2000-11-20 04:24:28 +03:00
|
|
|
VALUE arg;
|
|
|
|
{
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_glob2(path, FNM_CASEFOLD, func, arg);
|
2000-11-20 04:24:28 +03:00
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static void
|
|
|
|
push_pattern(path, ary)
|
2001-05-06 19:06:00 +04:00
|
|
|
const char *path;
|
1999-08-13 09:45:20 +04:00
|
|
|
VALUE ary;
|
|
|
|
{
|
2001-02-14 08:52:06 +03:00
|
|
|
VALUE str = rb_tainted_str_new2(path);
|
|
|
|
|
|
|
|
if (ary) {
|
|
|
|
rb_ary_push(ary, str);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_yield(str);
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
static void
|
2002-03-13 13:11:09 +03:00
|
|
|
push_globs(ary, s, flags)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
char *s;
|
2002-03-13 13:11:09 +03:00
|
|
|
int flags;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_glob2(s, flags, push_pattern, ary);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-03-13 13:11:09 +03:00
|
|
|
push_braces(ary, s, flags)
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
char *s;
|
2002-03-13 13:11:09 +03:00
|
|
|
int flags;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2002-02-18 12:52:48 +03:00
|
|
|
char *buf;
|
1998-01-16 15:13:05 +03:00
|
|
|
char *p, *t, *b;
|
|
|
|
char *lbrace, *rbrace;
|
1999-08-13 09:45:20 +04:00
|
|
|
int nest = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
p = s;
|
|
|
|
lbrace = rbrace = 0;
|
|
|
|
while (*p) {
|
|
|
|
if (*p == '{') {
|
|
|
|
lbrace = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
while (*p) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (*p == '{') nest++;
|
|
|
|
if (*p == '}' && --nest == 0) {
|
1998-01-16 15:13:05 +03:00
|
|
|
rbrace = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lbrace) {
|
1999-08-13 09:45:20 +04:00
|
|
|
int len = strlen(s);
|
2002-02-18 12:52:48 +03:00
|
|
|
buf = xmalloc(len + 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
memcpy(buf, s, lbrace-s);
|
|
|
|
b = buf + (lbrace-s);
|
|
|
|
p = lbrace;
|
|
|
|
while (*p != '}') {
|
|
|
|
t = p + 1;
|
|
|
|
for (p = t; *p!='}' && *p!=','; p++) {
|
|
|
|
/* skip inner braces */
|
|
|
|
if (*p == '{') while (*p!='}') p++;
|
|
|
|
}
|
|
|
|
memcpy(b, t, p-t);
|
|
|
|
strcpy(b+(p-t), rbrace+1);
|
2002-03-13 13:11:09 +03:00
|
|
|
push_braces(ary, buf, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-02-18 12:52:48 +03:00
|
|
|
free(buf);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
2002-03-13 13:11:09 +03:00
|
|
|
push_globs(ary, s, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-07-02 12:46:28 +04:00
|
|
|
#define isdelim(c) ((c)=='\0')
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_push_glob(str, flags)
|
|
|
|
VALUE str;
|
|
|
|
int flags;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
char *p, *pend;
|
2002-02-18 12:52:48 +03:00
|
|
|
char *buf;
|
1999-08-13 09:45:20 +04:00
|
|
|
char *t;
|
2002-03-25 12:08:15 +03:00
|
|
|
int nest, maxnest;
|
2002-03-13 13:11:09 +03:00
|
|
|
int noescape = flags & FNM_NOESCAPE;
|
|
|
|
VALUE ary;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2002-03-13 13:11:09 +03:00
|
|
|
if (rb_block_given_p())
|
2002-03-14 02:59:41 +03:00
|
|
|
ary = 0;
|
2002-03-13 13:11:09 +03:00
|
|
|
else
|
2001-02-14 08:52:06 +03:00
|
|
|
ary = rb_ary_new();
|
2002-03-13 13:11:09 +03:00
|
|
|
|
|
|
|
SafeStringValue(str);
|
2002-02-18 12:52:48 +03:00
|
|
|
buf = xmalloc(RSTRING(str)->len + 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
p = RSTRING(str)->ptr;
|
|
|
|
pend = p + RSTRING(str)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (p < pend) {
|
|
|
|
t = buf;
|
2002-03-25 12:08:15 +03:00
|
|
|
nest = maxnest = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend && isdelim(*p)) p++;
|
|
|
|
while (p < pend && !isdelim(*p)) {
|
2002-03-13 13:11:09 +03:00
|
|
|
if (*p == '{') nest++, maxnest++;
|
|
|
|
if (*p == '}') nest--;
|
|
|
|
if (!noescape && *p == '\\') {
|
2001-02-14 08:52:06 +03:00
|
|
|
*t++ = *p++;
|
|
|
|
if (p == pend) break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
*t++ = *p++;
|
|
|
|
}
|
|
|
|
*t = '\0';
|
2002-03-13 13:11:09 +03:00
|
|
|
if (maxnest == 0) {
|
|
|
|
push_globs(ary, buf, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2002-03-13 13:11:09 +03:00
|
|
|
else if (nest == 0) {
|
|
|
|
push_braces(ary, buf, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
/* else unmatched braces */
|
|
|
|
}
|
2002-02-18 12:52:48 +03:00
|
|
|
free(buf);
|
2002-03-13 13:11:09 +03:00
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_aref(obj, str)
|
|
|
|
VALUE obj, str;
|
|
|
|
{
|
|
|
|
return rb_push_glob(str, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_s_glob(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE str, rflags;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "11", &str, &rflags) == 2)
|
|
|
|
flags = NUM2INT(rflags);
|
|
|
|
else
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
return rb_push_glob(str, flags);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
dir_foreach(io, dirname)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE io, dirname;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
VALUE dir;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
dir = rb_funcall(rb_cDir, rb_intern("open"), 1, dirname);
|
2000-06-12 11:48:31 +04:00
|
|
|
rb_ensure(dir_each, dir, dir_close, dir);
|
|
|
|
return Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
static VALUE
|
|
|
|
dir_entries(io, dirname)
|
|
|
|
VALUE io, dirname;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
VALUE dir;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
dir = rb_funcall(rb_cDir, rb_intern("open"), 1, dirname);
|
|
|
|
return rb_ensure(rb_Array, dir, dir_close, dir);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-06-13 14:51:39 +04:00
|
|
|
static VALUE
|
|
|
|
file_s_fnmatch(argc, argv, obj)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE obj;
|
|
|
|
{
|
|
|
|
VALUE pattern, path;
|
|
|
|
VALUE rflags;
|
|
|
|
int flags;
|
|
|
|
|
|
|
|
if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
|
|
|
|
flags = NUM2INT(rflags);
|
|
|
|
else
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
StringValue(pattern);
|
|
|
|
StringValue(path);
|
|
|
|
|
|
|
|
if (fnmatch(RSTRING(pattern)->ptr, RSTRING(path)->ptr, flags) == 0)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return Qfalse;
|
|
|
|
}
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
void
|
|
|
|
Init_Dir()
|
|
|
|
{
|
|
|
|
rb_cDir = rb_define_class("Dir", rb_cObject);
|
|
|
|
|
|
|
|
rb_include_module(rb_cDir, rb_mEnumerable);
|
|
|
|
|
2002-12-20 11:33:17 +03:00
|
|
|
rb_define_alloc_func(rb_cDir, dir_s_alloc);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_singleton_method(rb_cDir, "open", dir_s_open, 1);
|
|
|
|
rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, 1);
|
|
|
|
rb_define_singleton_method(rb_cDir, "entries", dir_entries, 1);
|
|
|
|
|
2000-02-29 11:05:32 +03:00
|
|
|
rb_define_method(rb_cDir,"initialize", dir_initialize, 1);
|
2001-09-19 10:54:11 +04:00
|
|
|
rb_define_method(rb_cDir,"path", dir_path, 0);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cDir,"read", dir_read, 0);
|
|
|
|
rb_define_method(rb_cDir,"each", dir_each, 0);
|
|
|
|
rb_define_method(rb_cDir,"rewind", dir_rewind, 0);
|
|
|
|
rb_define_method(rb_cDir,"tell", dir_tell, 0);
|
|
|
|
rb_define_method(rb_cDir,"seek", dir_seek, 1);
|
2000-06-12 11:48:31 +04:00
|
|
|
rb_define_method(rb_cDir,"pos", dir_tell, 0);
|
2002-01-21 10:44:06 +03:00
|
|
|
rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cDir,"close", dir_close, 0);
|
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
|
|
|
|
rb_define_singleton_method(rb_cDir,"pwd", dir_s_getwd, 0);
|
|
|
|
rb_define_singleton_method(rb_cDir,"chroot", dir_s_chroot, 1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"mkdir", dir_s_mkdir, -1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
|
|
|
|
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, -1);
|
|
|
|
rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, 1);
|
2001-06-13 14:51:39 +04:00
|
|
|
|
|
|
|
rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1);
|
|
|
|
rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1);
|
|
|
|
|
|
|
|
rb_file_const("FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE));
|
|
|
|
rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
|
2002-03-13 13:11:09 +03:00
|
|
|
rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
|
2001-06-13 14:51:39 +04:00
|
|
|
rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|