* transcode_data.h (rb_trans_elem_t): new field: from and to.
[ruby-svn.git] / dir.c
blobdcbbc14e0ed7a8261be8bab42022e55a7eb92286
1 /**********************************************************************
3 dir.c -
5 $Author$
6 created at: Wed Jan 5 09:51:01 JST 1994
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9 Copyright (C) 2000 Network Applied Communication Laboratory, Inc.
10 Copyright (C) 2000 Information-technology Promotion Agency, Japan
12 **********************************************************************/
14 #include "ruby/ruby.h"
15 #include "ruby/encoding.h"
17 #include <sys/types.h>
18 #include <sys/stat.h>
20 #ifdef HAVE_UNISTD_H
21 #include <unistd.h>
22 #endif
24 #if defined HAVE_DIRENT_H && !defined _WIN32
25 # include <dirent.h>
26 # define NAMLEN(dirent) strlen((dirent)->d_name)
27 #elif defined HAVE_DIRECT_H && !defined _WIN32
28 # include <direct.h>
29 # define NAMLEN(dirent) strlen((dirent)->d_name)
30 #else
31 # define dirent direct
32 # if !defined __NeXT__
33 # define NAMLEN(dirent) (dirent)->d_namlen
34 # else
35 # /* On some versions of NextStep, d_namlen is always zero, so avoid it. */
36 # define NAMLEN(dirent) strlen((dirent)->d_name)
37 # endif
38 # if HAVE_SYS_NDIR_H
39 # include <sys/ndir.h>
40 # endif
41 # if HAVE_SYS_DIR_H
42 # include <sys/dir.h>
43 # endif
44 # if HAVE_NDIR_H
45 # include <ndir.h>
46 # endif
47 # ifdef _WIN32
48 # include "win32/dir.h"
49 # endif
50 #endif
52 #include <errno.h>
54 #ifndef HAVE_STDLIB_H
55 char *getenv();
56 #endif
58 #ifndef HAVE_STRING_H
59 char *strchr(char*,char);
60 #endif
62 #include <ctype.h>
64 #include "ruby/util.h"
66 #if !defined HAVE_LSTAT && !defined lstat
67 #define lstat stat
68 #endif
70 #define FNM_NOESCAPE 0x01
71 #define FNM_PATHNAME 0x02
72 #define FNM_DOTMATCH 0x04
73 #define FNM_CASEFOLD 0x08
74 #if CASEFOLD_FILESYSTEM
75 #define FNM_SYSCASE FNM_CASEFOLD
76 #else
77 #define FNM_SYSCASE 0
78 #endif
80 #define FNM_NOMATCH 1
81 #define FNM_ERROR 2
83 # define Next(p, e, enc) (p + rb_enc_mbclen(p, e, enc))
84 # define Inc(p, e, enc) ((p) = Next(p, e, enc))
86 static int
87 char_casecmp(const char *p1, const char *p2, rb_encoding *enc, const int nocase)
89 const char *p1end, *p2end;
90 int c1, c2;
92 if (!*p1 || !*p2) return !!*p1 - !!*p2;
93 p1end = p1 + strlen(p1);
94 p2end = p2 + strlen(p2);
95 c1 = rb_enc_codepoint(p1, p1end, enc);
96 c2 = rb_enc_codepoint(p2, p2end, enc);
98 if (c1 == c2) return 0;
99 if (nocase) {
100 c1 = rb_enc_toupper(c1, enc);
101 c2 = rb_enc_toupper(c2, enc);
103 return c1 - c2;
106 static char *
107 bracket(
108 const char *p, /* pattern (next to '[') */
109 const char *s, /* string */
110 int flags,
111 rb_encoding *enc)
113 const char *pend = p + strlen(p);
114 const int nocase = flags & FNM_CASEFOLD;
115 const int escape = !(flags & FNM_NOESCAPE);
117 int ok = 0, not = 0;
119 if (*p == '!' || *p == '^') {
120 not = 1;
121 p++;
124 while (*p != ']') {
125 const char *t1 = p;
126 if (escape && *t1 == '\\')
127 t1++;
128 if (!*t1)
129 return NULL;
130 p = Next(t1, pend, enc);
131 if (p[0] == '-' && p[1] != ']') {
132 const char *t2 = p + 1;
133 if (escape && *t2 == '\\')
134 t2++;
135 if (!*t2)
136 return NULL;
137 p = Next(t2, pend, enc);
138 if (!ok && char_casecmp(t1, s, enc, nocase) <= 0 && char_casecmp(s, t2, enc, nocase) <= 0)
139 ok = 1;
141 else
142 if (!ok && char_casecmp(t1, s, enc, nocase) == 0)
143 ok = 1;
146 return ok == not ? NULL : (char *)p + 1;
149 /* If FNM_PATHNAME is set, only path element will be matched. (upto '/' or '\0')
150 Otherwise, entire string will be matched.
151 End marker itself won't be compared.
152 And if function succeeds, *pcur reaches end marker.
154 #define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
155 #define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
156 #define RETURN(val) return *pcur = p, *scur = s, (val);
158 static int
159 fnmatch_helper(
160 const char **pcur, /* pattern */
161 const char **scur, /* string */
162 int flags,
163 rb_encoding *enc)
165 const int period = !(flags & FNM_DOTMATCH);
166 const int pathname = flags & FNM_PATHNAME;
167 const int escape = !(flags & FNM_NOESCAPE);
168 const int nocase = flags & FNM_CASEFOLD;
170 const char *ptmp = 0;
171 const char *stmp = 0;
173 const char *p = *pcur;
174 const char *pend = p + strlen(p);
175 const char *s = *scur;
176 const char *send = s + strlen(s);
178 if (period && *s == '.' && *UNESCAPE(p) != '.') /* leading period */
179 RETURN(FNM_NOMATCH);
181 while (1) {
182 switch (*p) {
183 case '*':
184 do { p++; } while (*p == '*');
185 if (ISEND(UNESCAPE(p))) {
186 p = UNESCAPE(p);
187 RETURN(0);
189 if (ISEND(s))
190 RETURN(FNM_NOMATCH);
191 ptmp = p;
192 stmp = s;
193 continue;
195 case '?':
196 if (ISEND(s))
197 RETURN(FNM_NOMATCH);
198 p++;
199 Inc(s, send, enc);
200 continue;
202 case '[': {
203 const char *t;
204 if (ISEND(s))
205 RETURN(FNM_NOMATCH);
206 if ((t = bracket(p + 1, s, flags, enc)) != 0) {
207 p = t;
208 Inc(s, send, enc);
209 continue;
211 goto failed;
215 /* ordinary */
216 p = UNESCAPE(p);
217 if (ISEND(s))
218 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
219 if (ISEND(p))
220 goto failed;
221 if (char_casecmp(p, s, enc, nocase) != 0)
222 goto failed;
223 Inc(p, pend, enc);
224 Inc(s, send, enc);
225 continue;
227 failed: /* try next '*' position */
228 if (ptmp && stmp) {
229 p = ptmp;
230 Inc(stmp, send, enc); /* !ISEND(*stmp) */
231 s = stmp;
232 continue;
234 RETURN(FNM_NOMATCH);
238 static int
239 fnmatch(
240 const char *pattern,
241 rb_encoding *enc,
242 const char *string,
243 int flags)
245 const char *p = pattern;
246 const char *s = string;
247 const char *send = s + strlen(string);
248 const int period = !(flags & FNM_DOTMATCH);
249 const int pathname = flags & FNM_PATHNAME;
251 const char *ptmp = 0;
252 const char *stmp = 0;
254 if (pathname) {
255 while (1) {
256 if (p[0] == '*' && p[1] == '*' && p[2] == '/') {
257 do { p += 3; } while (p[0] == '*' && p[1] == '*' && p[2] == '/');
258 ptmp = p;
259 stmp = s;
261 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
262 while (*s && *s != '/') Inc(s, send, enc);
263 if (*p && *s) {
264 p++;
265 s++;
266 continue;
268 if (!*p && !*s)
269 return 0;
271 /* failed : try next recursion */
272 if (ptmp && stmp && !(period && *stmp == '.')) {
273 while (*stmp && *stmp != '/') Inc(stmp, send, enc);
274 if (*stmp) {
275 p = ptmp;
276 stmp++;
277 s = stmp;
278 continue;
281 return FNM_NOMATCH;
284 else
285 return fnmatch_helper(&p, &s, flags, enc);
288 VALUE rb_cDir;
290 struct dir_data {
291 DIR *dir;
292 VALUE path;
293 rb_encoding *extenc;
296 static void
297 mark_dir(struct dir_data *dir)
299 rb_gc_mark(dir->path);
302 static void
303 free_dir(struct dir_data *dir)
305 if (dir) {
306 if (dir->dir) closedir(dir->dir);
308 xfree(dir);
311 static VALUE dir_close(VALUE);
313 static VALUE
314 dir_s_alloc(VALUE klass)
316 struct dir_data *dirp;
317 VALUE obj = Data_Make_Struct(klass, struct dir_data, mark_dir, free_dir, dirp);
319 dirp->dir = NULL;
320 dirp->path = Qnil;
321 dirp->extenc = NULL;
323 return obj;
327 * call-seq:
328 * Dir.new( string ) -> aDir
330 * Returns a new directory object for the named directory.
332 static VALUE
333 dir_initialize(int argc, VALUE *argv, VALUE dir)
335 struct dir_data *dp;
336 static rb_encoding *fs_encoding;
337 rb_encoding *extencoding;
338 VALUE dirname, opt;
339 static VALUE sym_extenc;
341 if (!sym_extenc) {
342 sym_extenc = ID2SYM(rb_intern("external_encoding"));
343 fs_encoding = rb_filesystem_encoding();
346 extencoding = fs_encoding;
347 rb_scan_args(argc, argv, "11", &dirname, &opt);
349 if (!NIL_P(opt)) {
350 VALUE v, extenc=Qnil;
351 opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
353 v = rb_hash_aref(opt, sym_extenc);
354 if (!NIL_P(v)) extenc = v;
356 if (!NIL_P(extenc)) {
357 extencoding = rb_to_encoding(extenc);
361 FilePathValue(dirname);
363 Data_Get_Struct(dir, struct dir_data, dp);
364 if (dp->dir) closedir(dp->dir);
365 dp->dir = NULL;
366 dp->path = Qnil;
367 dp->extenc = extencoding;
368 dp->dir = opendir(RSTRING_PTR(dirname));
369 if (dp->dir == NULL) {
370 if (errno == EMFILE || errno == ENFILE) {
371 rb_gc();
372 dp->dir = opendir(RSTRING_PTR(dirname));
374 if (dp->dir == NULL) {
375 rb_sys_fail(RSTRING_PTR(dirname));
378 dp->path = rb_str_dup_frozen(dirname);
380 return dir;
384 * call-seq:
385 * Dir.open( string ) => aDir
386 * Dir.open( string ) {| aDir | block } => anObject
388 * With no block, <code>open</code> is a synonym for
389 * <code>Dir::new</code>. If a block is present, it is passed
390 * <i>aDir</i> as a parameter. The directory is closed at the end of
391 * the block, and <code>Dir::open</code> returns the value of the
392 * block.
394 static VALUE
395 dir_s_open(int argc, VALUE *argv, VALUE klass)
397 struct dir_data *dp;
398 VALUE dir = Data_Make_Struct(klass, struct dir_data, mark_dir, free_dir, dp);
400 dir_initialize(argc, argv, dir);
401 if (rb_block_given_p()) {
402 return rb_ensure(rb_yield, dir, dir_close, dir);
405 return dir;
408 static void
409 dir_closed(void)
411 rb_raise(rb_eIOError, "closed directory");
414 static void
415 dir_check(VALUE dir)
417 if (!OBJ_TAINTED(dir) && rb_safe_level() >= 4)
418 rb_raise(rb_eSecurityError, "Insecure: operation on untainted Dir");
419 rb_check_frozen(dir);
422 #define GetDIR(obj, dirp) do {\
423 dir_check(dir);\
424 Data_Get_Struct(obj, struct dir_data, dirp);\
425 if (dirp->dir == NULL) dir_closed();\
426 } while (0)
428 static VALUE
429 dir_enc_str(VALUE str, struct dir_data *dirp)
431 rb_enc_associate(str, dirp->extenc);
432 return str;
436 * call-seq:
437 * dir.inspect => string
439 * Return a string describing this Dir object.
441 static VALUE
442 dir_inspect(VALUE dir)
444 struct dir_data *dirp;
446 Data_Get_Struct(dir, struct dir_data, dirp);
447 if (!NIL_P(dirp->path)) {
448 const char *c = rb_obj_classname(dir);
449 return rb_sprintf("#<%s:%s>", c, RSTRING_PTR(dirp->path));
451 return rb_funcall(dir, rb_intern("to_s"), 0, 0);
455 * call-seq:
456 * dir.path => string or nil
458 * Returns the path parameter passed to <em>dir</em>'s constructor.
460 * d = Dir.new("..")
461 * d.path #=> ".."
463 static VALUE
464 dir_path(VALUE dir)
466 struct dir_data *dirp;
468 Data_Get_Struct(dir, struct dir_data, dirp);
469 if (NIL_P(dirp->path)) return Qnil;
470 return rb_str_dup(dirp->path);
474 * call-seq:
475 * dir.read => string or nil
477 * Reads the next entry from <em>dir</em> and returns it as a string.
478 * Returns <code>nil</code> at the end of the stream.
480 * d = Dir.new("testdir")
481 * d.read #=> "."
482 * d.read #=> ".."
483 * d.read #=> "config.h"
485 static VALUE
486 dir_read(VALUE dir)
488 struct dir_data *dirp;
489 struct dirent *dp;
491 GetDIR(dir, dirp);
492 errno = 0;
493 dp = readdir(dirp->dir);
494 if (dp) {
495 return dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp);
497 else if (errno == 0) { /* end of stream */
498 return Qnil;
500 else {
501 rb_sys_fail(0);
503 return Qnil; /* not reached */
507 * call-seq:
508 * dir.each { |filename| block } => dir
510 * Calls the block once for each entry in this directory, passing the
511 * filename of each entry as a parameter to the block.
513 * d = Dir.new("testdir")
514 * d.each {|x| puts "Got #{x}" }
516 * <em>produces:</em>
518 * Got .
519 * Got ..
520 * Got config.h
521 * Got main.rb
523 static VALUE
524 dir_each(VALUE dir)
526 struct dir_data *dirp;
527 struct dirent *dp;
529 RETURN_ENUMERATOR(dir, 0, 0);
530 GetDIR(dir, dirp);
531 rewinddir(dirp->dir);
532 for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
533 rb_yield(dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp));
534 if (dirp->dir == NULL) dir_closed();
536 return dir;
540 * call-seq:
541 * dir.pos => integer
542 * dir.tell => integer
544 * Returns the current position in <em>dir</em>. See also
545 * <code>Dir#seek</code>.
547 * d = Dir.new("testdir")
548 * d.tell #=> 0
549 * d.read #=> "."
550 * d.tell #=> 12
552 static VALUE
553 dir_tell(VALUE dir)
555 #ifdef HAVE_TELLDIR
556 struct dir_data *dirp;
557 long pos;
559 GetDIR(dir, dirp);
560 pos = telldir(dirp->dir);
561 return rb_int2inum(pos);
562 #else
563 rb_notimplement();
564 #endif
568 * call-seq:
569 * dir.seek( integer ) => dir
571 * Seeks to a particular location in <em>dir</em>. <i>integer</i>
572 * must be a value returned by <code>Dir#tell</code>.
574 * d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
575 * d.read #=> "."
576 * i = d.tell #=> 12
577 * d.read #=> ".."
578 * d.seek(i) #=> #<Dir:0x401b3c40>
579 * d.read #=> ".."
581 static VALUE
582 dir_seek(VALUE dir, VALUE pos)
584 struct dir_data *dirp;
585 off_t p = NUM2OFFT(pos);
587 GetDIR(dir, dirp);
588 #ifdef HAVE_SEEKDIR
589 seekdir(dirp->dir, p);
590 return dir;
591 #else
592 rb_notimplement();
593 #endif
597 * call-seq:
598 * dir.pos( integer ) => integer
600 * Synonym for <code>Dir#seek</code>, but returns the position
601 * parameter.
603 * d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
604 * d.read #=> "."
605 * i = d.pos #=> 12
606 * d.read #=> ".."
607 * d.pos = i #=> 12
608 * d.read #=> ".."
610 static VALUE
611 dir_set_pos(VALUE dir, VALUE pos)
613 dir_seek(dir, pos);
614 return pos;
618 * call-seq:
619 * dir.rewind => dir
621 * Repositions <em>dir</em> to the first entry.
623 * d = Dir.new("testdir")
624 * d.read #=> "."
625 * d.rewind #=> #<Dir:0x401b3fb0>
626 * d.read #=> "."
628 static VALUE
629 dir_rewind(VALUE dir)
631 struct dir_data *dirp;
633 if (rb_safe_level() >= 4 && !OBJ_TAINTED(dir)) {
634 rb_raise(rb_eSecurityError, "Insecure: can't close");
636 GetDIR(dir, dirp);
637 rewinddir(dirp->dir);
638 return dir;
642 * call-seq:
643 * dir.close => nil
645 * Closes the directory stream. Any further attempts to access
646 * <em>dir</em> will raise an <code>IOError</code>.
648 * d = Dir.new("testdir")
649 * d.close #=> nil
651 static VALUE
652 dir_close(VALUE dir)
654 struct dir_data *dirp;
656 GetDIR(dir, dirp);
657 closedir(dirp->dir);
658 dirp->dir = NULL;
660 return Qnil;
663 static void
664 dir_chdir(VALUE path)
666 if (chdir(RSTRING_PTR(path)) < 0)
667 rb_sys_fail(RSTRING_PTR(path));
670 static int chdir_blocking = 0;
671 static VALUE chdir_thread = Qnil;
673 struct chdir_data {
674 VALUE old_path, new_path;
675 int done;
678 static VALUE
679 chdir_yield(struct chdir_data *args)
681 dir_chdir(args->new_path);
682 args->done = Qtrue;
683 chdir_blocking++;
684 if (chdir_thread == Qnil)
685 chdir_thread = rb_thread_current();
686 return rb_yield(args->new_path);
689 static VALUE
690 chdir_restore(struct chdir_data *args)
692 if (args->done) {
693 chdir_blocking--;
694 if (chdir_blocking == 0)
695 chdir_thread = Qnil;
696 dir_chdir(args->old_path);
698 return Qnil;
702 * call-seq:
703 * Dir.chdir( [ string] ) => 0
704 * Dir.chdir( [ string] ) {| path | block } => anObject
706 * Changes the current working directory of the process to the given
707 * string. When called without an argument, changes the directory to
708 * the value of the environment variable <code>HOME</code>, or
709 * <code>LOGDIR</code>. <code>SystemCallError</code> (probably
710 * <code>Errno::ENOENT</code>) if the target directory does not exist.
712 * If a block is given, it is passed the name of the new current
713 * directory, and the block is executed with that as the current
714 * directory. The original working directory is restored when the block
715 * exits. The return value of <code>chdir</code> is the value of the
716 * block. <code>chdir</code> blocks can be nested, but in a
717 * multi-threaded program an error will be raised if a thread attempts
718 * to open a <code>chdir</code> block while another thread has one
719 * open.
721 * Dir.chdir("/var/spool/mail")
722 * puts Dir.pwd
723 * Dir.chdir("/tmp") do
724 * puts Dir.pwd
725 * Dir.chdir("/usr") do
726 * puts Dir.pwd
727 * end
728 * puts Dir.pwd
729 * end
730 * puts Dir.pwd
732 * <em>produces:</em>
734 * /var/spool/mail
735 * /tmp
736 * /usr
737 * /tmp
738 * /var/spool/mail
740 static VALUE
741 dir_s_chdir(int argc, VALUE *argv, VALUE obj)
743 VALUE path = Qnil;
745 rb_secure(2);
746 if (rb_scan_args(argc, argv, "01", &path) == 1) {
747 FilePathValue(path);
749 else {
750 const char *dist = getenv("HOME");
751 if (!dist) {
752 dist = getenv("LOGDIR");
753 if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
755 path = rb_str_new2(dist);
758 if (chdir_blocking > 0) {
759 if (!rb_block_given_p() || rb_thread_current() != chdir_thread)
760 rb_warn("conflicting chdir during another chdir block");
763 if (rb_block_given_p()) {
764 struct chdir_data args;
765 char *cwd = my_getcwd();
767 args.old_path = rb_tainted_str_new2(cwd); xfree(cwd);
768 args.new_path = path;
769 args.done = Qfalse;
770 return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args);
772 dir_chdir(path);
774 return INT2FIX(0);
778 * call-seq:
779 * Dir.getwd => string
780 * Dir.pwd => string
782 * Returns the path to the current working directory of this process as
783 * a string.
785 * Dir.chdir("/tmp") #=> 0
786 * Dir.getwd #=> "/tmp"
788 static VALUE
789 dir_s_getwd(VALUE dir)
791 char *path;
792 VALUE cwd;
794 rb_secure(4);
795 path = my_getcwd();
796 cwd = rb_tainted_str_new2(path);
798 xfree(path);
799 return cwd;
802 static void
803 check_dirname(volatile VALUE *dir)
805 char *path, *pend;
807 rb_secure(2);
808 FilePathValue(*dir);
809 path = RSTRING_PTR(*dir);
810 if (path && *(pend = rb_path_end(rb_path_skip_prefix(path)))) {
811 *dir = rb_str_new(path, pend - path);
816 * call-seq:
817 * Dir.chroot( string ) => 0
819 * Changes this process's idea of the file system root. Only a
820 * privileged process may make this call. Not available on all
821 * platforms. On Unix systems, see <code>chroot(2)</code> for more
822 * information.
824 static VALUE
825 dir_s_chroot(VALUE dir, VALUE path)
827 #if defined(HAVE_CHROOT) && !defined(__CHECKER__)
828 check_dirname(&path);
830 if (chroot(RSTRING_PTR(path)) == -1)
831 rb_sys_fail(RSTRING_PTR(path));
833 return INT2FIX(0);
834 #else
835 rb_notimplement();
836 return Qnil; /* not reached */
837 #endif
841 * call-seq:
842 * Dir.mkdir( string [, integer] ) => 0
844 * Makes a new directory named by <i>string</i>, with permissions
845 * specified by the optional parameter <i>anInteger</i>. The
846 * permissions may be modified by the value of
847 * <code>File::umask</code>, and are ignored on NT. Raises a
848 * <code>SystemCallError</code> if the directory cannot be created. See
849 * also the discussion of permissions in the class documentation for
850 * <code>File</code>.
853 static VALUE
854 dir_s_mkdir(int argc, VALUE *argv, VALUE obj)
856 VALUE path, vmode;
857 int mode;
859 if (rb_scan_args(argc, argv, "11", &path, &vmode) == 2) {
860 mode = NUM2INT(vmode);
862 else {
863 mode = 0777;
866 check_dirname(&path);
867 if (mkdir(RSTRING_PTR(path), mode) == -1)
868 rb_sys_fail(RSTRING_PTR(path));
870 return INT2FIX(0);
874 * call-seq:
875 * Dir.delete( string ) => 0
876 * Dir.rmdir( string ) => 0
877 * Dir.unlink( string ) => 0
879 * Deletes the named directory. Raises a subclass of
880 * <code>SystemCallError</code> if the directory isn't empty.
882 static VALUE
883 dir_s_rmdir(VALUE obj, VALUE dir)
885 check_dirname(&dir);
886 if (rmdir(RSTRING_PTR(dir)) < 0)
887 rb_sys_fail(RSTRING_PTR(dir));
889 return INT2FIX(0);
892 static void
893 sys_warning_1(const char* mesg)
895 rb_sys_warning("%s", mesg);
898 #define GLOB_VERBOSE (1UL << (sizeof(int) * CHAR_BIT - 1))
899 #define sys_warning(val) \
900 (void)((flags & GLOB_VERBOSE) && rb_protect((VALUE (*)(VALUE))sys_warning_1, (VALUE)(val), 0))
902 #define GLOB_ALLOC(type) (type *)malloc(sizeof(type))
903 #define GLOB_ALLOC_N(type, n) (type *)malloc(sizeof(type) * (n))
904 #define GLOB_FREE(ptr) free(ptr)
905 #define GLOB_JUMP_TAG(status) ((status == -1) ? rb_memerror() : rb_jump_tag(status))
908 * ENOTDIR can be returned by stat(2) if a non-leaf element of the path
909 * is not a directory.
911 #define to_be_ignored(e) ((e) == ENOENT || (e) == ENOTDIR)
913 /* System call with warning */
914 static int
915 do_stat(const char *path, struct stat *pst, int flags)
918 int ret = stat(path, pst);
919 if (ret < 0 && !to_be_ignored(errno))
920 sys_warning(path);
922 return ret;
925 static int
926 do_lstat(const char *path, struct stat *pst, int flags)
928 int ret = lstat(path, pst);
929 if (ret < 0 && !to_be_ignored(errno))
930 sys_warning(path);
932 return ret;
935 static DIR *
936 do_opendir(const char *path, int flags)
938 DIR *dirp = opendir(path);
939 if (dirp == NULL && !to_be_ignored(errno))
940 sys_warning(path);
942 return dirp;
945 /* Return nonzero if S has any special globbing chars in it. */
946 static int
947 has_magic(const char *s, int flags, rb_encoding *enc)
949 const int escape = !(flags & FNM_NOESCAPE);
950 const int nocase = flags & FNM_CASEFOLD;
952 register const char *p = s;
953 register const char *pend = p + strlen(p);
954 register char c;
956 while ((c = *p++) != 0) {
957 switch (c) {
958 case '*':
959 case '?':
960 case '[':
961 return 1;
963 case '\\':
964 if (escape && !(c = *p++))
965 return 0;
966 continue;
968 default:
969 if (!FNM_SYSCASE && ISALPHA(c) && nocase)
970 return 1;
973 p = Next(p-1, pend, enc);
976 return 0;
979 /* Find separator in globbing pattern. */
980 static char *
981 find_dirsep(const char *s, int flags, rb_encoding *enc)
983 const int escape = !(flags & FNM_NOESCAPE);
985 register const char *p = s;
986 register const char *pend = p + strlen(p);
987 register char c;
988 int open = 0;
990 while ((c = *p++) != 0) {
991 switch (c) {
992 case '[':
993 open = 1;
994 continue;
995 case ']':
996 open = 0;
997 continue;
999 case '/':
1000 if (!open)
1001 return (char *)p-1;
1002 continue;
1004 case '\\':
1005 if (escape && !(c = *p++))
1006 return (char *)p-1;
1007 continue;
1010 p = Next(p-1, pend, enc);
1013 return (char *)p-1;
1016 /* Remove escaping backslashes */
1017 static void
1018 remove_backslashes(char *p, rb_encoding *enc)
1020 register const char *pend = p + strlen(p);
1021 char *t = p;
1022 char *s = p;
1024 while (*p) {
1025 if (*p == '\\') {
1026 if (t != s)
1027 memmove(t, s, p - s);
1028 t += p - s;
1029 s = ++p;
1030 if (!*p) break;
1032 Inc(p, pend, enc);
1035 while (*p++);
1037 if (t != s)
1038 memmove(t, s, p - s); /* move '\0' too */
1041 /* Globing pattern */
1042 enum glob_pattern_type { PLAIN, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1044 struct glob_pattern {
1045 char *str;
1046 enum glob_pattern_type type;
1047 struct glob_pattern *next;
1050 static void glob_free_pattern(struct glob_pattern *list);
1052 static struct glob_pattern *
1053 glob_make_pattern(const char *p, int flags, rb_encoding *enc)
1055 struct glob_pattern *list, *tmp, **tail = &list;
1056 int dirsep = 0; /* pattern is terminated with '/' */
1058 while (*p) {
1059 tmp = GLOB_ALLOC(struct glob_pattern);
1060 if (!tmp) goto error;
1061 if (p[0] == '*' && p[1] == '*' && p[2] == '/') {
1062 /* fold continuous RECURSIVEs (needed in glob_helper) */
1063 do { p += 3; } while (p[0] == '*' && p[1] == '*' && p[2] == '/');
1064 tmp->type = RECURSIVE;
1065 tmp->str = 0;
1066 dirsep = 1;
1068 else {
1069 const char *m = find_dirsep(p, flags, enc);
1070 char *buf = GLOB_ALLOC_N(char, m-p+1);
1071 if (!buf) {
1072 GLOB_FREE(tmp);
1073 goto error;
1075 memcpy(buf, p, m-p);
1076 buf[m-p] = '\0';
1077 tmp->type = has_magic(buf, flags, enc) ? MAGICAL : PLAIN;
1078 tmp->str = buf;
1079 if (*m) {
1080 dirsep = 1;
1081 p = m + 1;
1083 else {
1084 dirsep = 0;
1085 p = m;
1088 *tail = tmp;
1089 tail = &tmp->next;
1092 tmp = GLOB_ALLOC(struct glob_pattern);
1093 if (!tmp) {
1094 error:
1095 *tail = 0;
1096 glob_free_pattern(list);
1097 return 0;
1099 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
1100 tmp->str = 0;
1101 *tail = tmp;
1102 tmp->next = 0;
1104 return list;
1107 static void
1108 glob_free_pattern(struct glob_pattern *list)
1110 while (list) {
1111 struct glob_pattern *tmp = list;
1112 list = list->next;
1113 if (tmp->str)
1114 GLOB_FREE(tmp->str);
1115 GLOB_FREE(tmp);
1119 static char *
1120 join_path(const char *path, int dirsep, const char *name)
1122 long len = strlen(path);
1123 char *buf = GLOB_ALLOC_N(char, len+strlen(name)+(dirsep?1:0)+1);
1125 if (!buf) return 0;
1126 memcpy(buf, path, len);
1127 if (dirsep) {
1128 strcpy(buf+len, "/");
1129 len++;
1131 strcpy(buf+len, name);
1132 return buf;
1135 enum answer { YES, NO, UNKNOWN };
1137 #ifndef S_ISDIR
1138 # define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
1139 #endif
1141 #ifndef S_ISLNK
1142 # ifndef S_IFLNK
1143 # define S_ISLNK(m) (0)
1144 # else
1145 # define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
1146 # endif
1147 #endif
1149 struct glob_args {
1150 void (*func)(const char *, VALUE, void *);
1151 const char *path;
1152 VALUE value;
1153 rb_encoding *enc;
1156 static VALUE
1157 glob_func_caller(VALUE val)
1159 struct glob_args *args = (struct glob_args *)val;
1161 (*args->func)(args->path, args->value, args->enc);
1162 return Qnil;
1165 #define glob_call_func(func, path, arg, enc) (*func)(path, arg, enc)
1167 static int
1168 glob_helper(
1169 const char *path,
1170 int dirsep, /* '/' should be placed before appending child entry's name to 'path'. */
1171 enum answer exist, /* Does 'path' indicate an existing entry? */
1172 enum answer isdir, /* Does 'path' indicate a directory or a symlink to a directory? */
1173 struct glob_pattern **beg,
1174 struct glob_pattern **end,
1175 int flags,
1176 ruby_glob_func *func,
1177 VALUE arg,
1178 rb_encoding *enc)
1180 struct stat st;
1181 int status = 0;
1182 struct glob_pattern **cur, **new_beg, **new_end;
1183 int plain = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
1184 int escape = !(flags & FNM_NOESCAPE);
1186 for (cur = beg; cur < end; ++cur) {
1187 struct glob_pattern *p = *cur;
1188 if (p->type == RECURSIVE) {
1189 recursive = 1;
1190 p = p->next;
1192 switch (p->type) {
1193 case PLAIN:
1194 plain = 1;
1195 break;
1196 case MAGICAL:
1197 magical = 1;
1198 break;
1199 case MATCH_ALL:
1200 match_all = 1;
1201 break;
1202 case MATCH_DIR:
1203 match_dir = 1;
1204 break;
1205 case RECURSIVE:
1206 rb_bug("continuous RECURSIVEs");
1210 if (*path) {
1211 if (match_all && exist == UNKNOWN) {
1212 if (do_lstat(path, &st, flags) == 0) {
1213 exist = YES;
1214 isdir = S_ISDIR(st.st_mode) ? YES : S_ISLNK(st.st_mode) ? UNKNOWN : NO;
1216 else {
1217 exist = NO;
1218 isdir = NO;
1221 if (match_dir && isdir == UNKNOWN) {
1222 if (do_stat(path, &st, flags) == 0) {
1223 exist = YES;
1224 isdir = S_ISDIR(st.st_mode) ? YES : NO;
1226 else {
1227 exist = NO;
1228 isdir = NO;
1231 if (match_all && exist == YES) {
1232 status = glob_call_func(func, path, arg, enc);
1233 if (status) return status;
1235 if (match_dir && isdir == YES) {
1236 char *tmp = join_path(path, dirsep, "");
1237 if (!tmp) return -1;
1238 status = glob_call_func(func, tmp, arg, enc);
1239 GLOB_FREE(tmp);
1240 if (status) return status;
1244 if (exist == NO || isdir == NO) return 0;
1246 if (magical || recursive) {
1247 struct dirent *dp;
1248 DIR *dirp = do_opendir(*path ? path : ".", flags);
1249 if (dirp == NULL) return 0;
1251 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1252 char *buf = join_path(path, dirsep, dp->d_name);
1253 enum answer new_isdir = UNKNOWN;
1255 if (!buf) {
1256 status = -1;
1257 break;
1259 if (recursive && strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0
1260 && fnmatch("*", rb_usascii_encoding(), dp->d_name, flags) == 0) {
1261 #ifndef _WIN32
1262 if (do_lstat(buf, &st, flags) == 0)
1263 new_isdir = S_ISDIR(st.st_mode) ? YES : S_ISLNK(st.st_mode) ? UNKNOWN : NO;
1264 else
1265 new_isdir = NO;
1266 #else
1267 new_isdir = dp->d_isdir ? (!dp->d_isrep ? YES : UNKNOWN) : NO;
1268 #endif
1271 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, (end - beg) * 2);
1272 if (!new_beg) {
1273 GLOB_FREE(buf);
1274 status = -1;
1275 break;
1278 for (cur = beg; cur < end; ++cur) {
1279 struct glob_pattern *p = *cur;
1280 if (p->type == RECURSIVE) {
1281 if (new_isdir == YES) /* not symlink but real directory */
1282 *new_end++ = p; /* append recursive pattern */
1283 p = p->next; /* 0 times recursion */
1285 if (p->type == PLAIN || p->type == MAGICAL) {
1286 if (fnmatch(p->str, enc, dp->d_name, flags) == 0)
1287 *new_end++ = p->next;
1291 status = glob_helper(buf, 1, YES, new_isdir, new_beg, new_end,
1292 flags, func, arg, enc);
1293 GLOB_FREE(buf);
1294 GLOB_FREE(new_beg);
1295 if (status) break;
1298 closedir(dirp);
1300 else if (plain) {
1301 struct glob_pattern **copy_beg, **copy_end, **cur2;
1303 copy_beg = copy_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
1304 if (!copy_beg) return -1;
1305 for (cur = beg; cur < end; ++cur)
1306 *copy_end++ = (*cur)->type == PLAIN ? *cur : 0;
1308 for (cur = copy_beg; cur < copy_end; ++cur) {
1309 if (*cur) {
1310 char *buf;
1311 char *name;
1312 name = GLOB_ALLOC_N(char, strlen((*cur)->str) + 1);
1313 if (!name) {
1314 status = -1;
1315 break;
1317 strcpy(name, (*cur)->str);
1318 if (escape) remove_backslashes(name, enc);
1320 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
1321 if (!new_beg) {
1322 GLOB_FREE(name);
1323 status = -1;
1324 break;
1326 *new_end++ = (*cur)->next;
1327 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
1328 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
1329 *new_end++ = (*cur2)->next;
1330 *cur2 = 0;
1334 buf = join_path(path, dirsep, name);
1335 GLOB_FREE(name);
1336 if (!buf) {
1337 GLOB_FREE(new_beg);
1338 status = -1;
1339 break;
1341 status = glob_helper(buf, 1, UNKNOWN, UNKNOWN, new_beg,
1342 new_end, flags, func, arg, enc);
1343 GLOB_FREE(buf);
1344 GLOB_FREE(new_beg);
1345 if (status) break;
1349 GLOB_FREE(copy_beg);
1352 return status;
1355 static int
1356 ruby_glob0(const char *path, int flags, ruby_glob_func *func, VALUE arg, rb_encoding *enc)
1358 struct glob_pattern *list;
1359 const char *root, *start;
1360 char *buf;
1361 int n;
1362 int status;
1364 start = root = path;
1365 flags |= FNM_SYSCASE;
1366 #if defined DOSISH
1367 root = rb_path_skip_prefix(root);
1368 #endif
1370 if (root && *root == '/') root++;
1372 n = root - start;
1373 buf = GLOB_ALLOC_N(char, n + 1);
1374 if (!buf) return -1;
1375 MEMCPY(buf, start, char, n);
1376 buf[n] = '\0';
1378 list = glob_make_pattern(root, flags, enc);
1379 if (!list) {
1380 GLOB_FREE(buf);
1381 return -1;
1383 status = glob_helper(buf, 0, UNKNOWN, UNKNOWN, &list, &list + 1, flags, func, arg, enc);
1384 glob_free_pattern(list);
1385 GLOB_FREE(buf);
1387 return status;
1391 ruby_glob(const char *path, int flags, ruby_glob_func *func, VALUE arg)
1393 return ruby_glob0(path, flags & ~GLOB_VERBOSE, func, arg,
1394 rb_ascii8bit_encoding());
1397 static int
1398 rb_glob_caller(const char *path, VALUE a, void *enc)
1400 int status;
1401 struct glob_args *args = (struct glob_args *)a;
1403 args->path = path;
1404 rb_protect(glob_func_caller, a, &status);
1405 return status;
1408 static int
1409 rb_glob2(const char *path, int flags,
1410 void (*func)(const char *, VALUE, void *), VALUE arg,
1411 rb_encoding* enc)
1413 struct glob_args args;
1415 args.func = func;
1416 args.value = arg;
1417 args.enc = enc;
1419 if (flags & FNM_SYSCASE) {
1420 rb_warning("Dir.glob() ignores File::FNM_CASEFOLD");
1423 return ruby_glob0(path, flags | GLOB_VERBOSE, rb_glob_caller, (VALUE)&args,
1424 enc);
1427 void
1428 rb_glob(const char *path, void (*func)(const char *, VALUE, void *), VALUE arg)
1430 int status = rb_glob2(path, 0, func, arg, rb_ascii8bit_encoding());
1431 if (status) GLOB_JUMP_TAG(status);
1434 static void
1435 push_pattern(const char *path, VALUE ary, void *enc)
1437 VALUE vpath = rb_tainted_str_new2(path);
1438 rb_enc_associate(vpath, enc);
1439 rb_ary_push(ary, vpath);
1442 static int
1443 ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
1444 rb_encoding *enc)
1446 const int escape = !(flags & FNM_NOESCAPE);
1447 const char *p = str;
1448 const char *pend = p + strlen(p);
1449 const char *s = p;
1450 const char *lbrace = 0, *rbrace = 0;
1451 int nest = 0, status = 0;
1453 while (*p) {
1454 if (*p == '{' && nest++ == 0) {
1455 lbrace = p;
1457 if (*p == '}' && --nest <= 0) {
1458 rbrace = p;
1459 break;
1461 if (*p == '\\' && escape) {
1462 if (!*++p) break;
1464 Inc(p, pend, enc);
1467 if (lbrace && rbrace) {
1468 char *buf = GLOB_ALLOC_N(char, strlen(s) + 1);
1469 long shift;
1471 if (!buf) return -1;
1472 memcpy(buf, s, lbrace-s);
1473 shift = (lbrace-s);
1474 p = lbrace;
1475 while (p < rbrace) {
1476 const char *t = ++p;
1477 nest = 0;
1478 while (p < rbrace && !(*p == ',' && nest == 0)) {
1479 if (*p == '{') nest++;
1480 if (*p == '}') nest--;
1481 if (*p == '\\' && escape) {
1482 if (++p == rbrace) break;
1484 Inc(p, pend, enc);
1486 memcpy(buf+shift, t, p-t);
1487 strcpy(buf+shift+(p-t), rbrace+1);
1488 status = ruby_brace_expand(buf, flags, func, arg, enc);
1489 if (status) break;
1491 GLOB_FREE(buf);
1493 else if (!lbrace && !rbrace) {
1494 status = (*func)(s, arg, enc);
1497 return status;
1500 struct brace_args {
1501 ruby_glob_func *func;
1502 VALUE value;
1503 int flags;
1506 static int
1507 glob_brace(const char *path, VALUE val, void *enc)
1509 struct brace_args *arg = (struct brace_args *)val;
1511 return ruby_glob0(path, arg->flags, arg->func, arg->value, enc);
1514 static int
1515 ruby_brace_glob0(const char *str, int flags, ruby_glob_func *func, VALUE arg,
1516 rb_encoding* enc)
1518 struct brace_args args;
1520 args.func = func;
1521 args.value = arg;
1522 args.flags = flags;
1523 return ruby_brace_expand(str, flags, glob_brace, (VALUE)&args, enc);
1527 ruby_brace_glob(const char *str, int flags, ruby_glob_func *func, VALUE arg)
1529 return ruby_brace_glob0(str, flags & ~GLOB_VERBOSE, func, arg,
1530 rb_ascii8bit_encoding());
1533 static int
1534 push_glob(VALUE ary, VALUE str, int flags)
1536 struct glob_args args;
1537 rb_encoding *enc = rb_enc_get(str);
1539 args.func = push_pattern;
1540 args.value = ary;
1541 args.enc = enc;
1543 return ruby_brace_glob0(RSTRING_PTR(str), flags | GLOB_VERBOSE,
1544 rb_glob_caller, (VALUE)&args, enc);
1547 static VALUE
1548 rb_push_glob(VALUE str, int flags) /* '\0' is delimiter */
1550 long offset = 0;
1551 VALUE ary;
1553 StringValue(str);
1554 ary = rb_ary_new();
1556 while (offset < RSTRING_LEN(str)) {
1557 char *p, *pend;
1558 int status;
1559 p = RSTRING_PTR(str) + offset;
1560 status = push_glob(ary, rb_enc_str_new(p, strlen(p), rb_enc_get(str)),
1561 flags);
1562 if (status) GLOB_JUMP_TAG(status);
1563 if (offset >= RSTRING_LEN(str)) break;
1564 p += strlen(p) + 1;
1565 pend = RSTRING_PTR(str) + RSTRING_LEN(str);
1566 while (p < pend && !*p)
1567 p++;
1568 offset = p - RSTRING_PTR(str);
1571 return ary;
1574 static VALUE
1575 dir_globs(long argc, VALUE *argv, int flags)
1577 VALUE ary = rb_ary_new();
1578 long i;
1580 for (i = 0; i < argc; ++i) {
1581 int status;
1582 VALUE str = argv[i];
1583 StringValue(str);
1584 status = push_glob(ary, str, flags);
1585 if (status) GLOB_JUMP_TAG(status);
1588 return ary;
1592 * call-seq:
1593 * Dir[ array ] => array
1594 * Dir[ string [, string ...] ] => array
1596 * Equivalent to calling
1597 * <code>Dir.glob(</code><i>array,</i><code>0)</code> and
1598 * <code>Dir.glob([</code><i>string,...</i><code>],0)</code>.
1601 static VALUE
1602 dir_s_aref(int argc, VALUE *argv, VALUE obj)
1604 if (argc == 1) {
1605 return rb_push_glob(argv[0], 0);
1607 return dir_globs(argc, argv, 0);
1611 * call-seq:
1612 * Dir.glob( pattern, [flags] ) => array
1613 * Dir.glob( pattern, [flags] ) {| filename | block } => nil
1615 * Returns the filenames found by expanding <i>pattern</i> which is
1616 * an +Array+ of the patterns or the pattern +String+, either as an
1617 * <i>array</i> or as parameters to the block. Note that this pattern
1618 * is not a regexp (it's closer to a shell glob). See
1619 * <code>File::fnmatch</code> for the meaning of the <i>flags</i>
1620 * parameter. Note that case sensitivity depends on your system (so
1621 * <code>File::FNM_CASEFOLD</code> is ignored)
1623 * <code>*</code>:: Matches any file. Can be restricted by
1624 * other values in the glob. <code>*</code>
1625 * will match all files; <code>c*</code> will
1626 * match all files beginning with
1627 * <code>c</code>; <code>*c</code> will match
1628 * all files ending with <code>c</code>; and
1629 * <code>*c*</code> will match all files that
1630 * have <code>c</code> in them (including at
1631 * the beginning or end). Equivalent to
1632 * <code>/ .* /x</code> in regexp.
1633 * <code>**</code>:: Matches directories recursively.
1634 * <code>?</code>:: Matches any one character. Equivalent to
1635 * <code>/.{1}/</code> in regexp.
1636 * <code>[set]</code>:: Matches any one character in +set+.
1637 * Behaves exactly like character sets in
1638 * Regexp, including set negation
1639 * (<code>[^a-z]</code>).
1640 * <code>{p,q}</code>:: Matches either literal <code>p</code> or
1641 * literal <code>q</code>. Matching literals
1642 * may be more than one character in length.
1643 * More than two literals may be specified.
1644 * Equivalent to pattern alternation in
1645 * regexp.
1646 * <code>\</code>:: Escapes the next metacharacter.
1648 * Dir["config.?"] #=> ["config.h"]
1649 * Dir.glob("config.?") #=> ["config.h"]
1650 * Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
1651 * Dir.glob("*.[^r]*") #=> ["config.h"]
1652 * Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
1653 * Dir.glob("*") #=> ["config.h", "main.rb"]
1654 * Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"]
1656 * rbfiles = File.join("**", "*.rb")
1657 * Dir.glob(rbfiles) #=> ["main.rb",
1658 * # "lib/song.rb",
1659 * # "lib/song/karaoke.rb"]
1660 * libdirs = File.join("**", "lib")
1661 * Dir.glob(libdirs) #=> ["lib"]
1663 * librbfiles = File.join("**", "lib", "**", "*.rb")
1664 * Dir.glob(librbfiles) #=> ["lib/song.rb",
1665 * # "lib/song/karaoke.rb"]
1667 * librbfiles = File.join("**", "lib", "*.rb")
1668 * Dir.glob(librbfiles) #=> ["lib/song.rb"]
1670 static VALUE
1671 dir_s_glob(int argc, VALUE *argv, VALUE obj)
1673 VALUE str, rflags, ary;
1674 int flags;
1676 if (rb_scan_args(argc, argv, "11", &str, &rflags) == 2)
1677 flags = NUM2INT(rflags);
1678 else
1679 flags = 0;
1681 ary = rb_check_array_type(str);
1682 if (NIL_P(ary)) {
1683 ary = rb_push_glob(str, flags);
1685 else {
1686 volatile VALUE v = ary;
1687 ary = dir_globs(RARRAY_LEN(v), RARRAY_PTR(v), flags);
1690 if (rb_block_given_p()) {
1691 rb_ary_each(ary);
1692 return Qnil;
1694 return ary;
1697 static VALUE
1698 dir_open_dir(int argc, VALUE *argv)
1700 VALUE dir = rb_funcall2(rb_cDir, rb_intern("open"), argc, argv);
1702 if (TYPE(dir) != T_DATA ||
1703 RDATA(dir)->dfree != (RUBY_DATA_FUNC)free_dir) {
1704 rb_raise(rb_eTypeError, "wrong argument type %s (expected Dir)",
1705 rb_obj_classname(dir));
1707 return dir;
1712 * call-seq:
1713 * Dir.foreach( dirname ) {| filename | block } => nil
1715 * Calls the block once for each entry in the named directory, passing
1716 * the filename of each entry as a parameter to the block.
1718 * Dir.foreach("testdir") {|x| puts "Got #{x}" }
1720 * <em>produces:</em>
1722 * Got .
1723 * Got ..
1724 * Got config.h
1725 * Got main.rb
1728 static VALUE
1729 dir_foreach(int argc, VALUE *argv, VALUE io)
1731 VALUE dir;
1733 RETURN_ENUMERATOR(io, argc, argv);
1734 dir = dir_open_dir(argc, argv);
1735 rb_ensure(dir_each, dir, dir_close, dir);
1736 return Qnil;
1740 * call-seq:
1741 * Dir.entries( dirname ) => array
1743 * Returns an array containing all of the filenames in the given
1744 * directory. Will raise a <code>SystemCallError</code> if the named
1745 * directory doesn't exist.
1747 * Dir.entries("testdir") #=> [".", "..", "config.h", "main.rb"]
1750 static VALUE
1751 dir_entries(int argc, VALUE *argv, VALUE io)
1753 VALUE dir;
1755 dir = dir_open_dir(argc, argv);
1756 return rb_ensure(rb_Array, dir, dir_close, dir);
1760 * call-seq:
1761 * File.fnmatch( pattern, path, [flags] ) => (true or false)
1762 * File.fnmatch?( pattern, path, [flags] ) => (true or false)
1764 * Returns true if <i>path</i> matches against <i>pattern</i> The
1765 * pattern is not a regular expression; instead it follows rules
1766 * similar to shell filename globbing. It may contain the following
1767 * metacharacters:
1769 * <code>*</code>:: Matches any file. Can be restricted by
1770 * other values in the glob. <code>*</code>
1771 * will match all files; <code>c*</code> will
1772 * match all files beginning with
1773 * <code>c</code>; <code>*c</code> will match
1774 * all files ending with <code>c</code>; and
1775 * <code>*c*</code> will match all files that
1776 * have <code>c</code> in them (including at
1777 * the beginning or end). Equivalent to
1778 * <code>/ .* /x</code> in regexp.
1779 * <code>**</code>:: Matches directories recursively or files
1780 * expansively.
1781 * <code>?</code>:: Matches any one character. Equivalent to
1782 * <code>/.{1}/</code> in regexp.
1783 * <code>[set]</code>:: Matches any one character in +set+.
1784 * Behaves exactly like character sets in
1785 * Regexp, including set negation
1786 * (<code>[^a-z]</code>).
1787 * <code>\</code>:: Escapes the next metacharacter.
1789 * <i>flags</i> is a bitwise OR of the <code>FNM_xxx</code>
1790 * parameters. The same glob pattern and flags are used by
1791 * <code>Dir::glob</code>.
1793 * File.fnmatch('cat', 'cat') #=> true # match entire string
1794 * File.fnmatch('cat', 'category') #=> false # only match partial string
1795 * File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported
1797 * File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character
1798 * File.fnmatch('c??t', 'cat') #=> false # ditto
1799 * File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters
1800 * File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto
1801 * File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression
1802 * File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!')
1804 * File.fnmatch('cat', 'CAT') #=> false # case sensitive
1805 * File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive
1807 * File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME
1808 * File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto
1809 * File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto
1811 * File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary
1812 * File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary
1813 * File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESACPE makes '\' ordinary
1814 * File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression
1816 * File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading
1817 * File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default.
1818 * File.fnmatch('.*', '.profile') #=> true
1820 * rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
1821 * File.fnmatch(rbfiles, 'main.rb') #=> false
1822 * File.fnmatch(rbfiles, './main.rb') #=> false
1823 * File.fnmatch(rbfiles, 'lib/song.rb') #=> true
1824 * File.fnmatch('**.rb', 'main.rb') #=> true
1825 * File.fnmatch('**.rb', './main.rb') #=> false
1826 * File.fnmatch('**.rb', 'lib/song.rb') #=> true
1827 * File.fnmatch('*', 'dave/.profile') #=> true
1829 * pattern = '*' '/' '*'
1830 * File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
1831 * File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1833 * pattern = '**' '/' 'foo'
1834 * File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
1835 * File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
1836 * File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
1837 * File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
1838 * File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1840 static VALUE
1841 file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
1843 VALUE pattern, path;
1844 VALUE rflags;
1845 int flags;
1847 if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
1848 flags = NUM2INT(rflags);
1849 else
1850 flags = 0;
1852 StringValue(pattern);
1853 FilePathStringValue(path);
1855 if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
1856 flags) == 0)
1857 return Qtrue;
1859 return Qfalse;
1863 * Objects of class <code>Dir</code> are directory streams representing
1864 * directories in the underlying file system. They provide a variety of
1865 * ways to list directories and their contents. See also
1866 * <code>File</code>.
1868 * The directory used in these examples contains the two regular files
1869 * (<code>config.h</code> and <code>main.rb</code>), the parent
1870 * directory (<code>..</code>), and the directory itself
1871 * (<code>.</code>).
1873 void
1874 Init_Dir(void)
1876 rb_cDir = rb_define_class("Dir", rb_cObject);
1878 rb_include_module(rb_cDir, rb_mEnumerable);
1880 rb_define_alloc_func(rb_cDir, dir_s_alloc);
1881 rb_define_singleton_method(rb_cDir, "open", dir_s_open, -1);
1882 rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, -1);
1883 rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
1885 rb_define_method(rb_cDir,"initialize", dir_initialize, -1);
1886 rb_define_method(rb_cDir,"path", dir_path, 0);
1887 rb_define_method(rb_cDir,"inspect", dir_inspect, 0);
1888 rb_define_method(rb_cDir,"read", dir_read, 0);
1889 rb_define_method(rb_cDir,"each", dir_each, 0);
1890 rb_define_method(rb_cDir,"rewind", dir_rewind, 0);
1891 rb_define_method(rb_cDir,"tell", dir_tell, 0);
1892 rb_define_method(rb_cDir,"seek", dir_seek, 1);
1893 rb_define_method(rb_cDir,"pos", dir_tell, 0);
1894 rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
1895 rb_define_method(rb_cDir,"close", dir_close, 0);
1897 rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
1898 rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
1899 rb_define_singleton_method(rb_cDir,"pwd", dir_s_getwd, 0);
1900 rb_define_singleton_method(rb_cDir,"chroot", dir_s_chroot, 1);
1901 rb_define_singleton_method(rb_cDir,"mkdir", dir_s_mkdir, -1);
1902 rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1);
1903 rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1);
1904 rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
1906 rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, -1);
1907 rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, -1);
1908 rb_define_singleton_method(rb_cDir,"exist?", rb_file_directory_p, 1); /* in file.c */
1909 rb_define_singleton_method(rb_cDir,"exists?", rb_file_directory_p, 1); /* in file.c */
1911 rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1);
1912 rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1);
1914 rb_file_const("FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE));
1915 rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
1916 rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
1917 rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
1918 rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE));