14#include "ruby/internal/config.h"
26# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD) && \
27 defined(HAVE_OPENAT) && defined(HAVE_FSTATAT)
28# define USE_OPENDIR_AT 1
30# define USE_OPENDIR_AT 0
42#undef HAVE_DIRENT_NAMLEN
43#if defined HAVE_DIRENT_H && !defined _WIN32
45# define NAMLEN(dirent) strlen((dirent)->d_name)
46#elif defined HAVE_DIRECT_H && !defined _WIN32
48# define NAMLEN(dirent) strlen((dirent)->d_name)
51# define NAMLEN(dirent) (dirent)->d_namlen
52# define HAVE_DIRENT_NAMLEN 1
53# ifdef HAVE_SYS_NDIR_H
63# include "win32/dir.h"
72char *strchr(
char*,
char);
79#define USE_NAME_ON_FS_REAL_BASENAME 1
82#ifdef HAVE_GETATTRLIST
83# define USE_NAME_ON_FS USE_NAME_ON_FS_REAL_BASENAME
84# define RUP32(size) ((size)+3/4)
85# define SIZEUP32(type) RUP32(sizeof(type))
87# define USE_NAME_ON_FS USE_NAME_ON_FS_REAL_BASENAME
89# define USE_NAME_ON_FS 0
93# define NORMALIZE_UTF8PATH 1
94# include <sys/param.h>
95# include <sys/mount.h>
96# include <sys/vnode.h>
98# define NORMALIZE_UTF8PATH 0
104#include "internal/array.h"
105#include "internal/dir.h"
106#include "internal/encoding.h"
107#include "internal/error.h"
108#include "internal/file.h"
109#include "internal/gc.h"
110#include "internal/io.h"
111#include "internal/object.h"
112#include "internal/imemo.h"
113#include "internal/vm.h"
127#define vm_initialized rb_cThread
132# define chdir(p) rb_w32_uchdir(p)
133# define ruby_getcwd() rb_w32_ugetcwd(NULL, 0)
139#ifdef HAVE_GETATTRLIST
140struct getattrlist_args {
143 struct attrlist *list;
146 unsigned int options;
149# define GETATTRLIST_ARGS(list_, buf_, options_) (struct getattrlist_args) \
150 {.list = list_, .buf = buf_, .size = sizeof(buf_), .options = options_}
153nogvl_getattrlist(
void *args)
155 struct getattrlist_args *arg = args;
156 return (
void *)(
VALUE)getattrlist(arg->path, arg->list, arg->buf, arg->size, arg->options);
160gvl_getattrlist(
struct getattrlist_args *args,
const char *path)
163 return IO_WITHOUT_GVL_INT(nogvl_getattrlist, args);
166# ifdef HAVE_FGETATTRLIST
168nogvl_fgetattrlist(
void *args)
170 struct getattrlist_args *arg = args;
171 return (
void *)(
VALUE)fgetattrlist(arg->fd, arg->list, arg->buf, arg->size, arg->options);
175gvl_fgetattrlist(
struct getattrlist_args *args,
int fd)
178 return IO_WITHOUT_GVL_INT(nogvl_fgetattrlist, args);
183#if NORMALIZE_UTF8PATH
184# if defined HAVE_FGETATTRLIST || !defined HAVE_GETATTRLIST
185# define need_normalization(dirp, path) need_normalization(dirp)
187# define need_normalization(dirp, path) need_normalization(path)
190need_normalization(
DIR *dirp,
const char *path)
192# if defined HAVE_FGETATTRLIST || defined HAVE_GETATTRLIST
193 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
194 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
195 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
196# if defined HAVE_FGETATTRLIST
197 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
199 int ret = gvl_getattrlist(&args, path);
202 const fsobj_tag_t *tag = (
void *)(attrbuf+1);
214has_nonascii(
const char *ptr,
size_t len)
217 if (!ISASCII(*ptr))
return 1;
224# define IF_NORMALIZE_UTF8PATH(something) something
226# define IF_NORMALIZE_UTF8PATH(something)
229#if defined(IFTODT) && defined(DT_UNKNOWN)
230# define EMULATE_IFTODT 0
232# define EMULATE_IFTODT 1
236# define IFTODT(m) (((m) & S_IFMT) / ((~S_IFMT & (S_IFMT-1)) + 1))
241 path_exist = DT_UNKNOWN,
242 path_directory = DT_DIR,
243 path_regular = DT_REG,
244 path_symlink = DT_LNK,
247 path_directory = IFTODT(S_IFDIR),
248 path_regular = IFTODT(S_IFREG),
249 path_symlink = IFTODT(S_IFLNK),
255#define FNM_NOESCAPE 0x01
256#define FNM_PATHNAME 0x02
257#define FNM_DOTMATCH 0x04
258#define FNM_CASEFOLD 0x08
259#define FNM_EXTGLOB 0x10
260#if CASEFOLD_FILESYSTEM
261#define FNM_SYSCASE FNM_CASEFOLD
266#define FNM_SHORTNAME 0x20
268#define FNM_SHORTNAME 0
270#define FNM_GLOB_NOSORT 0x40
271#define FNM_GLOB_SKIPDOT 0x80
276# define Next(p, e, enc) ((p)+ rb_enc_mbclen((p), (e), (enc)))
277# define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
288 const int nocase = flags & FNM_CASEFOLD;
289 const int escape = !(flags & FNM_NOESCAPE);
294 if (p >= pend)
return NULL;
295 if (*p ==
'!' || *p ==
'^') {
302 if (escape && *t1 ==
'\\')
306 p = t1 + (r = rb_enc_mbclen(t1, pend, enc));
307 if (p >= pend)
return NULL;
308 if (p[0] ==
'-' && p[1] !=
']') {
309 const char *t2 = p + 1;
311 if (escape && *t2 ==
'\\')
315 p = t2 + (r2 = rb_enc_mbclen(t2, pend, enc));
317 if ((r <= (send-s) && memcmp(t1, s, r) == 0) ||
318 (r2 <= (send-s) && memcmp(t2, s, r2) == 0)) {
323 if (nocase) c1 = rb_enc_toupper(c1, enc);
325 if (nocase) c2 = rb_enc_toupper(c2, enc);
326 if (c1 < c2)
continue;
328 if (nocase) c2 = rb_enc_toupper(c2, enc);
329 if (c1 > c2)
continue;
333 if (r <= (send-s) && memcmp(t1, s, r) == 0) {
337 if (!nocase)
continue;
340 if (c1 != c2)
continue;
345 return ok == not ? NULL : (
char *)p + 1;
353#define UNESCAPE(p) (escape && *(p) == '\\' ? (p) + 1 : (p))
354#define ISEND(p) (!*(p) || (pathname && *(p) == '/'))
355#define RETURN(val) return *pcur = p, *scur = s, (val);
364 const int period = !(flags & FNM_DOTMATCH);
365 const int pathname = flags & FNM_PATHNAME;
366 const int escape = !(flags & FNM_NOESCAPE);
367 const int nocase = flags & FNM_CASEFOLD;
369 const char *ptmp = 0;
370 const char *stmp = 0;
372 const char *p = *pcur;
373 const char *pend = p + strlen(p);
374 const char *s = *scur;
375 const char *send = s + strlen(s);
379 if (period && *s ==
'.' && *UNESCAPE(p) !=
'.')
385 do { p++; }
while (*p ==
'*');
386 if (ISEND(UNESCAPE(p))) {
407 if ((t = bracket(p + 1, pend, s, send, flags, enc)) != 0) {
419 RETURN(ISEND(p) ? 0 : FNM_NOMATCH);
422 r = rb_enc_precise_mbclen(p, pend, enc);
425 if (r <= (send-s) && memcmp(p, s, r) == 0) {
430 if (!nocase)
goto failed;
441 Inc(stmp, send, enc);
456 const char *p = pattern;
457 const char *s = string;
458 const char *send = s + strlen(
string);
459 const int period = !(flags & FNM_DOTMATCH);
460 const int pathname = flags & FNM_PATHNAME;
462 const char *ptmp = 0;
463 const char *stmp = 0;
467 if (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/') {
468 do { p += 3; }
while (p[0] ==
'*' && p[1] ==
'*' && p[2] ==
'/');
472 if (fnmatch_helper(&p, &s, flags, enc) == 0) {
473 while (*s && *s !=
'/') Inc(s, send, enc);
483 if (ptmp && stmp && !(period && *stmp ==
'.')) {
484 while (*stmp && *stmp !=
'/') Inc(stmp, send, enc);
496 return fnmatch_helper(&p, &s, flags, enc);
500static VALUE sym_directory, sym_link, sym_file, sym_unknown;
502#if defined(DT_BLK) || (defined(HAVE_DIRFD) && defined(S_IFBLK))
503static VALUE sym_block_device;
505#if defined(DT_CHR) || (defined(HAVE_DIRFD) && defined(S_IFCHR))
506static VALUE sym_character_device;
508#if defined(DT_FIFO) || (defined(HAVE_DIRFD) && defined(S_IFIFO))
509static VALUE sym_fifo;
511#if defined(DT_SOCK) || (defined(HAVE_DIRFD) && defined(S_IFSOCK))
512static VALUE sym_socket;
526 if (dir->dir) closedir(dir->dir);
529RUBY_REFERENCES(dir_refs) = {
530 RUBY_REF_EDGE(
struct dir_data, path),
537 RUBY_REFS_LIST_PTR(dir_refs),
541 0, NULL, RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_THREAD_SAFE_FREE | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE
547dir_s_alloc(
VALUE klass)
560nogvl_opendir(
void *ptr)
562 const char *path = ptr;
564 return opendir(path);
568opendir_without_gvl(
const char *path)
570 if (vm_initialized) {
571 union {
const void *in;
void *out; } u;
575 return IO_WITHOUT_GVL(nogvl_opendir, u.out);
578 return opendir(path);
585 if (closedir(dp->dir) < 0) {
587 rb_sys_fail(
"closedir");
594check_closedir(
DIR *dirp)
596 if (closedir(dirp) < 0)
597 rb_sys_fail(
"closedir");
601dir_initialize(rb_execution_context_t *ec,
VALUE dir,
VALUE dirname,
VALUE enc)
610 dirname = rb_str_encode_ospath(dirname);
617 path = RSTRING_PTR(dirname);
618 dp->dir = opendir_without_gvl(path);
619 if (dp->dir == NULL) {
621 if (rb_gc_for_fd(e)) {
622 dp->dir = opendir_without_gvl(path);
624#ifdef HAVE_GETATTRLIST
626 u_int32_t attrbuf[1];
627 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0};
628 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
629 if (gvl_getattrlist(&args, path) == 0) {
630 dp->dir = opendir_without_gvl(path);
634 if (dp->dir == NULL) {
636 rb_syserr_fail_path(e, orig);
645dir_s_open(rb_execution_context_t *ec,
VALUE klass,
VALUE dirname,
VALUE enc)
650 dir_initialize(ec, dir, dirname, enc);
656dir_s_close(rb_execution_context_t *ec,
VALUE klass,
VALUE dir)
658 return dir_close(dir);
661# if defined(HAVE_FDOPENDIR) && defined(HAVE_DIRFD)
663nogvl_fdopendir(
void *fd)
665 return fdopendir((
int)(
VALUE)fd);
694 if (!(dp->dir = IO_WITHOUT_GVL(nogvl_fdopendir, (
void *)(
VALUE)
NUM2INT(fd)))) {
695 rb_sys_fail(
"fdopendir");
703#define dir_s_for_fd rb_f_notimplement
706NORETURN(
static void dir_closed(
void));
717 rb_check_frozen(dir);
724 struct dir_data *dirp = dir_get(dir);
725 if (!dirp->dir) dir_closed();
729#define GetDIR(obj, dirp) ((dirp) = dir_check(obj))
742dir_inspect(
VALUE dir)
747 if (!
NIL_P(dirp->path)) {
761#if defined(__sun) && !defined(HAVE_DIRFD)
762# if defined(HAVE_DIR_D_FD)
763# define dirfd(x) ((x)->d_fd)
765# elif defined(HAVE_DIR_DD_FD)
766# define dirfd(x) ((x)->dd_fd)
793 fd = dirfd(dirp->dir);
795 rb_sys_fail(
"dirfd");
799#define dir_fileno rb_f_notimplement
846fundamental_encoding_p(rb_encoding *enc)
848 switch (rb_enc_to_index(enc)) {
849 case ENCINDEX_ASCII_8BIT:
850 case ENCINDEX_US_ASCII:
857# define READDIR(dir, enc) rb_w32_readdir((dir), (enc))
858# define READDIR_NOGVL READDIR
860NORETURN(
static void *readdir_failure(
void *err));
862readdir_failure(
void *err)
868nogvl_readdir(
void *dir)
871 if ((dir = readdir(dir)) == NULL) {
879# define READDIR(dir, enc) IO_WITHOUT_GVL(nogvl_readdir, (void *)(dir))
880# define READDIR_NOGVL(dir, enc) nogvl_readdir((dir))
885to_be_skipped(
const struct dirent *dp)
887 const char *name = dp->d_name;
888 if (name[0] !=
'.')
return FALSE;
889#ifdef HAVE_DIRENT_NAMLEN
890 switch (NAMLEN(dp)) {
892 if (name[1] !=
'.')
return FALSE;
899 if (!name[1])
return TRUE;
900 if (name[1] !=
'.')
return FALSE;
901 if (!name[2])
return TRUE;
928 if ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
951static int do_lstat(
int fd,
const char *path,
struct stat *pst,
int flags, rb_encoding *enc);
958 switch (dir_entry->dp->d_type) {
961 type = sym_block_device;
966 type = sym_character_device;
970 type = sym_directory;
999 if (do_lstat(dirfd(dir_entry->dirp->dir), dir_entry->dp->d_name, &st, 0, rb_filesystem_encoding()) == 0) {
1000 switch (st.st_mode & S_IFMT) {
1002 type = sym_directory;
1022 type = sym_block_device;
1027 type = sym_character_device;
1068 return dir_each_entry(dir, dir_yield,
Qnil, FALSE);
1076 IF_NORMALIZE_UTF8PATH(
int norm_p);
1079 rewinddir(dirp->dir);
1080 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp->dir, RSTRING_PTR(dirp->path)));
1081 while ((dp = READDIR(dirp->dir, dirp->enc)) != NULL) {
1082 const char *name = dp->d_name;
1083 size_t namlen = NAMLEN(dp);
1086 if (children_only && name[0] ==
'.') {
1087 if (namlen == 1)
continue;
1088 if (namlen == 2 && name[1] ==
'.')
continue;
1090#if NORMALIZE_UTF8PATH
1091 if (norm_p && has_nonascii(name, namlen) &&
1092 !
NIL_P(path = rb_str_normalize_ospath(name, namlen))) {
1093 path = rb_external_str_with_enc(path, dirp->enc);
1102 (*each)(arg, path, &each_args);
1104 if (!dirp->dir) dir_closed();
1130 if((pos = telldir(dirp->dir)) < 0)
1131 rb_sys_fail(
"telldir");
1132 return rb_int2inum(pos);
1135#define dir_tell rb_f_notimplement
1166 seekdir(dirp->dir, p);
1170#define dir_seek rb_f_notimplement
1201#define dir_set_pos rb_f_notimplement
1220dir_rewind(
VALUE dir)
1225 rewinddir(dirp->dir);
1248 dirp = dir_get(dir);
1249 if (!dirp->dir)
return Qnil;
1250 close_dir_data(dirp);
1256nogvl_chdir(
void *ptr)
1258 const char *path = ptr;
1260 return (
void *)(
VALUE)chdir(path);
1264dir_chdir0(
VALUE path)
1266 if (IO_WITHOUT_GVL_INT(nogvl_chdir, (
void*)RSTRING_PTR(path)) < 0)
1267 rb_sys_fail_path(path);
1271 rb_thread_t *thread;
1276 .blocking = 0, .thread = NULL,
1277 .path =
Qnil, .line = 0,
1283 if (chdir_lock.blocking == 0) {
1284 VALUE path = rb_source_location(&chdir_lock.line);
1291 chdir_lock.blocking++;
1292 if (chdir_lock.thread == NULL) {
1300 chdir_lock.blocking--;
1301 if (chdir_lock.blocking == 0) {
1302 chdir_lock.thread = NULL;
1303 chdir_lock.path =
Qnil;
1304 chdir_lock.line = 0;
1309chdir_alone_block_p(
void)
1312 if (chdir_lock.blocking > 0) {
1314 rb_raise(
rb_eRuntimeError,
"conflicting chdir during another chdir block");
1316 if (!
NIL_P(chdir_lock.path)) {
1317 rb_warn(
"conflicting chdir during another chdir block\n"
1318 "%" PRIsVALUE
":%d: note: previous chdir was here",
1319 chdir_lock.path, chdir_lock.line);
1322 rb_warn(
"conflicting chdir during another chdir block");
1330 VALUE old_path, new_path;
1339 dir_chdir0(args->new_path);
1346chdir_restore(
VALUE v)
1351 dir_chdir0(args->old_path);
1357chdir_path(
VALUE path,
bool yield_path)
1359 if (chdir_alone_block_p()) {
1363 args.new_path = path;
1365 args.yield_path = yield_path;
1369 char *p = RSTRING_PTR(path);
1370 int r = IO_WITHOUT_GVL_INT(nogvl_chdir, p);
1372 rb_sys_fail_path(path);
1447 path = rb_str_encode_ospath(rb_get_path(argv[0]));
1450 const char *dist = getenv(
"HOME");
1452 dist = getenv(
"LOGDIR");
1453 if (!dist) rb_raise(rb_eArgError,
"HOME/LOGDIR not set");
1458 return chdir_path(path,
true);
1461#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1463nogvl_fchdir(
void *ptr)
1465 const int *fd = ptr;
1467 return (
void *)(
VALUE)fchdir(*fd);
1473 if (IO_WITHOUT_GVL_INT(nogvl_fchdir, (
void *)&fd) < 0)
1474 rb_sys_fail(
"fchdir");
1484fchdir_yield(
VALUE v)
1486 struct fchdir_data *args = (
void *)v;
1487 dir_fchdir(args->fd);
1494fchdir_restore(
VALUE v)
1496 struct fchdir_data *args = (
void *)v;
1499 dir_fchdir(
RB_NUM2INT(dir_fileno(args->old_dir)));
1501 dir_close(args->old_dir);
1562 if (chdir_alone_block_p()) {
1563 struct fchdir_data args;
1564 args.old_dir = dir_s_alloc(klass);
1565 dir_initialize(NULL, args.old_dir, rb_fstring_cstr(
"."),
Qnil);
1571 int r = IO_WITHOUT_GVL_INT(nogvl_fchdir, &fd);
1573 rb_sys_fail(
"fchdir");
1579#define dir_s_fchdir rb_f_notimplement
1608#if defined(HAVE_FCHDIR) && defined(HAVE_DIRFD) && HAVE_FCHDIR && HAVE_DIRFD
1609 return dir_s_fchdir(
rb_cDir, dir_fileno(dir));
1611 return chdir_path(dir_get(dir)->path,
false);
1615static VALUE last_cwd;
1619getcwd_to_str(
VALUE arg)
1621 const char *path = (
const char *)arg;
1623 return rb_str_normalize_ospath(path, strlen(path));
1630getcwd_xfree(
VALUE arg)
1637rb_dir_getwd_ospath_slowpath(
void)
1644rb_dir_getwd_ospath(
void)
1646 char buf[PATH_MAX < LONG_MAX ? PATH_MAX : -1];
1647 char *path = getcwd(buf, PATH_MAX);
1649 return rb_dir_getwd_ospath_slowpath();
1651 size_t len = strlen(path);
1654 VALUE cached_cwd = RUBY_ATOMIC_VALUE_LOAD(last_cwd);
1656 if (!cached_cwd || (
size_t)RSTRING_LEN(cached_cwd) !=
len ||
1657 memcmp(RSTRING_PTR(cached_cwd), path,
len) != 0) {
1659 cached_cwd = rb_str_normalize_ospath(path, (
rb_len_t)
len);
1675 int fsenc = rb_enc_to_index(fs);
1679 case ENCINDEX_US_ASCII:
1680 fsenc = ENCINDEX_ASCII_8BIT;
1681 case ENCINDEX_ASCII_8BIT:
1683#if defined _WIN32 || defined __APPLE__
1688 return rb_enc_associate_index(cwd, fsenc);
1702dir_s_getwd(
VALUE dir)
1708check_dirname(
VALUE dir)
1716 enc = rb_enc_get(d);
1719 pend = rb_enc_path_end(rb_enc_path_skip_prefix(path, pend, enc), pend, enc);
1720 if (pend - path <
len) {
1724 return rb_str_encode_ospath(d);
1727#if defined(HAVE_CHROOT)
1729nogvl_chroot(
void *dirname)
1731 return (
void *)(
VALUE)chroot((
const char *)dirname);
1749 path = check_dirname(path);
1750 if (IO_WITHOUT_GVL_INT(nogvl_chroot, (
void *)RSTRING_PTR(path)) == -1)
1751 rb_sys_fail_path(path);
1756#define dir_s_chroot rb_f_notimplement
1765nogvl_mkdir(
void *ptr)
1769 return (
void *)(
VALUE)mkdir(m->path, m->mode);
1796 if (
rb_scan_args(argc, argv,
"11", &path, &vmode) == 2) {
1803 path = check_dirname(path);
1804 m.path = RSTRING_PTR(path);
1805 r = IO_WITHOUT_GVL_INT(nogvl_mkdir, &m);
1807 rb_sys_fail_path(path);
1813nogvl_rmdir(
void *ptr)
1815 const char *path = ptr;
1817 return (
void *)(
VALUE)rmdir(path);
1845 dir = check_dirname(dir);
1846 p = RSTRING_PTR(dir);
1847 r = IO_WITHOUT_GVL_INT(nogvl_rmdir, (
void *)p);
1849 rb_sys_fail_path(dir);
1855#ifdef RUBY_FUNCTION_NAME_STRING
1862#ifndef RUBY_FUNCTION_NAME_STRING
1863#define sys_enc_warning_in(func, mesg, enc) sys_enc_warning(mesg, enc)
1867sys_warning_1(
VALUE mesg)
1870#ifdef RUBY_FUNCTION_NAME_STRING
1871 rb_sys_enc_warning(arg->enc,
"%s: %s", arg->func, arg->mesg);
1873 rb_sys_enc_warning(arg->enc,
"%s", arg->mesg);
1879sys_enc_warning_in(
const char *func,
const char *mesg, rb_encoding *enc)
1882#ifdef RUBY_FUNCTION_NAME_STRING
1887 rb_protect(sys_warning_1, (
VALUE)&arg, 0);
1890#define GLOB_VERBOSE (1U << (sizeof(int) * CHAR_BIT - 1))
1891#define sys_warning(val, enc) \
1892 ((flags & GLOB_VERBOSE) ? sys_enc_warning_in(RUBY_FUNCTION_NAME_STRING, (val), (enc)) :(void)0)
1895glob_alloc_n(
size_t x,
size_t y)
1897 return malloc(rbimpl_size_mul_or_raise(x, y));
1901glob_realloc_n(
void *p,
size_t x,
size_t y)
1903 return realloc(p, rbimpl_size_mul_or_raise(x, y));
1906#define GLOB_ALLOC(type) ((type *)malloc(sizeof(type)))
1907#define GLOB_ALLOC_N(type, n) ((type *)glob_alloc_n(sizeof(type), n))
1908#define GLOB_REALLOC(ptr, size) realloc((ptr), (size))
1909#define GLOB_REALLOC_N(ptr, n) glob_realloc_n(ptr, sizeof(*(ptr)), n)
1910#define GLOB_FREE(ptr) free(ptr)
1911#define GLOB_JUMP_TAG(status) (((status) == -1) ? rb_memerror() : rb_jump_tag(status))
1917ALWAYS_INLINE(
static int to_be_ignored(
int e));
1921 return e == ENOENT || e == ENOTDIR;
1925#define STAT(args) (int)(VALUE)nogvl_stat(&(args))
1926#define LSTAT(args) (int)(VALUE)nogvl_lstat(&(args))
1928#define STAT(args) IO_WITHOUT_GVL_INT(nogvl_stat, (void *)&(args))
1929#define LSTAT(args) IO_WITHOUT_GVL_INT(nogvl_lstat, (void *)&(args))
1932typedef int ruby_glob_errfunc(
const char*,
VALUE,
const void*,
int);
1935 ruby_glob_errfunc *error;
1939at_subpath(
int fd,
size_t baselen,
const char *path)
1942 if (fd != (
int)AT_FDCWD && baselen > 0) {
1944 if (*path ==
'/') ++path;
1947 return *path ? path :
".";
1951struct fstatat_args {
1959nogvl_fstatat(
void *args)
1961 struct fstatat_args *arg = (
struct fstatat_args *)args;
1962 return (
void *)(
VALUE)fstatat(arg->fd, arg->path, arg->pst, arg->flag);
1971nogvl_stat(
void *args)
1974 return (
void *)(
VALUE)stat(arg->path, arg->pst);
1980do_stat(
int fd,
const char *path,
struct stat *pst,
int flags, rb_encoding *enc)
1983 struct fstatat_args args;
1988 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (
void *)&args);
1993 int ret = STAT(args);
1995 if (ret < 0 && !to_be_ignored(
errno))
1996 sys_warning(path, enc);
2001#if defined HAVE_LSTAT || defined lstat || USE_OPENDIR_AT
2004nogvl_lstat(
void *args)
2007 return (
void *)(
VALUE)lstat(arg->path, arg->pst);
2012do_lstat(
int fd,
const char *path,
struct stat *pst,
int flags, rb_encoding *enc)
2015 struct fstatat_args args;
2019 args.flag = AT_SYMLINK_NOFOLLOW;
2020 int ret = IO_WITHOUT_GVL_INT(nogvl_fstatat, (
void *)&args);
2025 int ret = LSTAT(args);
2027 if (ret < 0 && !to_be_ignored(
errno))
2028 sys_warning(path, enc);
2033#define do_lstat do_stat
2042with_gvl_gc_for_fd(
void *ptr)
2046 return (
void *)RBOOL(rb_gc_for_fd(*e));
2050gc_for_fd_with_gvl(
int e)
2055 return RBOOL(rb_gc_for_fd(e));
2059nogvl_opendir_at(
void *ptr)
2065 const int opendir_flags = (O_RDONLY|O_CLOEXEC|
2070 int fd = openat(oaa->basefd, oaa->path, opendir_flags);
2072 dirp = fd >= 0 ? fdopendir(fd) : 0;
2076 switch (gc_for_fd_with_gvl(e)) {
2078 if (fd < 0) fd = openat(oaa->basefd, oaa->path, opendir_flags);
2079 if (fd >= 0) dirp = fdopendir(fd);
2080 if (dirp)
return dirp;
2085 if (fd >= 0) close(fd);
2090 dirp = opendir(oaa->path);
2091 if (!dirp && gc_for_fd_with_gvl(
errno))
2092 dirp = opendir(oaa->path);
2099opendir_at(
int basefd,
const char *path)
2103 oaa.basefd = basefd;
2107 return IO_WITHOUT_GVL(nogvl_opendir_at, &oaa);
2109 return nogvl_opendir_at(&oaa);
2113do_opendir(
const int basefd,
size_t baselen,
const char *path,
int flags, rb_encoding *enc,
2114 ruby_glob_errfunc *errfunc,
VALUE arg,
int *status)
2119 if (!fundamental_encoding_p(enc)) {
2120 tmp = rb_enc_str_new(path, strlen(path), enc);
2121 tmp = rb_str_encode_ospath(tmp);
2122 path = RSTRING_PTR(tmp);
2125 dirp = opendir_at(basefd, at_subpath(basefd, baselen, path));
2130 if (!to_be_ignored(e)) {
2132 *status = (*errfunc)(path, arg, enc, e);
2135 sys_warning(path, enc);
2140 if (tmp) rb_str_resize(tmp, 0);
2147enum glob_pattern_type { PLAIN, ALPHA, BRACE, MAGICAL, RECURSIVE, MATCH_ALL, MATCH_DIR };
2150static enum glob_pattern_type
2151has_magic(
const char *p,
const char *pend,
int flags, rb_encoding *enc)
2153 const int escape = !(flags & FNM_NOESCAPE);
2159 while (p < pend && (c = *p++) != 0) {
2171 if (escape && p++ >= pend)
2186 p = Next(p-1, pend, enc);
2189 return hasmagical ? MAGICAL : hasalpha ? ALPHA : PLAIN;
2194find_dirsep(
const char *p,
const char *pend,
int flags, rb_encoding *enc)
2196 const int escape = !(flags & FNM_NOESCAPE);
2201 while ((c = *p++) != 0) {
2223 if (escape && !(c = *p++))
2228 p = Next(p-1, pend, enc);
2236remove_backslashes(
char *p,
register const char *pend, rb_encoding *enc)
2244 memmove(t, s, p - s);
2255 memmove(t, s, p - s);
2262 enum glob_pattern_type type;
2263 bool follow_symlinks;
2267static void glob_free_pattern(
struct glob_pattern *list);
2271glob_recursive_length(
const char *p,
const char *e)
2273 if (e - p < 3 || p[0] !=
'*' || p[1] !=
'*')
return 0;
2274 if (p[2] ==
'/')
return 3;
2275 if (e - p >= 4 && p[2] ==
'*' && p[3] ==
'/')
return 4;
2280glob_make_pattern(
const char *p,
const char *e,
int flags, rb_encoding *enc)
2286 while (p < e && *p) {
2287 const char *start = p;
2288 bool follow_symlinks =
false;
2291 while ((
len = glob_recursive_length(p, e)) != 0) {
2292 follow_symlinks |=
len == 4;
2294 while (p < e && *p ==
'/') p++;
2297 if (!tmp)
goto error;
2298 tmp->follow_symlinks = follow_symlinks;
2300 tmp->type = RECURSIVE;
2306 const char *m = find_dirsep(p, e, flags, enc);
2307 const enum glob_pattern_type magic = has_magic(p, m, flags, enc);
2308 const enum glob_pattern_type non_magic = (USE_NAME_ON_FS || FNM_SYSCASE) ? PLAIN : ALPHA;
2311 if (!(FNM_SYSCASE || magic > non_magic) && !recursive && *m) {
2313 while (has_magic(m+1, m2 = find_dirsep(m+1, e, flags, enc), flags, enc) <= non_magic &&
2318 buf = GLOB_ALLOC_N(
char, m-p+1);
2323 memcpy(buf, p, m-p);
2325 tmp->type = magic > MAGICAL ? MAGICAL : magic > non_magic ? magic : PLAIN;
2344 tmp->follow_symlinks =
false;
2345 tmp->type = dirsep ? MATCH_DIR : MATCH_ALL;
2354 glob_free_pattern(list);
2365 GLOB_FREE(tmp->str);
2371join_path(
const char *path,
size_t len,
int dirsep,
const char *name,
size_t namlen)
2373 char *buf = GLOB_ALLOC_N(
char,
len+namlen+(dirsep?1:0)+1);
2376 memcpy(buf, path,
len);
2380 memcpy(buf+
len, name, namlen);
2381 buf[
len+namlen] =
'\0';
2385#ifdef HAVE_GETATTRLIST
2386# if defined HAVE_FGETATTRLIST
2387# define is_case_sensitive(dirp, path) is_case_sensitive(dirp)
2389# define is_case_sensitive(dirp, path) is_case_sensitive(path)
2392is_case_sensitive(
DIR *dirp,
const char *path)
2396 vol_capabilities_attr_t cap[1];
2398 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, 0, ATTR_VOL_INFO|ATTR_VOL_CAPABILITIES};
2399 const vol_capabilities_attr_t *
const cap = attrbuf[0].cap;
2400 const int idx = VOL_CAPABILITIES_FORMAT;
2401 const uint32_t mask = VOL_CAP_FMT_CASE_SENSITIVE;
2402 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2403# if defined HAVE_FGETATTRLIST
2404 int ret = gvl_fgetattrlist(&args, dirfd(dirp));
2406 int ret = gvl_getattrlist(&args, path);
2411 if (!(cap->valid[idx] & mask))
2413 return (cap->capabilities[idx] & mask) != 0;
2417replace_real_basename(
char *path,
long base, rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2421 attrreference_t ref[1];
2422 fsobj_type_t objtype;
2423 char path[MAXPATHLEN * 3];
2425 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME|ATTR_CMN_OBJTYPE};
2426 const attrreference_t *
const ar = attrbuf[0].ref;
2430 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
2433 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, FSOPT_NOFOLLOW);
2434 if (gvl_getattrlist(&args, path)) {
2435 if (!to_be_ignored(
errno))
2436 sys_warning(path, enc);
2440 switch (attrbuf[0].objtype) {
2441 case VREG: *
type = path_regular;
break;
2442 case VDIR: *
type = path_directory;
break;
2443 case VLNK: *
type = path_symlink;
break;
2444 default: *
type = path_exist;
break;
2446 name = (
char *)ar + ar->attr_dataoffset;
2447 len = (long)ar->attr_length - 1;
2448 if (name +
len > (
char *)attrbuf +
sizeof(attrbuf))
2451# if NORMALIZE_UTF8PATH
2452 if (norm_p && has_nonascii(name,
len)) {
2453 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name,
len))) {
2459 tmp = GLOB_REALLOC(path, base +
len + 1);
2462 memcpy(path + base, name,
len);
2463 path[base +
len] =
'\0';
2465 IF_NORMALIZE_UTF8PATH(
if (!
NIL_P(utf8str)) rb_str_resize(utf8str, 0));
2469VALUE rb_w32_conv_from_wchar(
const WCHAR *wstr, rb_encoding *enc);
2470int rb_w32_reparse_symlink_p(
const WCHAR *path);
2473replace_real_basename(
char *path,
long base, rb_encoding *enc,
int norm_p,
int flags, rb_pathtype_t *
type)
2475 char *plainname = path;
2476 volatile VALUE tmp = 0;
2477 WIN32_FIND_DATAW fd;
2478 WIN32_FILE_ATTRIBUTE_DATA fa;
2480 HANDLE h = INVALID_HANDLE_VALUE;
2483 if (!fundamental_encoding_p(enc)) {
2485 tmp = rb_str_encode_ospath(tmp);
2486 plainname = RSTRING_PTR(tmp);
2488 wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
2489 if (tmp) rb_str_resize(tmp, 0);
2490 if (!wplain)
return path;
2491 if (GetFileAttributesExW(wplain, GetFileExInfoStandard, &fa)) {
2492 h = FindFirstFileW(wplain, &fd);
2493 e = rb_w32_map_errno(GetLastError());
2495 if (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
2496 if (!rb_w32_reparse_symlink_p(wplain))
2497 fa.dwFileAttributes &= ~FILE_ATTRIBUTE_REPARSE_POINT;
2500 if (h == INVALID_HANDLE_VALUE) {
2502 if (e && !to_be_ignored(e)) {
2504 sys_warning(path, enc);
2510 (fa.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) ? path_symlink :
2511 (fa.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? path_directory :
2515 tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
2516 wlen = RSTRING_LEN(tmp);
2517 buf = GLOB_REALLOC(path, base + wlen + 1);
2520 memcpy(path + base, RSTRING_PTR(tmp), wlen);
2521 path[base + wlen] = 0;
2523 rb_str_resize(tmp, 0);
2527 wlen = WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, NULL, 0, NULL, NULL);
2528 utf8filename = GLOB_REALLOC(0, wlen);
2531 WideCharToMultiByte(CP_UTF8, 0, fd.cFileName, -1, utf8filename, wlen, NULL, NULL);
2532 buf = GLOB_REALLOC(path, base + wlen + 1);
2535 memcpy(path + base, utf8filename, wlen);
2536 path[base + wlen] = 0;
2538 GLOB_FREE(utf8filename);
2543#elif USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2544# error not implemented
2548# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
2553# define S_ISLNK(m) (0)
2555# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
2560 void (*func)(
const char *,
VALUE,
void *);
2568#define glob_call_func(func, path, arg, enc) (*(func))((path), (arg), (void *)(enc))
2571glob_func_caller(
VALUE val)
2575 glob_call_func(args->func, args->path, args->value, args->enc);
2586glob_func_warning(
VALUE val)
2589 rb_syserr_enc_warning(arg->error, arg->enc,
"%s", arg->path);
2595rb_glob_warning(
const char *path,
VALUE a,
const void *enc,
int error)
2603 rb_protect(glob_func_warning, (
VALUE)&args, &status);
2608NORETURN(
static VALUE glob_func_error(
VALUE val));
2611glob_func_error(
VALUE val)
2620rb_glob_error(
const char *path,
VALUE a,
const void *enc,
int error)
2631 errfunc = glob_func_warning;
2636 rb_protect(errfunc, (
VALUE)&args, &status);
2644 const char *d_altname;
2650dirent_match(
const char *pat, rb_encoding *enc,
const char *name,
const rb_dirent_t *dp,
int flags)
2652 if (fnmatch(pat, enc, name, flags) == 0)
return 1;
2654 if (dp->d_altname && (flags & FNM_SHORTNAME)) {
2655 if (fnmatch(pat, enc, dp->d_altname, flags) == 0)
return 1;
2668glob_directory_seen(
const struct glob_directory *directory,
const struct stat *st)
2670 for (; directory; directory = directory->parent) {
2671 if (directory->st.st_dev == st->st_dev && directory->st.st_ino == st->st_ino
2672#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
2673 && directory->st.st_inohigh == st->st_inohigh
2686 rb_pathtype_t pathtype;
2695 const rb_dirent_t *dp;
2700dirent_match_brace(
const char *pattern,
VALUE val,
void *enc)
2704 return dirent_match(pattern, enc, arg->name, arg->dp, arg->flags);
2713 size_t path_len = 0;
2715 for (p = *beg; p; p = p->next) {
2719 str = p->follow_symlinks ?
"***" :
"**";
2730 size_t len = strlen(str);
2731 char *tmp = join_path(path ? path :
"", path_len, path != NULL, str,
len);
2736 if (!tmp)
return NULL;
2744static int push_caller(
const char *path,
VALUE val,
void *enc);
2747 rb_encoding *enc,
VALUE var);
2749static const size_t rb_dirent_name_offset =
2750 offsetof(rb_dirent_t, d_type) +
sizeof(uint8_t);
2753dirent_copy(
const struct dirent *dp, rb_dirent_t *rdp)
2755 if (!dp)
return NULL;
2756 size_t namlen = NAMLEN(dp);
2757 const size_t altlen =
2759 dp->d_altlen ? dp->d_altlen + 1 :
2762 rb_dirent_t *newrdp = rdp;
2763 if (!rdp && !(newrdp = malloc(rb_dirent_name_offset + namlen + 1 + altlen)))
2765 newrdp->d_namlen = namlen;
2767 char *name = (
char *)newrdp + rb_dirent_name_offset;
2768 memcpy(name, dp->d_name, namlen);
2769 name[namlen] =
'\0';
2771 newrdp->d_altname = NULL;
2773 char *
const altname = name + namlen + 1;
2774 memcpy(altname, dp->d_altname, altlen - 1);
2775 altname[altlen - 1] =
'\0';
2776 newrdp->d_altname = altname;
2779 newrdp->d_name = name;
2782 newrdp->d_name = dp->d_name;
2784 newrdp->d_altname = dp->d_altname;
2788 newrdp->d_type = dp->d_type;
2802 rb_dirent_t **entries;
2807glob_sort_cmp(
const void *a,
const void *b,
void *e)
2809 const rb_dirent_t *ent1 = *(
void **)a;
2810 const rb_dirent_t *ent2 = *(
void **)b;
2811 return strcmp(ent1->d_name, ent2->d_name);
2817 if (flags & FNM_GLOB_NOSORT) {
2818 check_closedir(ent->nosort.dirp);
2819 ent->nosort.dirp = NULL;
2821 else if (ent->sort.entries) {
2822 for (
size_t i = 0, count = ent->sort.count; i < count;) {
2823 GLOB_FREE(ent->sort.entries[i++]);
2825 GLOB_FREE(ent->sort.entries);
2826 ent->sort.entries = NULL;
2827 ent->sort.count = ent->sort.idx = 0;
2835 if (flags & FNM_GLOB_NOSORT) {
2836 ent->nosort.dirp = dirp;
2842 size_t count = 0, capacity = 0;
2843 ent->sort.count = 0;
2845 ent->sort.entries = 0;
2847 if ((capacity = dirp->nfiles) > 0) {
2848 if (!(newp = GLOB_ALLOC_N(rb_dirent_t, capacity))) {
2849 check_closedir(dirp);
2852 ent->sort.entries = newp;
2855 while ((dp = READDIR(dirp, enc)) != NULL) {
2856 rb_dirent_t *rdp = dirent_copy(dp, NULL);
2860 if (count >= capacity) {
2862 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, capacity))) {
2866 ent->sort.entries = newp;
2868 ent->sort.entries[count++] = rdp;
2869 ent->sort.count = count;
2871 check_closedir(dirp);
2872 if (count < capacity) {
2873 if (!(newp = GLOB_REALLOC_N(ent->sort.entries, count))) {
2874 glob_dir_finish(ent, 0);
2877 ent->sort.entries = newp;
2879 ruby_qsort(ent->sort.entries, ent->sort.count,
sizeof(ent->sort.entries[0]),
2880 glob_sort_cmp, NULL);
2885 glob_dir_finish(ent, 0);
2886 check_closedir(dirp);
2893 if (flags & FNM_GLOB_NOSORT) {
2894 return dirent_copy(READDIR(ent->nosort.dirp, enc), &ent->nosort.ent);
2896 else if (ent->sort.idx < ent->sort.count) {
2897 return ent->sort.entries[ent->sort.idx++];
2911 rb_pathtype_t pathtype,
2924 int plain = 0, brace = 0, magical = 0, recursive = 0, match_all = 0, match_dir = 0;
2925 int follow_symlink = 0;
2926 int escape = !(flags & FNM_NOESCAPE);
2927 size_t pathlen = baselen + namelen;
2929 rb_check_stack_overflow();
2931 for (cur = beg; cur < end; ++cur) {
2933 if (p->type == RECURSIVE) {
2935 if (p->follow_symlinks) follow_symlink = 1;
2943#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
2950 if (!recursive || strchr(p->str,
'/')) {
2964 rb_bug(
"continuous RECURSIVEs");
2970 char* brace_path = join_path_from_pattern(beg);
2971 if (!brace_path)
return -1;
2974 args.baselen = baselen;
2975 args.namelen = namelen;
2976 args.dirsep = dirsep;
2977 args.pathtype = pathtype;
2978 args.ancestors = ancestors;
2982 status = ruby_brace_expand(brace_path, flags, push_caller, (
VALUE)&args, enc,
Qfalse);
2983 GLOB_FREE(brace_path);
2988 if (match_all && pathtype == path_unknown) {
2989 if (do_lstat(fd, path, &st, flags, enc) == 0) {
2990 pathtype = IFTODT(st.st_mode);
2993 pathtype = path_noent;
2996 if (match_dir && (pathtype == path_unknown || pathtype == path_symlink)) {
2997 if (do_stat(fd, path, &st, flags, enc) == 0) {
2998 pathtype = IFTODT(st.st_mode);
3001 pathtype = path_noent;
3004 if (match_all && pathtype > path_noent) {
3005 const char *subpath = path + baselen + (baselen && path[baselen] ==
'/');
3006 status = glob_call_func(funcs->match, subpath, arg, enc);
3007 if (status)
return status;
3009 if (match_dir && pathtype == path_directory) {
3010 int seplen = (baselen && path[baselen] ==
'/');
3011 const char *subpath = path + baselen + seplen;
3012 char *tmp = join_path(subpath, namelen - seplen, dirsep,
"", 0);
3013 if (!tmp)
return -1;
3014 status = glob_call_func(funcs->match, tmp, arg, enc);
3016 if (status)
return status;
3020 if (pathtype == path_noent)
return 0;
3022 if ((follow_symlink || ancestors) && (magical || recursive || plain)) {
3023 if (do_stat(fd, at_subpath(fd, baselen, path), &directory.st, flags, enc) < 0 ||
3024 !S_ISDIR(directory.st.st_mode))
return 0;
3025 directory.parent = ancestors;
3026 ancestors = &directory;
3029 if (magical || recursive) {
3032 IF_NORMALIZE_UTF8PATH(
int norm_p);
3033 dirp = do_opendir(fd, baselen, path, flags, enc, funcs->error, arg, &status);
3035# if FNM_SYSCASE || NORMALIZE_UTF8PATH
3036 if ((magical < 2) && !recursive && (
errno == EACCES)) {
3043 IF_NORMALIZE_UTF8PATH(norm_p = need_normalization(dirp, *path ? path :
"."));
3045# if NORMALIZE_UTF8PATH
3046 if (!(norm_p || magical || recursive)) {
3047 check_closedir(dirp);
3051# ifdef HAVE_GETATTRLIST
3052 if (is_case_sensitive(dirp, path) == 0)
3053 flags |= FNM_CASEFOLD;
3056 if (!glob_opendir(&globent, dirp, flags, enc)) {
3059 status = (*funcs->error)(path, arg, enc, ENOMEM);
3062 sys_warning(path, enc);
3067 int skipdot = (flags & FNM_GLOB_SKIPDOT);
3068 flags |= FNM_GLOB_SKIPDOT;
3070 while ((dp = glob_getent(&globent, flags, enc)) != NULL) {
3072 rb_pathtype_t new_pathtype = path_unknown;
3076 int symlink_directory = 0;
3077 bool symlink_cycle =
false;
3078 IF_NORMALIZE_UTF8PATH(
VALUE utf8str =
Qnil);
3081 namlen = dp->d_namlen;
3082 if (name[0] ==
'.') {
3086 if (recursive && !(flags & FNM_DOTMATCH))
continue;
3087 if (skipdot)
continue;
3089 new_pathtype = path_directory;
3091 else if (namlen == 2 && name[1] ==
'.') {
3097# if NORMALIZE_UTF8PATH
3098 if (norm_p && has_nonascii(name, namlen)) {
3099 if (!
NIL_P(utf8str = rb_str_normalize_ospath(name, namlen))) {
3104 buf = join_path(path, pathlen, dirsep, name, namlen);
3105 IF_NORMALIZE_UTF8PATH(
if (!
NIL_P(utf8str)) rb_str_resize(utf8str, 0));
3110 name = buf + pathlen + (dirsep != 0);
3112 if (dp->d_type != DT_UNKNOWN) {
3114 new_pathtype = dp->d_type;
3117 if (recursive && dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1) &&
3118 new_pathtype == path_unknown) {
3120 if (do_lstat(fd, buf, &st, flags, enc) == 0)
3121 new_pathtype = IFTODT(st.st_mode);
3123 new_pathtype = path_noent;
3126 if (follow_symlink && new_pathtype == path_symlink &&
3127 dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1)) {
3128 symlink_directory = do_stat(fd, at_subpath(fd, baselen, buf), &st, flags, enc) == 0 &&
3129 S_ISDIR(st.st_mode);
3130 if (symlink_directory) symlink_cycle = glob_directory_seen(ancestors, &st);
3133 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, (end - beg) * 2);
3140 for (cur = beg; cur < end; ++cur) {
3143 if (p->type == RECURSIVE) {
3144 if (new_pathtype == path_directory ||
3145 new_pathtype == path_exist) {
3146 if (dotfile < ((flags & FNM_DOTMATCH) ? 2 : 1))
3149 else if (p->follow_symlinks && symlink_directory) {
3150 if (!symlink_cycle) {
3153 else if (p->next->type == MATCH_DIR) {
3155 *new_end++ = p->next;
3165 if (ruby_brace_expand(p->str, flags, dirent_match_brace,
3167 *new_end++ = p->next;
3172 if (dirent_match(p->str, enc, name, dp, flags))
3173 *new_end++ = p->next;
3179 status = glob_helper(fd, buf, baselen, name - buf - baselen + namlen, 1,
3180 new_pathtype, new_beg, new_end,
3181 ancestors, flags, funcs, arg, enc);
3187 glob_dir_finish(&globent, flags);
3192# if FNM_SYSCASE || NORMALIZE_UTF8PATH
3195 copy_beg = copy_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
3196 if (!copy_beg)
return -1;
3197 for (cur = beg; cur < end; ++cur)
3198 *copy_end++ = (*cur)->type <= ALPHA ? *cur : 0;
3200 for (cur = copy_beg; cur < copy_end; ++cur) {
3202 rb_pathtype_t new_pathtype = path_unknown;
3205 size_t len = strlen((*cur)->str) + 1;
3206 name = GLOB_ALLOC_N(
char,
len);
3211 memcpy(name, (*cur)->str,
len);
3213 len = remove_backslashes(name, name+
len-1, enc) - name;
3215 new_beg = new_end = GLOB_ALLOC_N(
struct glob_pattern *, end - beg);
3221 *new_end++ = (*cur)->next;
3222 for (cur2 = cur + 1; cur2 < copy_end; ++cur2) {
3223 if (*cur2 && fnmatch((*cur2)->str, enc, name, flags) == 0) {
3224 *new_end++ = (*cur2)->next;
3229 buf = join_path(path, pathlen, dirsep, name,
len);
3236#if USE_NAME_ON_FS == USE_NAME_ON_FS_REAL_BASENAME
3237 if ((*cur)->type == ALPHA) {
3238 buf = replace_real_basename(buf, pathlen + (dirsep != 0), enc,
3239 IF_NORMALIZE_UTF8PATH(1)+0,
3240 flags, &new_pathtype);
3244 status = glob_helper(fd, buf, baselen,
3245 namelen + strlen(buf + pathlen), 1,
3246 new_pathtype, new_beg, new_end,
3247 ancestors, flags, funcs, arg, enc);
3254 GLOB_FREE(copy_beg);
3261push_caller(
const char *path,
VALUE val,
void *enc)
3267 list = glob_make_pattern(path, path + strlen(path), arg->flags, enc);
3271 status = glob_helper(arg->fd, arg->path, arg->baselen, arg->namelen, arg->dirsep,
3272 arg->pathtype, &list, &list + 1, arg->ancestors, arg->flags, arg->funcs,
3274 glob_free_pattern(list);
3278static int ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
3290push_glob0_caller(
const char *path,
VALUE val,
void *enc)
3293 return ruby_glob0(path, arg->fd, arg->base, arg->flags, arg->funcs, arg->arg, enc);
3297ruby_glob0(
const char *path,
int fd,
const char *base,
int flags,
3302 const char *root, *start;
3304 size_t n, baselen = 0;
3305 int status, dirsep = FALSE;
3307 start = root = path;
3316 return ruby_brace_expand(path, flags, push_glob0_caller, (
VALUE)&args, enc,
Qfalse);
3319 flags |= FNM_SYSCASE;
3321 root = rb_enc_path_skip_prefix(root, root + strlen(root), enc);
3324 if (*root ==
'/') root++;
3333 buf = GLOB_ALLOC_N(
char, n + 1);
3334 if (!buf)
return -1;
3335 MEMCPY(buf, start,
char, n);
3338 list = glob_make_pattern(root, root + strlen(root), flags, enc);
3343 status = glob_helper(fd, buf, baselen, n-baselen, dirsep,
3344 path_unknown, &list, &list + 1,
3345 NULL, flags, funcs, arg, enc);
3346 glob_free_pattern(list);
3358 return ruby_glob0(path, AT_FDCWD, 0, flags & ~GLOB_VERBOSE,
3363rb_glob_caller(
const char *path,
VALUE a,
void *enc)
3369 rb_protect(glob_func_caller, a, &status);
3374 rb_glob_caller, rb_glob_error,
3378rb_glob(
const char *path,
void (*func)(
const char *,
VALUE,
void *),
VALUE arg)
3387 status = ruby_glob0(path, AT_FDCWD, 0, GLOB_VERBOSE, &rb_glob_funcs,
3388 (
VALUE)&args, args.enc);
3389 if (status) GLOB_JUMP_TAG(status);
3393push_pattern(
const char *path,
VALUE ary,
void *enc)
3395#if defined _WIN32 || defined __APPLE__
3407 rb_encoding *enc,
VALUE var)
3409 const int escape = !(flags & FNM_NOESCAPE);
3410 const char *p = str;
3411 const char *pend = p + strlen(p);
3413 const char *lbrace = 0, *rbrace = 0;
3414 int nest = 0, status = 0;
3417 if (*p ==
'{' && nest++ == 0) {
3420 if (*p ==
'}' && lbrace && --nest == 0) {
3424 if (*p ==
'\\' && escape) {
3430 if (lbrace && rbrace) {
3431 size_t len = strlen(s) + 1;
3432 char *buf = GLOB_ALLOC_N(
char,
len);
3435 if (!buf)
return -1;
3436 memcpy(buf, s, lbrace-s);
3439 while (p < rbrace) {
3440 const char *t = ++p;
3442 while (p < rbrace && !(*p ==
',' && nest == 0)) {
3443 if (*p ==
'{') nest++;
3444 if (*p ==
'}') nest--;
3445 if (*p ==
'\\' && escape) {
3446 if (++p == rbrace)
break;
3450 memcpy(buf+shift, t, p-t);
3451 strlcpy(buf+shift+(p-t), rbrace+1,
len-(shift+(p-t)));
3452 status = ruby_brace_expand(buf, flags, func, arg, enc, var);
3457 else if (!lbrace && !rbrace) {
3458 status = glob_call_func(func, s, arg, enc);
3472glob_brace(
const char *path,
VALUE val,
void *enc)
3476 return ruby_glob0(path, AT_FDCWD, 0, arg->flags, &arg->funcs, arg->value, enc);
3480ruby_brace_glob_with_enc(
const char *str,
int flags,
ruby_glob_func *func,
VALUE arg, rb_encoding *enc)
3484 flags &= ~GLOB_VERBOSE;
3485 args.funcs.match = func;
3486 args.funcs.error = 0;
3489 return ruby_brace_expand(str, flags, glob_brace, (
VALUE)&args, enc,
Qfalse);
3503 rb_encoding *enc = rb_enc_get(str);
3505#if defined _WIN32 || defined __APPLE__
3506 str = rb_str_encode_ospath(str);
3508 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3510 if (rb_enc_to_index(enc) == ENCINDEX_US_ASCII)
3512 flags |= GLOB_VERBOSE;
3513 args.func = push_pattern;
3520 struct dir_data *dirp = RTYPEDDATA_GET_DATA(base);
3521 if (!dirp->dir) dir_closed();
3523 if ((fd = dirfd(dirp->dir)) == -1)
3524 rb_sys_fail_path(dir_inspect(base));
3528 args.base = RSTRING_PTR(base);
3530#if defined _WIN32 || defined __APPLE__
3534 return ruby_glob0(RSTRING_PTR(str), fd, args.base, flags, &rb_glob_funcs,
3539rb_push_glob(
VALUE str,
VALUE base,
int flags)
3548 else if (!rb_str_to_cstr(str)) {
3549 rb_raise(rb_eArgError,
"nul-separated glob pattern is deprecated");
3556 status = push_glob(ary, str, base, flags);
3557 if (status) GLOB_JUMP_TAG(status);
3572 status = push_glob(ary, str, base, flags);
3573 if (status) GLOB_JUMP_TAG(status);
3581dir_glob_option_base(
VALUE base)
3583 if (NIL_OR_UNDEF_P(base)) {
3587 if (rb_typeddata_is_kind_of(base, &dir_data_type)) {
3592 if (!RSTRING_LEN(base))
return Qnil;
3597dir_glob_option_sort(
VALUE sort)
3599 return (rb_bool_expected(sort,
"sort", TRUE) ? 0 : FNM_GLOB_NOSORT);
3605 const int flags = dir_glob_option_sort(sort);
3606 base = dir_glob_option_base(base);
3608 return rb_push_glob(
RARRAY_AREF(args, 0), base, flags);
3610 return dir_globs(args, base, flags);
3617 const int flags = (
NUM2INT(rflags) | dir_glob_option_sort(sort)) & ~FNM_CASEFOLD;
3618 base = dir_glob_option_base(base);
3620 ary = rb_push_glob(str, base, flags);
3623 ary = dir_globs(ary, base, flags);
3634dir_open_dir(
int argc,
VALUE *argv)
3680 dir = dir_open_dir(argc, argv);
3681 rb_ensure(dir_each, dir, dir_close, dir);
3692dir_collect(
VALUE dir)
3695 dir_each_entry(dir, dir_entry_ary_push, ary, FALSE);
3721 dir = dir_open_dir(argc, argv);
3722 return rb_ensure(dir_collect, dir, dir_close, dir);
3726dir_each_child(
VALUE dir)
3728 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3740dir_s_each_child(
int argc,
VALUE *argv,
VALUE io)
3745 dir = dir_open_dir(argc, argv);
3746 rb_ensure(dir_each_child, dir, dir_close, dir);
3769dir_each_child_m(
VALUE dir)
3772 return dir_each_entry(dir, dir_yield,
Qnil, TRUE);
3787dir_collect_children(
VALUE dir)
3790 dir_each_entry(dir, dir_entry_ary_push, ary, TRUE);
3818dir_scan_children(
VALUE dir)
3821 dir_each_entry(dir, dir_yield_with_type,
Qnil, TRUE);
3826 dir_each_entry(dir, dir_yield_with_type, ary, TRUE);
3851dir_s_children(
int argc,
VALUE *argv,
VALUE io)
3855 dir = dir_open_dir(argc, argv);
3856 return rb_ensure(dir_collect_children, dir, dir_close, dir);
3898dir_s_scan(
int argc,
VALUE *argv,
VALUE klass)
3900 VALUE dir = dir_open_dir(argc, argv);
3901 return rb_ensure(dir_scan_children, dir, dir_close, dir);
3905fnmatch_brace(
const char *pattern,
VALUE val,
void *enc)
3908 VALUE path = arg->value;
3909 rb_encoding *enc_pattern = enc;
3910 rb_encoding *enc_path = rb_enc_get(path);
3912 if (enc_pattern != enc_path) {
3913 if (!rb_enc_asciicompat(enc_pattern))
3915 if (!rb_enc_asciicompat(enc_path))
3919 long len = strlen(pattern);
3921 enc_pattern, &cr) !=
len)
3927 return (fnmatch(pattern, enc, RSTRING_PTR(path), arg->flags) == 0);
3932file_s_fnmatch(
int argc,
VALUE *argv,
VALUE obj)
3934 VALUE pattern, path;
3938 if (
rb_scan_args(argc, argv,
"21", &pattern, &path, &rflags) == 3)
3946 if (flags & FNM_EXTGLOB) {
3951 if (ruby_brace_expand(RSTRING_PTR(pattern), flags, fnmatch_brace,
3952 (
VALUE)&args, rb_enc_get(pattern), pattern) > 0)
3956 rb_encoding *enc = rb_enc_compatible(pattern, path);
3958 if (fnmatch(RSTRING_PTR(pattern), enc, RSTRING_PTR(path), flags) == 0)
3985 user = (argc > 0) ? argv[0] :
Qnil;
3991 return rb_home_dir_of(user,
rb_str_new(0, 0));
3994 return rb_default_home_dir(
rb_str_new(0, 0));
4013rb_file_directory_p(
void)
4019nogvl_dir_empty_p(
void *ptr)
4021 const char *path = ptr;
4022 DIR *dir = opendir(path);
4028 switch (gc_for_fd_with_gvl(e)) {
4030 dir = opendir(path);
4035 if (e == ENOTDIR)
return (
void *)
Qfalse;
4039 while ((dp = READDIR_NOGVL(dir, NULL)) != NULL) {
4040 if (!to_be_skipped(dp)) {
4045 check_closedir(dir);
4046 return (
void *)result;
4069 enum {false_on_notdir = 1};
4073 dirname = rb_str_encode_ospath(dirname);
4075 path = RSTRING_PTR(dirname);
4077#if defined HAVE_GETATTRLIST && defined ATTR_DIR_ENTRYCOUNT
4079 u_int32_t attrbuf[SIZEUP32(fsobj_tag_t)];
4080 struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_OBJTAG,};
4081 struct getattrlist_args args = GETATTRLIST_ARGS(&al, attrbuf, 0);
4082 if (gvl_getattrlist(&args, path) != 0)
4083 rb_sys_fail_path(orig);
4084 if (*(
const fsobj_tag_t *)(attrbuf+1) == VT_HFS) {
4086 al.dirattr = ATTR_DIR_ENTRYCOUNT;
4087 if (gvl_getattrlist(&args, path) == 0) {
4088 if (attrbuf[0] >= 2 *
sizeof(u_int32_t))
4089 return RBOOL(attrbuf[1] == 0);
4090 if (false_on_notdir)
return Qfalse;
4092 rb_sys_fail_path(orig);
4097 result = (
VALUE)IO_WITHOUT_GVL(nogvl_dir_empty_p, (
void *)path);
4099 rb_syserr_fail_path((
int)
FIX2LONG(result), orig);
4107 sym_directory =
ID2SYM(rb_intern(
"directory"));
4108 sym_link =
ID2SYM(rb_intern(
"link"));
4109 sym_file =
ID2SYM(rb_intern(
"file"));
4110 sym_unknown =
ID2SYM(rb_intern(
"unknown"));
4112#if defined(DT_BLK) || (defined(HAVE_DIRFD) && defined(S_IFBLK))
4113 sym_block_device =
ID2SYM(rb_intern(
"blockSpecial"));
4115#if defined(DT_CHR) || (defined(HAVE_DIRFD) && defined(S_IFCHR))
4116 sym_character_device =
ID2SYM(rb_intern(
"characterSpecial"));
4118#if defined(DT_FIFO) || (defined(HAVE_DIRFD) && defined(S_IFIFO))
4119 sym_fifo =
ID2SYM(rb_intern(
"fifo"));
4121#if defined(DT_SOCK) || (defined(HAVE_DIRFD) && defined(S_IFSOCK))
4122 sym_socket =
ID2SYM(rb_intern(
"socket"));
4125 rb_gc_register_address(&chdir_lock.path);
4126 rb_gc_register_address(&last_cwd);
4175 rb_file_const(
"FNM_NOESCAPE",
INT2FIX(FNM_NOESCAPE));
4177 rb_file_const(
"FNM_PATHNAME",
INT2FIX(FNM_PATHNAME));
4179 rb_file_const(
"FNM_DOTMATCH",
INT2FIX(FNM_DOTMATCH));
4181 rb_file_const(
"FNM_CASEFOLD",
INT2FIX(FNM_CASEFOLD));
4183 rb_file_const(
"FNM_EXTGLOB",
INT2FIX(FNM_EXTGLOB));
4185 rb_file_const(
"FNM_SYSCASE",
INT2FIX(FNM_SYSCASE));
4187 rb_file_const(
"FNM_SHORTNAME",
INT2FIX(FNM_SHORTNAME));
#define RUBY_ATOMIC_VALUE_SET(var, val)
Identical to RUBY_ATOMIC_SET, except it expects its arguments are VALUE.
#define ISALPHA
@old{rb_isalpha}
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
int ruby_glob_func(const char *path, VALUE arg, void *enc)
Type of a glob callback function.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
int rb_block_given_p(void)
Determines if the current method is given a block.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define INT2FIX
Old name of RB_INT2FIX.
#define rb_str_cat2
Old name of rb_str_cat_cstr.
#define ID2SYM
Old name of RB_ID2SYM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define CLASS_OF
Old name of rb_class_of.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define INT2NUM
Old name of RB_INT2NUM.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define FIX2LONG
Old name of RB_FIX2LONG.
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
#define NUM2LONG
Old name of RB_NUM2LONG.
#define FIXNUM_P
Old name of RB_FIXNUM_P.
void rb_syserr_fail(int e, const char *mesg)
Raises appropriate exception that represents a C errno.
VALUE rb_eIOError
IOError exception.
void rb_syserr_fail_str(int e, VALUE mesg)
Identical to rb_syserr_fail(), except it takes the message in Ruby's String instead of C's.
VALUE rb_eRuntimeError
RuntimeError exception.
void * rb_check_typeddata(VALUE obj, const rb_data_type_t *data_type)
Identical to rb_typeddata_is_kind_of(), except it raises exceptions instead of returning false.
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
VALUE rb_cObject
Object class.
VALUE rb_mEnumerable
Enumerable module.
VALUE rb_cFile
File class.
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
#define RBIMPL_ASSERT_OR_ASSUME(...)
This is either RUBY_ASSERT or RBIMPL_ASSUME, depending on RUBY_DEBUG.
#define RUBY_DEBUG
Define this macro when you want assertions.
rb_encoding * rb_utf8_encoding(void)
Queries the encoding that represents UTF-8.
rb_encoding * rb_ascii8bit_encoding(void)
Queries the encoding that represents ASCII-8BIT a.k.a.
rb_encoding * rb_filesystem_encoding(void)
Queries the "filesystem" encoding.
rb_encoding * rb_default_internal_encoding(void)
Queries the "default internal" encoding.
static unsigned int rb_enc_codepoint(const char *p, const char *e, rb_encoding *enc)
Queries the code point of character pointed by the passed pointer.
rb_encoding * rb_usascii_encoding(void)
Queries the encoding that represents US-ASCII.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
rb_len_t rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
VALUE rb_external_str_new_with_enc(const char *ptr, rb_len_t len, rb_encoding *enc)
Identical to rb_external_str_new(), except it additionally takes an encoding.
VALUE rb_enc_str_new_cstr(const char *ptr, rb_encoding *enc)
Identical to rb_enc_str_new(), except it assumes the passed pointer is a pointer to a C string.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
VALUE rb_funcallv_kw(VALUE recv, ID mid, int argc, const VALUE *argv, int kw_splat)
Identical to rb_funcallv(), except you can specify how to handle the last element of the given array.
VALUE rb_funcallv(VALUE recv, ID mid, int argc, const VALUE *argv)
Identical to rb_funcall(), except it takes the method arguments as a C array.
VALUE rb_ary_each(VALUE ary)
Iteratively yields each element of the passed array to the implicitly passed block if any.
VALUE rb_check_array_type(VALUE obj)
Try converting an object to its array representation using its to_ary method, if any.
VALUE rb_ary_new(void)
Allocates a new, empty array.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
VALUE rb_dir_getwd(void)
Queries the path of the current working directory of the current process.
#define RETURN_ENUMERATOR(obj, argc, argv)
Identical to RETURN_SIZED_ENUMERATOR(), except its size is unknown.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
VALUE rb_str_subseq(VALUE str, rb_len_t beg, rb_len_t len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
#define rb_utf8_str_new_cstr(str)
Identical to rb_str_new_cstr, except it generates a string of "UTF-8" encoding.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
void rb_must_asciicompat(VALUE obj)
Asserts that the given string's encoding is (Ruby's definition of) ASCII compatible.
VALUE rb_str_freeze(VALUE str)
This is the implementation of String#freeze.
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
#define rb_str_new_cstr(str)
Identical to rb_str_new, except it assumes the passed pointer is a pointer to a C string.
VALUE rb_thread_current(void)
Obtains the "current" thread.
VALUE rb_class_name(VALUE obj)
Queries the name of the given object's class.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
int len
Length of the buffer.
#define RB_OBJ_SET_SHAREABLE(obj)
Wrapper of rb_obj_set_shareable().
static VALUE RB_OBJ_SET_FROZEN_SHAREABLE(VALUE obj)
Freezes and marks the object as shareable.
void * rb_thread_call_with_gvl(void *(*func)(void *), void *data1)
(Re-)acquires the GVL.
void ruby_qsort(void *, const size_t, const size_t, int(*)(const void *, const void *, void *), void *)
Reentrant implementation of quick sort.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define RB_NUM2INT
Just another name of rb_num2int_inline.
VALUE rb_yield_values(int n,...)
Identical to rb_yield(), except it takes variadic number of parameters and pass them to the block.
VALUE rb_yield_values2(int n, const VALUE *argv)
Identical to rb_yield_values(), except it takes the parameters as a C array instead of variadic argum...
VALUE rb_yield(VALUE val)
Yields the block.
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
VALUE type(ANYARGS)
ANYARGS-ed function type.
VALUE rb_ensure(type *q, VALUE w, type *e, VALUE r)
An equivalent of ensure clause.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
References an object in an array.
#define StringValue(v)
Ensures that the parameter object is a String.
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
struct rb_data_type_struct rb_data_type_t
This is the struct that holds necessary info for a struct.
#define TypedData_Make_Struct(klass, type, data_type, sval)
Identical to TypedData_Wrap_Struct, except it allocates a new data region internally instead of takin...
#define FilePathValue(v)
Ensures that the parameter object is a path.
int rb_errno(void)
Identical to system errno.
#define errno
Ractor-aware version of errno.
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
#define RB_PASS_CALLED_KEYWORDS
Pass keywords if current method is called with keywords, useful for argument delegation.
long rb_len_t
A signed integer type for lengths and indices of objects such as String and Array.
uintptr_t VALUE
Type that represents a Ruby object.
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.