* io.c (rb_open_file): encoding in mode string was ignored if perm is
[ruby-svn.git] / dir.c
blobea13490b210962043976a55281c7993590afe1d7
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 rb_encoding *extencoding;
337 VALUE dirname, opt;
338 static VALUE sym_extenc;
340 if (!sym_extenc) {
341 sym_extenc = ID2SYM(rb_intern("external_encoding"));
343 extencoding = rb_filesystem_encoding();
345 rb_scan_args(argc, argv, "11", &dirname, &opt);
347 if (!NIL_P(opt)) {
348 VALUE v, extenc=Qnil;
349 opt = rb_convert_type(opt, T_HASH, "Hash", "to_hash");
351 v = rb_hash_aref(opt, sym_extenc);
352 if (!NIL_P(v)) extenc = v;
354 if (!NIL_P(extenc)) {
355 extencoding = rb_to_encoding(extenc);
359 FilePathValue(dirname);
361 Data_Get_Struct(dir, struct dir_data, dp);
362 if (dp->dir) closedir(dp->dir);
363 dp->dir = NULL;
364 dp->path = Qnil;
365 dp->extenc = extencoding;
366 dp->dir = opendir(RSTRING_PTR(dirname));
367 if (dp->dir == NULL) {
368 if (errno == EMFILE || errno == ENFILE) {
369 rb_gc();
370 dp->dir = opendir(RSTRING_PTR(dirname));
372 if (dp->dir == NULL) {
373 rb_sys_fail(RSTRING_PTR(dirname));
376 dp->path = rb_str_dup_frozen(dirname);
378 return dir;
382 * call-seq:
383 * Dir.open( string ) => aDir
384 * Dir.open( string ) {| aDir | block } => anObject
386 * With no block, <code>open</code> is a synonym for
387 * <code>Dir::new</code>. If a block is present, it is passed
388 * <i>aDir</i> as a parameter. The directory is closed at the end of
389 * the block, and <code>Dir::open</code> returns the value of the
390 * block.
392 static VALUE
393 dir_s_open(int argc, VALUE *argv, VALUE klass)
395 struct dir_data *dp;
396 VALUE dir = Data_Make_Struct(klass, struct dir_data, mark_dir, free_dir, dp);
398 dir_initialize(argc, argv, dir);
399 if (rb_block_given_p()) {
400 return rb_ensure(rb_yield, dir, dir_close, dir);
403 return dir;
406 static void
407 dir_closed(void)
409 rb_raise(rb_eIOError, "closed directory");
412 static void
413 dir_check(VALUE dir)
415 if (!OBJ_UNTRUSTED(dir) && rb_safe_level() >= 4)
416 rb_raise(rb_eSecurityError, "Insecure: operation on trusted Dir");
417 rb_check_frozen(dir);
420 #define GetDIR(obj, dirp) do {\
421 dir_check(dir);\
422 Data_Get_Struct(obj, struct dir_data, dirp);\
423 if (dirp->dir == NULL) dir_closed();\
424 } while (0)
426 static VALUE
427 dir_enc_str(VALUE str, struct dir_data *dirp)
429 rb_enc_associate(str, dirp->extenc);
430 return str;
434 * call-seq:
435 * dir.inspect => string
437 * Return a string describing this Dir object.
439 static VALUE
440 dir_inspect(VALUE dir)
442 struct dir_data *dirp;
444 Data_Get_Struct(dir, struct dir_data, dirp);
445 if (!NIL_P(dirp->path)) {
446 const char *c = rb_obj_classname(dir);
447 return rb_sprintf("#<%s:%s>", c, RSTRING_PTR(dirp->path));
449 return rb_funcall(dir, rb_intern("to_s"), 0, 0);
453 * call-seq:
454 * dir.path => string or nil
456 * Returns the path parameter passed to <em>dir</em>'s constructor.
458 * d = Dir.new("..")
459 * d.path #=> ".."
461 static VALUE
462 dir_path(VALUE dir)
464 struct dir_data *dirp;
466 Data_Get_Struct(dir, struct dir_data, dirp);
467 if (NIL_P(dirp->path)) return Qnil;
468 return rb_str_dup(dirp->path);
472 * call-seq:
473 * dir.read => string or nil
475 * Reads the next entry from <em>dir</em> and returns it as a string.
476 * Returns <code>nil</code> at the end of the stream.
478 * d = Dir.new("testdir")
479 * d.read #=> "."
480 * d.read #=> ".."
481 * d.read #=> "config.h"
483 static VALUE
484 dir_read(VALUE dir)
486 struct dir_data *dirp;
487 struct dirent *dp;
489 GetDIR(dir, dirp);
490 errno = 0;
491 dp = readdir(dirp->dir);
492 if (dp) {
493 return dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp);
495 else if (errno == 0) { /* end of stream */
496 return Qnil;
498 else {
499 rb_sys_fail(0);
501 return Qnil; /* not reached */
505 * call-seq:
506 * dir.each { |filename| block } => dir
508 * Calls the block once for each entry in this directory, passing the
509 * filename of each entry as a parameter to the block.
511 * d = Dir.new("testdir")
512 * d.each {|x| puts "Got #{x}" }
514 * <em>produces:</em>
516 * Got .
517 * Got ..
518 * Got config.h
519 * Got main.rb
521 static VALUE
522 dir_each(VALUE dir)
524 struct dir_data *dirp;
525 struct dirent *dp;
527 RETURN_ENUMERATOR(dir, 0, 0);
528 GetDIR(dir, dirp);
529 rewinddir(dirp->dir);
530 for (dp = readdir(dirp->dir); dp != NULL; dp = readdir(dirp->dir)) {
531 rb_yield(dir_enc_str(rb_tainted_str_new(dp->d_name, NAMLEN(dp)), dirp));
532 if (dirp->dir == NULL) dir_closed();
534 return dir;
538 * call-seq:
539 * dir.pos => integer
540 * dir.tell => integer
542 * Returns the current position in <em>dir</em>. See also
543 * <code>Dir#seek</code>.
545 * d = Dir.new("testdir")
546 * d.tell #=> 0
547 * d.read #=> "."
548 * d.tell #=> 12
550 static VALUE
551 dir_tell(VALUE dir)
553 #ifdef HAVE_TELLDIR
554 struct dir_data *dirp;
555 long pos;
557 GetDIR(dir, dirp);
558 pos = telldir(dirp->dir);
559 return rb_int2inum(pos);
560 #else
561 rb_notimplement();
562 #endif
566 * call-seq:
567 * dir.seek( integer ) => dir
569 * Seeks to a particular location in <em>dir</em>. <i>integer</i>
570 * must be a value returned by <code>Dir#tell</code>.
572 * d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
573 * d.read #=> "."
574 * i = d.tell #=> 12
575 * d.read #=> ".."
576 * d.seek(i) #=> #<Dir:0x401b3c40>
577 * d.read #=> ".."
579 static VALUE
580 dir_seek(VALUE dir, VALUE pos)
582 struct dir_data *dirp;
583 off_t p = NUM2OFFT(pos);
585 GetDIR(dir, dirp);
586 #ifdef HAVE_SEEKDIR
587 seekdir(dirp->dir, p);
588 return dir;
589 #else
590 rb_notimplement();
591 #endif
595 * call-seq:
596 * dir.pos( integer ) => integer
598 * Synonym for <code>Dir#seek</code>, but returns the position
599 * parameter.
601 * d = Dir.new("testdir") #=> #<Dir:0x401b3c40>
602 * d.read #=> "."
603 * i = d.pos #=> 12
604 * d.read #=> ".."
605 * d.pos = i #=> 12
606 * d.read #=> ".."
608 static VALUE
609 dir_set_pos(VALUE dir, VALUE pos)
611 dir_seek(dir, pos);
612 return pos;
616 * call-seq:
617 * dir.rewind => dir
619 * Repositions <em>dir</em> to the first entry.
621 * d = Dir.new("testdir")
622 * d.read #=> "."
623 * d.rewind #=> #<Dir:0x401b3fb0>
624 * d.read #=> "."
626 static VALUE
627 dir_rewind(VALUE dir)
629 struct dir_data *dirp;
631 if (rb_safe_level() >= 4 && !OBJ_UNTRUSTED(dir)) {
632 rb_raise(rb_eSecurityError, "Insecure: can't close");
634 GetDIR(dir, dirp);
635 rewinddir(dirp->dir);
636 return dir;
640 * call-seq:
641 * dir.close => nil
643 * Closes the directory stream. Any further attempts to access
644 * <em>dir</em> will raise an <code>IOError</code>.
646 * d = Dir.new("testdir")
647 * d.close #=> nil
649 static VALUE
650 dir_close(VALUE dir)
652 struct dir_data *dirp;
654 GetDIR(dir, dirp);
655 closedir(dirp->dir);
656 dirp->dir = NULL;
658 return Qnil;
661 static void
662 dir_chdir(VALUE path)
664 if (chdir(RSTRING_PTR(path)) < 0)
665 rb_sys_fail(RSTRING_PTR(path));
668 static int chdir_blocking = 0;
669 static VALUE chdir_thread = Qnil;
671 struct chdir_data {
672 VALUE old_path, new_path;
673 int done;
676 static VALUE
677 chdir_yield(struct chdir_data *args)
679 dir_chdir(args->new_path);
680 args->done = Qtrue;
681 chdir_blocking++;
682 if (chdir_thread == Qnil)
683 chdir_thread = rb_thread_current();
684 return rb_yield(args->new_path);
687 static VALUE
688 chdir_restore(struct chdir_data *args)
690 if (args->done) {
691 chdir_blocking--;
692 if (chdir_blocking == 0)
693 chdir_thread = Qnil;
694 dir_chdir(args->old_path);
696 return Qnil;
700 * call-seq:
701 * Dir.chdir( [ string] ) => 0
702 * Dir.chdir( [ string] ) {| path | block } => anObject
704 * Changes the current working directory of the process to the given
705 * string. When called without an argument, changes the directory to
706 * the value of the environment variable <code>HOME</code>, or
707 * <code>LOGDIR</code>. <code>SystemCallError</code> (probably
708 * <code>Errno::ENOENT</code>) if the target directory does not exist.
710 * If a block is given, it is passed the name of the new current
711 * directory, and the block is executed with that as the current
712 * directory. The original working directory is restored when the block
713 * exits. The return value of <code>chdir</code> is the value of the
714 * block. <code>chdir</code> blocks can be nested, but in a
715 * multi-threaded program an error will be raised if a thread attempts
716 * to open a <code>chdir</code> block while another thread has one
717 * open.
719 * Dir.chdir("/var/spool/mail")
720 * puts Dir.pwd
721 * Dir.chdir("/tmp") do
722 * puts Dir.pwd
723 * Dir.chdir("/usr") do
724 * puts Dir.pwd
725 * end
726 * puts Dir.pwd
727 * end
728 * puts Dir.pwd
730 * <em>produces:</em>
732 * /var/spool/mail
733 * /tmp
734 * /usr
735 * /tmp
736 * /var/spool/mail
738 static VALUE
739 dir_s_chdir(int argc, VALUE *argv, VALUE obj)
741 VALUE path = Qnil;
743 rb_secure(2);
744 if (rb_scan_args(argc, argv, "01", &path) == 1) {
745 FilePathValue(path);
747 else {
748 const char *dist = getenv("HOME");
749 if (!dist) {
750 dist = getenv("LOGDIR");
751 if (!dist) rb_raise(rb_eArgError, "HOME/LOGDIR not set");
753 path = rb_str_new2(dist);
756 if (chdir_blocking > 0) {
757 if (!rb_block_given_p() || rb_thread_current() != chdir_thread)
758 rb_warn("conflicting chdir during another chdir block");
761 if (rb_block_given_p()) {
762 struct chdir_data args;
763 char *cwd = my_getcwd();
765 args.old_path = rb_tainted_str_new2(cwd); xfree(cwd);
766 args.new_path = path;
767 args.done = Qfalse;
768 return rb_ensure(chdir_yield, (VALUE)&args, chdir_restore, (VALUE)&args);
770 dir_chdir(path);
772 return INT2FIX(0);
776 * call-seq:
777 * Dir.getwd => string
778 * Dir.pwd => string
780 * Returns the path to the current working directory of this process as
781 * a string.
783 * Dir.chdir("/tmp") #=> 0
784 * Dir.getwd #=> "/tmp"
786 static VALUE
787 dir_s_getwd(VALUE dir)
789 char *path;
790 VALUE cwd;
792 rb_secure(4);
793 path = my_getcwd();
794 cwd = rb_tainted_str_new2(path);
796 xfree(path);
797 return cwd;
800 static void
801 check_dirname(volatile VALUE *dir)
803 char *path, *pend;
805 rb_secure(2);
806 FilePathValue(*dir);
807 path = RSTRING_PTR(*dir);
808 if (path && *(pend = rb_path_end(rb_path_skip_prefix(path)))) {
809 *dir = rb_str_new(path, pend - path);
814 * call-seq:
815 * Dir.chroot( string ) => 0
817 * Changes this process's idea of the file system root. Only a
818 * privileged process may make this call. Not available on all
819 * platforms. On Unix systems, see <code>chroot(2)</code> for more
820 * information.
822 static VALUE
823 dir_s_chroot(VALUE dir, VALUE path)
825 #if defined(HAVE_CHROOT) && !defined(__CHECKER__)
826 check_dirname(&path);
828 if (chroot(RSTRING_PTR(path)) == -1)
829 rb_sys_fail(RSTRING_PTR(path));
831 return INT2FIX(0);
832 #else
833 rb_notimplement();
834 return Qnil; /* not reached */
835 #endif
839 * call-seq:
840 * Dir.mkdir( string [, integer] ) => 0
842 * Makes a new directory named by <i>string</i>, with permissions
843 * specified by the optional parameter <i>anInteger</i>. The
844 * permissions may be modified by the value of
845 * <code>File::umask</code>, and are ignored on NT. Raises a
846 * <code>SystemCallError</code> if the directory cannot be created. See
847 * also the discussion of permissions in the class documentation for
848 * <code>File</code>.
851 static VALUE
852 dir_s_mkdir(int argc, VALUE *argv, VALUE obj)
854 VALUE path, vmode;
855 int mode;
857 if (rb_scan_args(argc, argv, "11", &path, &vmode) == 2) {
858 mode = NUM2INT(vmode);
860 else {
861 mode = 0777;
864 check_dirname(&path);
865 if (mkdir(RSTRING_PTR(path), mode) == -1)
866 rb_sys_fail(RSTRING_PTR(path));
868 return INT2FIX(0);
872 * call-seq:
873 * Dir.delete( string ) => 0
874 * Dir.rmdir( string ) => 0
875 * Dir.unlink( string ) => 0
877 * Deletes the named directory. Raises a subclass of
878 * <code>SystemCallError</code> if the directory isn't empty.
880 static VALUE
881 dir_s_rmdir(VALUE obj, VALUE dir)
883 check_dirname(&dir);
884 if (rmdir(RSTRING_PTR(dir)) < 0)
885 rb_sys_fail(RSTRING_PTR(dir));
887 return INT2FIX(0);
890 static void
891 sys_warning_1(const char* mesg)
893 rb_sys_warning("%s", mesg);
896 #define GLOB_VERBOSE (1UL << (sizeof(int) * CHAR_BIT - 1))
897 #define sys_warning(val) \
898 (void)((flags & GLOB_VERBOSE) && rb_protect((VALUE (*)(VALUE))sys_warning_1, (VALUE)(val), 0))
900 #define GLOB_ALLOC(type) (type *)malloc(sizeof(type))
901 #define GLOB_ALLOC_N(type, n) (type *)malloc(sizeof(type) * (n))
902 #define GLOB_FREE(ptr) free(ptr)
903 #define GLOB_JUMP_TAG(status) ((status == -1) ? rb_memerror() : rb_jump_tag(status))
906 * ENOTDIR can be returned by stat(2) if a non-leaf element of the path
907 * is not a directory.
909 #define to_be_ignored(e) ((e) == ENOENT || (e) == ENOTDIR)
911 /* System call with warning */
912 static int
913 do_stat(const char *path, struct stat *pst, int flags)
916 int ret = stat(path, pst);
917 if (ret < 0 && !to_be_ignored(errno))
918 sys_warning(path);
920 return ret;
923 static int
924 do_lstat(const char *path, struct stat *pst, int flags)
926 int ret = lstat(path, pst);
927 if (ret < 0 && !to_be_ignored(errno))
928 sys_warning(path);
930 return ret;
933 static DIR *
934 do_opendir(const char *path, int flags)
936 DIR *dirp = opendir(path);
937 if (dirp == NULL && !to_be_ignored(errno))
938 sys_warning(path);
940 return dirp;
943 /* Return nonzero if S has any special globbing chars in it. */
944 static int
945 has_magic(const char *s, int flags, rb_encoding *enc)
947 const int escape = !(flags & FNM_NOESCAPE);
948 const int nocase = flags & FNM_CASEFOLD;
950 register const char *p = s;
951 register const char *pend = p + strlen(p);
952 register char c;
954 while ((c = *p++) != 0) {
955 switch (c) {
956 case '*':
957 case '?':
958 case '[':
959 return 1;
961 case '\\':
962 if (escape && !(c = *p++))
963 return 0;
964 continue;
966 default:
967 if (!FNM_SYSCASE && ISALPHA(c) && nocase)
968 return 1;
971 p = Next(p-1, pend, enc);
974 return 0;
977 /* Find separator in globbing pattern. */
978 static char *
979 find_dirsep(const char *s, int flags, rb_encoding *enc)
981 const int escape = !(flags & FNM_NOESCAPE);
983 register const char *p = s;
984 register const char *pend = p + strlen(p);
985 register char c;
986 int open = 0;
988 while ((c = *p++) != 0) {
989 switch (c) {
990 case '[':
991 open = 1;
992 continue;
993 case ']':
994 open = 0;
995 continue;
997 case '/':
998 if (!open)
999 return (char *)p-1;
1000 continue;
1002 case '\\':
1003 if (escape && !(c = *p++))
1004 return (char *)p-1;
1005 continue;
1008 p = Next(p-1, pend, enc);
1011 return (char *)p-1;
1014 /* Remove escaping backslashes */
1015 static void
1016 remove_backslashes(char *p, rb_encoding *enc)
1018 register const char *pend = p + strlen(p);
1019 char *t = p;
1020 char *s = p;
1022 while (*p) {
1023 if (*p == '\\') {
1024 if (t != s)
1025 memmove(t, s, p - s);
1026 t += p - s;
1027 s = ++p;
1028 if (!*p) break;
1030 Inc(p, pend, enc);
1033 while (*p++);
1035 if (t != s)
1036 memmove(t, s, p - s); /* move '\0' too */
1039 /* Globing pattern */
1040 enum glob_pattern_type { PLAIN, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
1042 struct glob_pattern {
1043 char *str;
1044 enum glob_pattern_type type;
1045 struct glob_pattern *next;
1048 static void glob_free_pattern(struct glob_pattern *list);
1050 static struct glob_pattern *
1051 glob_make_pattern(const char *p, int flags, rb_encoding *enc)
1053 struct glob_pattern *list, *tmp, **tail = &list;
1054 int dirsep = 0; /* pattern is terminated with '/' */
1056 while (*p) {
1057 tmp = GLOB_ALLOC(struct glob_pattern);
1058 if (!tmp) goto error;
1059 if (p[0] == '*' && p[1] == '*' && p[2] == '/') {
1060 /* fold continuous RECURSIVEs (needed in glob_helper) */
1061 do { p += 3; } while (p[0] == '*' && p[1] == '*' && p[2] == '/');
1062 tmp->type = RECURSIVE;
1063 tmp->str = 0;
1064 dirsep = 1;
1066 else {
1067 const char *m = find_dirsep(p, flags, enc);
1068 char *buf = GLOB_ALLOC_N(char, m-p+1);
1069 if (!buf) {
1070 GLOB_FREE(tmp);
1071 goto error;
1073 memcpy(buf, p, m-p);
1074 buf[m-p] = '\0';
1075 tmp->type = has_magic(buf, flags, enc) ? MAGICAL : PLAIN;
1076 tmp->str = buf;
1077 if (*m) {
1078 dirsep = 1;
1079 p = m + 1;
1081 else {
1082 dirsep = 0;
1083 p = m;
1086 *tail = tmp;
1087 tail = &tmp->next;
1090 tmp = GLOB_ALLOC(struct glob_pattern);
1091 if (!tmp) {
1092 error:
1093 *tail = 0;
1094 glob_free_pattern(list);
1095 return 0;
1097 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
1098 tmp->str = 0;
1099 *tail = tmp;
1100 tmp->next = 0;
1102 return list;
1105 static void
1106 glob_free_pattern(struct glob_pattern *list)
1108 while (list) {
1109 struct glob_pattern *tmp = list;
1110 list = list->next;
1111 if (tmp->str)
1112 GLOB_FREE(tmp->str);
1113 GLOB_FREE(tmp);
1117 static char *
1118 join_path(const char *path, int dirsep, const char *name)
1120 long len = strlen(path);
1121 char *buf = GLOB_ALLOC_N(char, len+strlen(name)+(dirsep?1:0)+1);
1123 if (!buf) return 0;
1124 memcpy(buf, path, len);
1125 if (dirsep) {
1126 strcpy(buf+len, "/");
1127 len++;
1129 strcpy(buf+len, name);
1130 return buf;
1133 enum answer { YES, NO, UNKNOWN };
1135 #ifndef S_ISDIR
1136 # define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
1137 #endif
1139 #ifndef S_ISLNK
1140 # ifndef S_IFLNK
1141 # define S_ISLNK(m) (0)
1142 # else
1143 # define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
1144 # endif
1145 #endif
1147 struct glob_args {
1148 void (*func)(const char *, VALUE, void *);
1149 const char *path;
1150 VALUE value;
1151 rb_encoding *enc;
1154 static VALUE
1155 glob_func_caller(VALUE val)
1157 struct glob_args *args = (struct glob_args *)val;
1159 (*args->func)(args->path, args->value, args->enc);
1160 return Qnil;
1163 #define glob_call_func(func, path, arg, enc) (*func)(path, arg, enc)
1165 static int
1166 glob_helper(
1167 const char *path,
1168 int dirsep, /* '/' should be placed before appending child entry's name to 'path'. */
1169 enum answer exist, /* Does 'path' indicate an existing entry? */
1170 enum answer isdir, /* Does 'path' indicate a directory or a symlink to a directory? */
1171 struct glob_pattern **beg,
1172 struct glob_pattern **end,
1173 int flags,
1174 ruby_glob_func *func,
1175 VALUE arg,
1176 rb_encoding *enc)
1178 struct stat st;
1179 int status = 0;
1180 struct glob_pattern **cur, **new_beg, **new_end;
1181 int plain = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
1182 int escape = !(flags & FNM_NOESCAPE);
1184 for (cur = beg; cur < end; ++cur) {
1185 struct glob_pattern *p = *cur;
1186 if (p->type == RECURSIVE) {
1187 recursive = 1;
1188 p = p->next;
1190 switch (p->type) {
1191 case PLAIN:
1192 plain = 1;
1193 break;
1194 case MAGICAL:
1195 magical = 1;
1196 break;
1197 case MATCH_ALL:
1198 match_all = 1;
1199 break;
1200 case MATCH_DIR:
1201 match_dir = 1;
1202 break;
1203 case RECURSIVE:
1204 rb_bug("continuous RECURSIVEs");
1208 if (*path) {
1209 if (match_all && exist == UNKNOWN) {
1210 if (do_lstat(path, &st, flags) == 0) {
1211 exist = YES;
1212 isdir = S_ISDIR(st.st_mode) ? YES : S_ISLNK(st.st_mode) ? UNKNOWN : NO;
1214 else {
1215 exist = NO;
1216 isdir = NO;
1219 if (match_dir && isdir == UNKNOWN) {
1220 if (do_stat(path, &st, flags) == 0) {
1221 exist = YES;
1222 isdir = S_ISDIR(st.st_mode) ? YES : NO;
1224 else {
1225 exist = NO;
1226 isdir = NO;
1229 if (match_all && exist == YES) {
1230 status = glob_call_func(func, path, arg, enc);
1231 if (status) return status;
1233 if (match_dir && isdir == YES) {
1234 char *tmp = join_path(path, dirsep, "");
1235 if (!tmp) return -1;
1236 status = glob_call_func(func, tmp, arg, enc);
1237 GLOB_FREE(tmp);
1238 if (status) return status;
1242 if (exist == NO || isdir == NO) return 0;
1244 if (magical || recursive) {
1245 struct dirent *dp;
1246 DIR *dirp = do_opendir(*path ? path : ".", flags);
1247 if (dirp == NULL) return 0;
1249 for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
1250 char *buf = join_path(path, dirsep, dp->d_name);
1251 enum answer new_isdir = UNKNOWN;
1253 if (!buf) {
1254 status = -1;
1255 break;
1257 if (recursive && strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0
1258 && fnmatch("*", rb_usascii_encoding(), dp->d_name, flags) == 0) {
1259 #ifndef _WIN32
1260 if (do_lstat(buf, &st, flags) == 0)
1261 new_isdir = S_ISDIR(st.st_mode) ? YES : S_ISLNK(st.st_mode) ? UNKNOWN : NO;
1262 else
1263 new_isdir = NO;
1264 #else
1265 new_isdir = dp->d_isdir ? (!dp->d_isrep ? YES : UNKNOWN) : NO;
1266 #endif
1269 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, (end - beg) * 2);
1270 if (!new_beg) {
1271 GLOB_FREE(buf);
1272 status = -1;
1273 break;
1276 for (cur = beg; cur < end; ++cur) {
1277 struct glob_pattern *p = *cur;
1278 if (p->type == RECURSIVE) {
1279 if (new_isdir == YES) /* not symlink but real directory */
1280 *new_end++ = p; /* append recursive pattern */
1281 p = p->next; /* 0 times recursion */
1283 if (p->type == PLAIN || p->type == MAGICAL) {
1284 if (fnmatch(p->str, enc, dp->d_name, flags) == 0)
1285 *new_end++ = p->next;
1289 status = glob_helper(buf, 1, YES, new_isdir, new_beg, new_end,
1290 flags, func, arg, enc);
1291 GLOB_FREE(buf);
1292 GLOB_FREE(new_beg);
1293 if (status) break;
1296 closedir(dirp);
1298 else if (plain) {
1299 struct glob_pattern **copy_beg, **copy_end, **cur2;
1301 copy_beg = copy_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
1302 if (!copy_beg) return -1;
1303 for (cur = beg; cur < end; ++cur)
1304 *copy_end++ = (*cur)->type == PLAIN ? *cur : 0;
1306 for (cur = copy_beg; cur < copy_end; ++cur) {
1307 if (*cur) {
1308 char *buf;
1309 char *name;
1310 name = GLOB_ALLOC_N(char, strlen((*cur)->str) + 1);
1311 if (!name) {
1312 status = -1;
1313 break;
1315 strcpy(name, (*cur)->str);
1316 if (escape) remove_backslashes(name, enc);
1318 new_beg = new_end = GLOB_ALLOC_N(struct glob_pattern *, end - beg);
1319 if (!new_beg) {
1320 GLOB_FREE(name);
1321 status = -1;
1322 break;
1324 *new_end++ = (*cur)->next;
1325 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
1326 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
1327 *new_end++ = (*cur2)->next;
1328 *cur2 = 0;
1332 buf = join_path(path, dirsep, name);
1333 GLOB_FREE(name);
1334 if (!buf) {
1335 GLOB_FREE(new_beg);
1336 status = -1;
1337 break;
1339 status = glob_helper(buf, 1, UNKNOWN, UNKNOWN, new_beg,
1340 new_end, flags, func, arg, enc);
1341 GLOB_FREE(buf);
1342 GLOB_FREE(new_beg);
1343 if (status) break;
1347 GLOB_FREE(copy_beg);
1350 return status;
1353 static int
1354 ruby_glob0(const char *path, int flags, ruby_glob_func *func, VALUE arg, rb_encoding *enc)
1356 struct glob_pattern *list;
1357 const char *root, *start;
1358 char *buf;
1359 int n;
1360 int status;
1362 start = root = path;
1363 flags |= FNM_SYSCASE;
1364 #if defined DOSISH
1365 root = rb_path_skip_prefix(root);
1366 #endif
1368 if (root && *root == '/') root++;
1370 n = root - start;
1371 buf = GLOB_ALLOC_N(char, n + 1);
1372 if (!buf) return -1;
1373 MEMCPY(buf, start, char, n);
1374 buf[n] = '\0';
1376 list = glob_make_pattern(root, flags, enc);
1377 if (!list) {
1378 GLOB_FREE(buf);
1379 return -1;
1381 status = glob_helper(buf, 0, UNKNOWN, UNKNOWN, &list, &list + 1, flags, func, arg, enc);
1382 glob_free_pattern(list);
1383 GLOB_FREE(buf);
1385 return status;
1389 ruby_glob(const char *path, int flags, ruby_glob_func *func, VALUE arg)
1391 return ruby_glob0(path, flags & ~GLOB_VERBOSE, func, arg,
1392 rb_ascii8bit_encoding());
1395 static int
1396 rb_glob_caller(const char *path, VALUE a, void *enc)
1398 int status;
1399 struct glob_args *args = (struct glob_args *)a;
1401 args->path = path;
1402 rb_protect(glob_func_caller, a, &status);
1403 return status;
1406 static int
1407 rb_glob2(const char *path, int flags,
1408 void (*func)(const char *, VALUE, void *), VALUE arg,
1409 rb_encoding* enc)
1411 struct glob_args args;
1413 args.func = func;
1414 args.value = arg;
1415 args.enc = enc;
1417 if (flags & FNM_SYSCASE) {
1418 rb_warning("Dir.glob() ignores File::FNM_CASEFOLD");
1421 return ruby_glob0(path, flags | GLOB_VERBOSE, rb_glob_caller, (VALUE)&args,
1422 enc);
1425 void
1426 rb_glob(const char *path, void (*func)(const char *, VALUE, void *), VALUE arg)
1428 int status = rb_glob2(path, 0, func, arg, rb_ascii8bit_encoding());
1429 if (status) GLOB_JUMP_TAG(status);
1432 static void
1433 push_pattern(const char *path, VALUE ary, void *enc)
1435 VALUE vpath = rb_tainted_str_new2(path);
1436 rb_enc_associate(vpath, enc);
1437 rb_ary_push(ary, vpath);
1440 static int
1441 ruby_brace_expand(const char *str, int flags, ruby_glob_func *func, VALUE arg,
1442 rb_encoding *enc)
1444 const int escape = !(flags & FNM_NOESCAPE);
1445 const char *p = str;
1446 const char *pend = p + strlen(p);
1447 const char *s = p;
1448 const char *lbrace = 0, *rbrace = 0;
1449 int nest = 0, status = 0;
1451 while (*p) {
1452 if (*p == '{' && nest++ == 0) {
1453 lbrace = p;
1455 if (*p == '}' && --nest <= 0) {
1456 rbrace = p;
1457 break;
1459 if (*p == '\\' && escape) {
1460 if (!*++p) break;
1462 Inc(p, pend, enc);
1465 if (lbrace && rbrace) {
1466 char *buf = GLOB_ALLOC_N(char, strlen(s) + 1);
1467 long shift;
1469 if (!buf) return -1;
1470 memcpy(buf, s, lbrace-s);
1471 shift = (lbrace-s);
1472 p = lbrace;
1473 while (p < rbrace) {
1474 const char *t = ++p;
1475 nest = 0;
1476 while (p < rbrace && !(*p == ',' && nest == 0)) {
1477 if (*p == '{') nest++;
1478 if (*p == '}') nest--;
1479 if (*p == '\\' && escape) {
1480 if (++p == rbrace) break;
1482 Inc(p, pend, enc);
1484 memcpy(buf+shift, t, p-t);
1485 strcpy(buf+shift+(p-t), rbrace+1);
1486 status = ruby_brace_expand(buf, flags, func, arg, enc);
1487 if (status) break;
1489 GLOB_FREE(buf);
1491 else if (!lbrace && !rbrace) {
1492 status = (*func)(s, arg, enc);
1495 return status;
1498 struct brace_args {
1499 ruby_glob_func *func;
1500 VALUE value;
1501 int flags;
1504 static int
1505 glob_brace(const char *path, VALUE val, void *enc)
1507 struct brace_args *arg = (struct brace_args *)val;
1509 return ruby_glob0(path, arg->flags, arg->func, arg->value, enc);
1512 static int
1513 ruby_brace_glob0(const char *str, int flags, ruby_glob_func *func, VALUE arg,
1514 rb_encoding* enc)
1516 struct brace_args args;
1518 args.func = func;
1519 args.value = arg;
1520 args.flags = flags;
1521 return ruby_brace_expand(str, flags, glob_brace, (VALUE)&args, enc);
1525 ruby_brace_glob(const char *str, int flags, ruby_glob_func *func, VALUE arg)
1527 return ruby_brace_glob0(str, flags & ~GLOB_VERBOSE, func, arg,
1528 rb_ascii8bit_encoding());
1531 static int
1532 push_glob(VALUE ary, VALUE str, int flags)
1534 struct glob_args args;
1535 rb_encoding *enc = rb_enc_get(str);
1537 args.func = push_pattern;
1538 args.value = ary;
1539 args.enc = enc;
1541 return ruby_brace_glob0(RSTRING_PTR(str), flags | GLOB_VERBOSE,
1542 rb_glob_caller, (VALUE)&args, enc);
1545 static VALUE
1546 rb_push_glob(VALUE str, int flags) /* '\0' is delimiter */
1548 long offset = 0;
1549 VALUE ary;
1551 StringValue(str);
1552 ary = rb_ary_new();
1554 while (offset < RSTRING_LEN(str)) {
1555 char *p, *pend;
1556 int status;
1557 p = RSTRING_PTR(str) + offset;
1558 status = push_glob(ary, rb_enc_str_new(p, strlen(p), rb_enc_get(str)),
1559 flags);
1560 if (status) GLOB_JUMP_TAG(status);
1561 if (offset >= RSTRING_LEN(str)) break;
1562 p += strlen(p) + 1;
1563 pend = RSTRING_PTR(str) + RSTRING_LEN(str);
1564 while (p < pend && !*p)
1565 p++;
1566 offset = p - RSTRING_PTR(str);
1569 return ary;
1572 static VALUE
1573 dir_globs(long argc, VALUE *argv, int flags)
1575 VALUE ary = rb_ary_new();
1576 long i;
1578 for (i = 0; i < argc; ++i) {
1579 int status;
1580 VALUE str = argv[i];
1581 StringValue(str);
1582 status = push_glob(ary, str, flags);
1583 if (status) GLOB_JUMP_TAG(status);
1586 return ary;
1590 * call-seq:
1591 * Dir[ array ] => array
1592 * Dir[ string [, string ...] ] => array
1594 * Equivalent to calling
1595 * <code>Dir.glob(</code><i>array,</i><code>0)</code> and
1596 * <code>Dir.glob([</code><i>string,...</i><code>],0)</code>.
1599 static VALUE
1600 dir_s_aref(int argc, VALUE *argv, VALUE obj)
1602 if (argc == 1) {
1603 return rb_push_glob(argv[0], 0);
1605 return dir_globs(argc, argv, 0);
1609 * call-seq:
1610 * Dir.glob( pattern, [flags] ) => array
1611 * Dir.glob( pattern, [flags] ) {| filename | block } => nil
1613 * Returns the filenames found by expanding <i>pattern</i> which is
1614 * an +Array+ of the patterns or the pattern +String+, either as an
1615 * <i>array</i> or as parameters to the block. Note that this pattern
1616 * is not a regexp (it's closer to a shell glob). See
1617 * <code>File::fnmatch</code> for the meaning of the <i>flags</i>
1618 * parameter. Note that case sensitivity depends on your system (so
1619 * <code>File::FNM_CASEFOLD</code> is ignored)
1621 * <code>*</code>:: Matches any file. Can be restricted by
1622 * other values in the glob. <code>*</code>
1623 * will match all files; <code>c*</code> will
1624 * match all files beginning with
1625 * <code>c</code>; <code>*c</code> will match
1626 * all files ending with <code>c</code>; and
1627 * <code>*c*</code> will match all files that
1628 * have <code>c</code> in them (including at
1629 * the beginning or end). Equivalent to
1630 * <code>/ .* /x</code> in regexp.
1631 * <code>**</code>:: Matches directories recursively.
1632 * <code>?</code>:: Matches any one character. Equivalent to
1633 * <code>/.{1}/</code> in regexp.
1634 * <code>[set]</code>:: Matches any one character in +set+.
1635 * Behaves exactly like character sets in
1636 * Regexp, including set negation
1637 * (<code>[^a-z]</code>).
1638 * <code>{p,q}</code>:: Matches either literal <code>p</code> or
1639 * literal <code>q</code>. Matching literals
1640 * may be more than one character in length.
1641 * More than two literals may be specified.
1642 * Equivalent to pattern alternation in
1643 * regexp.
1644 * <code>\</code>:: Escapes the next metacharacter.
1646 * Dir["config.?"] #=> ["config.h"]
1647 * Dir.glob("config.?") #=> ["config.h"]
1648 * Dir.glob("*.[a-z][a-z]") #=> ["main.rb"]
1649 * Dir.glob("*.[^r]*") #=> ["config.h"]
1650 * Dir.glob("*.{rb,h}") #=> ["main.rb", "config.h"]
1651 * Dir.glob("*") #=> ["config.h", "main.rb"]
1652 * Dir.glob("*", File::FNM_DOTMATCH) #=> [".", "..", "config.h", "main.rb"]
1654 * rbfiles = File.join("**", "*.rb")
1655 * Dir.glob(rbfiles) #=> ["main.rb",
1656 * # "lib/song.rb",
1657 * # "lib/song/karaoke.rb"]
1658 * libdirs = File.join("**", "lib")
1659 * Dir.glob(libdirs) #=> ["lib"]
1661 * librbfiles = File.join("**", "lib", "**", "*.rb")
1662 * Dir.glob(librbfiles) #=> ["lib/song.rb",
1663 * # "lib/song/karaoke.rb"]
1665 * librbfiles = File.join("**", "lib", "*.rb")
1666 * Dir.glob(librbfiles) #=> ["lib/song.rb"]
1668 static VALUE
1669 dir_s_glob(int argc, VALUE *argv, VALUE obj)
1671 VALUE str, rflags, ary;
1672 int flags;
1674 if (rb_scan_args(argc, argv, "11", &str, &rflags) == 2)
1675 flags = NUM2INT(rflags);
1676 else
1677 flags = 0;
1679 ary = rb_check_array_type(str);
1680 if (NIL_P(ary)) {
1681 ary = rb_push_glob(str, flags);
1683 else {
1684 volatile VALUE v = ary;
1685 ary = dir_globs(RARRAY_LEN(v), RARRAY_PTR(v), flags);
1688 if (rb_block_given_p()) {
1689 rb_ary_each(ary);
1690 return Qnil;
1692 return ary;
1695 static VALUE
1696 dir_open_dir(int argc, VALUE *argv)
1698 VALUE dir = rb_funcall2(rb_cDir, rb_intern("open"), argc, argv);
1700 if (TYPE(dir) != T_DATA ||
1701 RDATA(dir)->dfree != (RUBY_DATA_FUNC)free_dir) {
1702 rb_raise(rb_eTypeError, "wrong argument type %s (expected Dir)",
1703 rb_obj_classname(dir));
1705 return dir;
1710 * call-seq:
1711 * Dir.foreach( dirname ) {| filename | block } => nil
1713 * Calls the block once for each entry in the named directory, passing
1714 * the filename of each entry as a parameter to the block.
1716 * Dir.foreach("testdir") {|x| puts "Got #{x}" }
1718 * <em>produces:</em>
1720 * Got .
1721 * Got ..
1722 * Got config.h
1723 * Got main.rb
1726 static VALUE
1727 dir_foreach(int argc, VALUE *argv, VALUE io)
1729 VALUE dir;
1731 RETURN_ENUMERATOR(io, argc, argv);
1732 dir = dir_open_dir(argc, argv);
1733 rb_ensure(dir_each, dir, dir_close, dir);
1734 return Qnil;
1738 * call-seq:
1739 * Dir.entries( dirname ) => array
1741 * Returns an array containing all of the filenames in the given
1742 * directory. Will raise a <code>SystemCallError</code> if the named
1743 * directory doesn't exist.
1745 * Dir.entries("testdir") #=> [".", "..", "config.h", "main.rb"]
1748 static VALUE
1749 dir_entries(int argc, VALUE *argv, VALUE io)
1751 VALUE dir;
1753 dir = dir_open_dir(argc, argv);
1754 return rb_ensure(rb_Array, dir, dir_close, dir);
1758 * call-seq:
1759 * File.fnmatch( pattern, path, [flags] ) => (true or false)
1760 * File.fnmatch?( pattern, path, [flags] ) => (true or false)
1762 * Returns true if <i>path</i> matches against <i>pattern</i> The
1763 * pattern is not a regular expression; instead it follows rules
1764 * similar to shell filename globbing. It may contain the following
1765 * metacharacters:
1767 * <code>*</code>:: Matches any file. Can be restricted by
1768 * other values in the glob. <code>*</code>
1769 * will match all files; <code>c*</code> will
1770 * match all files beginning with
1771 * <code>c</code>; <code>*c</code> will match
1772 * all files ending with <code>c</code>; and
1773 * <code>*c*</code> will match all files that
1774 * have <code>c</code> in them (including at
1775 * the beginning or end). Equivalent to
1776 * <code>/ .* /x</code> in regexp.
1777 * <code>**</code>:: Matches directories recursively or files
1778 * expansively.
1779 * <code>?</code>:: Matches any one character. Equivalent to
1780 * <code>/.{1}/</code> in regexp.
1781 * <code>[set]</code>:: Matches any one character in +set+.
1782 * Behaves exactly like character sets in
1783 * Regexp, including set negation
1784 * (<code>[^a-z]</code>).
1785 * <code>\</code>:: Escapes the next metacharacter.
1787 * <i>flags</i> is a bitwise OR of the <code>FNM_xxx</code>
1788 * parameters. The same glob pattern and flags are used by
1789 * <code>Dir::glob</code>.
1791 * File.fnmatch('cat', 'cat') #=> true # match entire string
1792 * File.fnmatch('cat', 'category') #=> false # only match partial string
1793 * File.fnmatch('c{at,ub}s', 'cats') #=> false # { } isn't supported
1795 * File.fnmatch('c?t', 'cat') #=> true # '?' match only 1 character
1796 * File.fnmatch('c??t', 'cat') #=> false # ditto
1797 * File.fnmatch('c*', 'cats') #=> true # '*' match 0 or more characters
1798 * File.fnmatch('c*t', 'c/a/b/t') #=> true # ditto
1799 * File.fnmatch('ca[a-z]', 'cat') #=> true # inclusive bracket expression
1800 * File.fnmatch('ca[^t]', 'cat') #=> false # exclusive bracket expression ('^' or '!')
1802 * File.fnmatch('cat', 'CAT') #=> false # case sensitive
1803 * File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true # case insensitive
1805 * File.fnmatch('?', '/', File::FNM_PATHNAME) #=> false # wildcard doesn't match '/' on FNM_PATHNAME
1806 * File.fnmatch('*', '/', File::FNM_PATHNAME) #=> false # ditto
1807 * File.fnmatch('[/]', '/', File::FNM_PATHNAME) #=> false # ditto
1809 * File.fnmatch('\?', '?') #=> true # escaped wildcard becomes ordinary
1810 * File.fnmatch('\a', 'a') #=> true # escaped ordinary remains ordinary
1811 * File.fnmatch('\a', '\a', File::FNM_NOESCAPE) #=> true # FNM_NOESACPE makes '\' ordinary
1812 * File.fnmatch('[\?]', '?') #=> true # can escape inside bracket expression
1814 * File.fnmatch('*', '.profile') #=> false # wildcard doesn't match leading
1815 * File.fnmatch('*', '.profile', File::FNM_DOTMATCH) #=> true # period by default.
1816 * File.fnmatch('.*', '.profile') #=> true
1818 * rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
1819 * File.fnmatch(rbfiles, 'main.rb') #=> false
1820 * File.fnmatch(rbfiles, './main.rb') #=> false
1821 * File.fnmatch(rbfiles, 'lib/song.rb') #=> true
1822 * File.fnmatch('**.rb', 'main.rb') #=> true
1823 * File.fnmatch('**.rb', './main.rb') #=> false
1824 * File.fnmatch('**.rb', 'lib/song.rb') #=> true
1825 * File.fnmatch('*', 'dave/.profile') #=> true
1827 * pattern = '*' '/' '*'
1828 * File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME) #=> false
1829 * File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1831 * pattern = '**' '/' 'foo'
1832 * File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME) #=> true
1833 * File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME) #=> true
1834 * File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME) #=> true
1835 * File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME) #=> false
1836 * File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true
1838 static VALUE
1839 file_s_fnmatch(int argc, VALUE *argv, VALUE obj)
1841 VALUE pattern, path;
1842 VALUE rflags;
1843 int flags;
1845 if (rb_scan_args(argc, argv, "21", &pattern, &path, &rflags) == 3)
1846 flags = NUM2INT(rflags);
1847 else
1848 flags = 0;
1850 StringValue(pattern);
1851 FilePathStringValue(path);
1853 if (fnmatch(RSTRING_PTR(pattern), rb_enc_get(pattern), RSTRING_PTR(path),
1854 flags) == 0)
1855 return Qtrue;
1857 return Qfalse;
1861 * Objects of class <code>Dir</code> are directory streams representing
1862 * directories in the underlying file system. They provide a variety of
1863 * ways to list directories and their contents. See also
1864 * <code>File</code>.
1866 * The directory used in these examples contains the two regular files
1867 * (<code>config.h</code> and <code>main.rb</code>), the parent
1868 * directory (<code>..</code>), and the directory itself
1869 * (<code>.</code>).
1871 void
1872 Init_Dir(void)
1874 rb_cDir = rb_define_class("Dir", rb_cObject);
1876 rb_include_module(rb_cDir, rb_mEnumerable);
1878 rb_define_alloc_func(rb_cDir, dir_s_alloc);
1879 rb_define_singleton_method(rb_cDir, "open", dir_s_open, -1);
1880 rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, -1);
1881 rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
1883 rb_define_method(rb_cDir,"initialize", dir_initialize, -1);
1884 rb_define_method(rb_cDir,"path", dir_path, 0);
1885 rb_define_method(rb_cDir,"inspect", dir_inspect, 0);
1886 rb_define_method(rb_cDir,"read", dir_read, 0);
1887 rb_define_method(rb_cDir,"each", dir_each, 0);
1888 rb_define_method(rb_cDir,"rewind", dir_rewind, 0);
1889 rb_define_method(rb_cDir,"tell", dir_tell, 0);
1890 rb_define_method(rb_cDir,"seek", dir_seek, 1);
1891 rb_define_method(rb_cDir,"pos", dir_tell, 0);
1892 rb_define_method(rb_cDir,"pos=", dir_set_pos, 1);
1893 rb_define_method(rb_cDir,"close", dir_close, 0);
1895 rb_define_singleton_method(rb_cDir,"chdir", dir_s_chdir, -1);
1896 rb_define_singleton_method(rb_cDir,"getwd", dir_s_getwd, 0);
1897 rb_define_singleton_method(rb_cDir,"pwd", dir_s_getwd, 0);
1898 rb_define_singleton_method(rb_cDir,"chroot", dir_s_chroot, 1);
1899 rb_define_singleton_method(rb_cDir,"mkdir", dir_s_mkdir, -1);
1900 rb_define_singleton_method(rb_cDir,"rmdir", dir_s_rmdir, 1);
1901 rb_define_singleton_method(rb_cDir,"delete", dir_s_rmdir, 1);
1902 rb_define_singleton_method(rb_cDir,"unlink", dir_s_rmdir, 1);
1904 rb_define_singleton_method(rb_cDir,"glob", dir_s_glob, -1);
1905 rb_define_singleton_method(rb_cDir,"[]", dir_s_aref, -1);
1906 rb_define_singleton_method(rb_cDir,"exist?", rb_file_directory_p, 1); /* in file.c */
1907 rb_define_singleton_method(rb_cDir,"exists?", rb_file_directory_p, 1); /* in file.c */
1909 rb_define_singleton_method(rb_cFile,"fnmatch", file_s_fnmatch, -1);
1910 rb_define_singleton_method(rb_cFile,"fnmatch?", file_s_fnmatch, -1);
1912 rb_file_const("FNM_NOESCAPE", INT2FIX(FNM_NOESCAPE));
1913 rb_file_const("FNM_PATHNAME", INT2FIX(FNM_PATHNAME));
1914 rb_file_const("FNM_DOTMATCH", INT2FIX(FNM_DOTMATCH));
1915 rb_file_const("FNM_CASEFOLD", INT2FIX(FNM_CASEFOLD));
1916 rb_file_const("FNM_SYSCASE", INT2FIX(FNM_SYSCASE));