summaryrefslogtreecommitdiff
path: root/dir.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-01-20 04:59:39 +0000
commit62e648e148b3cb9f96dcce808c55c02b7ccb4486 (patch)
tree9708892ece92e860d81559ab55e6b1f9400d7ffc /dir.c
parentaeb049c573be4dc24dd20650f40e4777e0f698cf (diff)
ruby 1.3 cycle
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/RUBY@372 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c155
1 files changed, 101 insertions, 54 deletions
diff --git a/dir.c b/dir.c
index 29ce261272..51d7f9e344 100644
--- a/dir.c
+++ b/dir.c
@@ -6,7 +6,7 @@
$Date$
created at: Wed Jan 5 09:51:01 JST 1994
- Copyright (C) 1993-1996 Yukihiro Matsumoto
+ Copyright (C) 1993-1998 Yukihiro Matsumoto
************************************************/
@@ -27,6 +27,9 @@
#if HAVE_DIRENT_H
# include <dirent.h>
# define NAMLEN(dirent) strlen((dirent)->d_name)
+#elif HAVE_DIRECT_H
+# include <direct.h>
+# define NAMLEN(dirent) strlen((dirent)->d_name)
#else
# define dirent direct
# define NAMLEN(dirent) (dirent)->d_namlen
@@ -39,7 +42,7 @@
# if HAVE_NDIR_H
# include <ndir.h>
# endif
-# ifdef NT
+# if defined(NT) && defined(_MSC_VER)
# include "missing/dir.h"
# endif
#endif
@@ -50,7 +53,11 @@
char *getenv();
#endif
-static VALUE cDir;
+#ifdef USE_CWGUSI
+# include <sys/errno.h>
+#endif
+
+VALUE rb_cDir;
static void
free_dir(dir)
@@ -59,6 +66,8 @@ free_dir(dir)
if (dir) closedir(dir);
}
+static VALUE dir_close _((VALUE));
+
static VALUE
dir_s_open(dir_class, dirname)
VALUE dir_class, dirname;
@@ -71,7 +80,7 @@ dir_s_open(dir_class, dirname)
dirp = opendir(RSTRING(dirname)->ptr);
if (dirp == NULL) {
if (errno == EMFILE || errno == ENFILE) {
- gc_gc();
+ rb_gc();
dirp = opendir(RSTRING(dirname)->ptr);
}
if (dirp == NULL) {
@@ -81,13 +90,17 @@ dir_s_open(dir_class, dirname)
obj = Data_Wrap_Struct(dir_class, 0, free_dir, dirp);
+ if (rb_iterator_p()) {
+ return rb_ensure(rb_yield, obj, dir_close, obj);
+ }
+
return obj;
}
static void
dir_closed()
{
- Fail("closed directory");
+ rb_raise(rb_eIOError, "closed directory");
}
#define GetDIR(obj, dirp) {\
@@ -96,6 +109,27 @@ dir_closed()
}
static VALUE
+dir_read(dir)
+ VALUE dir;
+{
+ DIR *dirp;
+ struct dirent *dp;
+
+ GetDIR(dir, dirp);
+ errno = 0;
+ dp = readdir(dirp);
+ if (dp)
+ return rb_tainted_str_new(dp->d_name, NAMLEN(dp));
+ else if (errno == 0) { /* end of stream */
+ return Qnil;
+ }
+ else {
+ rb_sys_fail(0);
+ }
+ return Qnil; /* not reached */
+}
+
+static VALUE
dir_each(dir)
VALUE dir;
{
@@ -105,7 +139,7 @@ dir_each(dir)
GetDIR(dir, dirp);
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
- file = str_taint(str_new(dp->d_name, NAMLEN(dp)));
+ file = rb_tainted_str_new(dp->d_name, NAMLEN(dp));
rb_yield(file);
}
return dir;
@@ -118,10 +152,10 @@ dir_tell(dir)
DIR *dirp;
int pos;
-#if !defined(__CYGWIN32__)
+#if !defined(__CYGWIN32__) && !defined(__BEOS__)
GetDIR(dir, dirp);
pos = telldir(dirp);
- return int2inum(pos);
+ return rb_int2inum(pos);
#else
rb_notimplement();
#endif
@@ -133,7 +167,7 @@ dir_seek(dir, pos)
{
DIR *dirp;
-#if !defined(__CYGWIN32__)
+#if !defined(__CYGWIN32__) && !defined(__BEOS__)
GetDIR(dir, dirp);
seekdir(dirp, NUM2INT(pos));
return dir;
@@ -177,8 +211,7 @@ dir_s_chdir(argc, argv, obj)
char *dist = "";
rb_secure(2);
- rb_scan_args(argc, argv, "01", &path);
- if (!NIL_P(path)) {
+ if (rb_scan_args(argc, argv, "01", &path) == 1) {
Check_SafeStr(path);
dist = RSTRING(path)->ptr;
}
@@ -190,7 +223,7 @@ dir_s_chdir(argc, argv, obj)
}
if (chdir(dist) < 0)
- rb_sys_fail(0);
+ rb_sys_fail(dist);
return INT2FIX(0);
}
@@ -199,32 +232,33 @@ static VALUE
dir_s_getwd(dir)
VALUE dir;
{
- extern char *getwd();
char path[MAXPATHLEN];
#ifdef HAVE_GETCWD
if (getcwd(path, sizeof(path)) == 0) rb_sys_fail(path);
#else
+ extern char *getwd();
if (getwd(path) == 0) rb_sys_fail(path);
#endif
- return str_taint(str_new2(path));
+ return rb_tainted_str_new2(path);
}
static VALUE
dir_s_chroot(dir, path)
VALUE dir, path;
{
-#if !defined(DJGPP) && !defined(__CYGWIN32__) && !defined(NT) && !defined(__human68k__)
+#if !defined(DJGPP) && !defined(NT) && !defined(__human68k__) && !defined(USE_CWGUSI) && !defined(__BEOS__)
rb_secure(2);
Check_SafeStr(path);
if (chroot(RSTRING(path)->ptr) == -1)
- rb_sys_fail(0);
+ rb_sys_fail(RSTRING(path)->ptr);
return INT2FIX(0);
#else
rb_notimplement();
+ return Qnil; /* not reached */
#endif
}
@@ -246,7 +280,7 @@ dir_s_mkdir(argc, argv, obj)
}
Check_SafeStr(path);
-#ifndef NT
+#if !defined(NT) && !defined(USE_CWGUSI)
if (mkdir(RSTRING(path)->ptr, mode) == -1)
rb_sys_fail(RSTRING(path)->ptr);
#else
@@ -266,7 +300,7 @@ dir_s_rmdir(obj, dir)
if (rmdir(RSTRING(dir)->ptr) < 0)
rb_sys_fail(RSTRING(dir)->ptr);
- return TRUE;
+ return Qtrue;
}
#define isdelim(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)=='\0')
@@ -285,7 +319,7 @@ push_globs(ary, s)
if (fnames == (char**)-1) rb_sys_fail(s);
ff = fnames;
while (*ff) {
- ary_push(ary, str_taint(str_new2(*ff)));
+ rb_ary_push(ary, rb_tainted_str_new2(*ff));
free(*ff);
ff++;
}
@@ -341,22 +375,24 @@ push_braces(ary, s)
}
static VALUE
-dir_s_glob(dir, vstr)
- VALUE dir, vstr;
+dir_s_glob(dir, str)
+ VALUE dir, str;
{
char *p, *pend;
char buf[MAXPATHLEN];
char *t, *t0;
int nest;
VALUE ary;
- struct RString *str;
- Check_SafeStr(vstr);
- str = RSTRING(vstr);
- ary = ary_new();
+ Check_SafeStr(str);
+ if (RSTRING(str)->len > MAXPATHLEN) {
+ rb_raise(rb_eArgError, "pathname too long (%d bytes)",
+ RSTRING(str)->len);
+ }
+ ary = rb_ary_new();
- p = str->ptr;
- pend = p + str->len;
+ p = RSTRING(str)->ptr;
+ pend = p + RSTRING(str)->len;
while (p < pend) {
t = buf;
@@ -389,37 +425,48 @@ dir_foreach(io, dirname)
{
VALUE dir;
- dir = dir_s_open(cDir, dirname);
+ dir = rb_funcall(rb_cDir, rb_intern("open"), 1, dirname);
return rb_ensure(dir_each, dir, dir_close, dir);
}
-void
-Init_Dir()
+static VALUE
+dir_entries(io, dirname)
+ VALUE io, dirname;
{
- extern VALUE mEnumerable;
-
- cDir = rb_define_class("Dir", cObject);
-
- rb_include_module(cDir, mEnumerable);
-
- rb_define_singleton_method(cDir, "open", dir_s_open, 1);
- rb_define_singleton_method(cDir, "foreach", dir_foreach, 1);
-
- rb_define_method(cDir,"each", dir_each, 0);
- rb_define_method(cDir,"rewind", dir_rewind, 0);
- rb_define_method(cDir,"tell", dir_tell, 0);
- rb_define_method(cDir,"seek", dir_seek, 1);
- rb_define_method(cDir,"close", dir_close, 0);
+ VALUE dir;
- rb_define_singleton_method(cDir,"chdir", dir_s_chdir, -1);
- rb_define_singleton_method(cDir,"getwd", dir_s_getwd, 0);
- rb_define_singleton_method(cDir,"pwd", dir_s_getwd, 0);
- rb_define_singleton_method(cDir,"chroot", dir_s_chroot, 1);
- rb_define_singleton_method(cDir,"mkdir", dir_s_mkdir, -1);
- rb_define_singleton_method(cDir,"rmdir", dir_s_rmdir, 1);
- rb_define_singleton_method(cDir,"delete", dir_s_rmdir, 1);
- rb_define_singleton_method(cDir,"unlink", dir_s_rmdir, 1);
+ dir = rb_funcall(rb_cDir, rb_intern("open"), 1, dirname);
+ return rb_ensure(rb_Array, dir, dir_close, dir);
+}
- rb_define_singleton_method(cDir,"glob", dir_s_glob, 1);
- rb_define_singleton_method(cDir,"[]", dir_s_glob, 1);
+void
+Init_Dir()
+{
+ rb_cDir = rb_define_class("Dir", rb_cObject);
+
+ rb_include_module(rb_cDir, rb_mEnumerable);
+
+ rb_define_singleton_method(rb_cDir, "new", dir_s_open, 1);
+ 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);
+
+ 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);
+ 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);
+
+ rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, 1);
+ rb_define_singleton_method(rb_cDir,"[]", dir_s_glob, 1);
}