14#include "ruby/internal/config.h"
17# include "missing/file.h"
26# include <sys/cygwin.h>
31# if !(defined(__has_feature) && defined(__has_attribute))
36# define API_AVAILABLE(...)
37# define API_DEPRECATED(...)
39# include <CoreFoundation/CFString.h>
56#ifdef HAVE_SYS_PARAM_H
57# include <sys/param.h>
60# define MAXPATHLEN 1024
65#elif defined HAVE_SYS_UTIME_H
66# include <sys/utime.h>
73#ifdef HAVE_SYS_SYSMACROS_H
74# include <sys/sysmacros.h>
80#ifdef HAVE_SYS_MKDEV_H
81# include <sys/mkdev.h>
84#if defined(HAVE_FCNTL_H)
88#if defined(HAVE_SYS_TIME_H)
92#if !defined HAVE_LSTAT && !defined lstat
98# include "win32/file.h"
99# define STAT(p, s) rb_w32_ustati128((p), (s))
101# define lstat(p, s) rb_w32_ulstati128((p), (s))
103# define access(p, m) rb_w32_uaccess((p), (m))
105# define truncate(p, n) rb_w32_utruncate((p), (n))
107# define chmod(p, m) rb_w32_uchmod((p), (m))
109# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
111# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
113# define utimensat(s, p, t, f) rb_w32_uutimensat((s), (p), (t), (f))
115# define link(f, t) rb_w32_ulink((f), (t))
117# define unlink(p) rb_w32_uunlink(p)
119# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
121# define rename(f, t) rb_w32_urename((f), (t))
123# define symlink(s, l) rb_w32_usymlink((s), (l))
131# define STAT(p, s) stat((p), (s))
134#ifdef HAVE_STRUCT_STATX_STX_BTIME
135# define ST_(name) stx_ ## name
138# define ST_(name) st_ ## name
142#if defined _WIN32 || defined __APPLE__
144# define TO_OSPATH(str) rb_str_encode_ospath(str)
147# define TO_OSPATH(str) (str)
151#if defined DOSISH || defined __CYGWIN__
156#if defined HAVE_REALPATH && defined __sun && defined __SVR4
169#include "internal/compilers.h"
170#include "internal/dir.h"
171#include "internal/error.h"
172#include "internal/file.h"
173#include "internal/io.h"
174#include "internal/load.h"
175#include "internal/object.h"
176#include "internal/process.h"
177#include "internal/thread.h"
178#include "internal/vm.h"
183#define UIANY2NUM(x) \
184 ((sizeof(x) <= sizeof(unsigned int)) ? \
185 UINT2NUM((unsigned)(x)) : \
186 (sizeof(x) <= sizeof(unsigned long)) ? \
187 ULONG2NUM((unsigned long)(x)) : \
188 ULL2NUM((unsigned LONG_LONG)(x)))
195file_path_convert(
VALUE name)
200 if (ENCINDEX_US_ASCII != fname_encidx &&
201 ENCINDEX_ASCII_8BIT != fname_encidx &&
202 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
203 rb_default_internal_encoding() &&
207 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
208 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
216check_path_encoding(
VALUE str)
219 if (!rb_enc_asciicompat(enc)) {
227rb_get_path_check_to_string(
VALUE obj)
236 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
242rb_get_path_check_convert(
VALUE obj)
244 obj = file_path_convert(obj);
246 check_path_encoding(obj);
247 if (!rb_str_to_cstr(obj)) {
248 rb_raise(rb_eArgError,
"path name contains null byte");
257 return rb_get_path(obj);
263 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
271#if 0 && defined _WIN32
272 if (encidx == ENCINDEX_ASCII_8BIT) {
273 encidx = rb_filesystem_encindex();
276 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
286# define NORMALIZE_UTF8PATH 1
288# ifdef HAVE_WORKING_FORK
289static CFMutableStringRef
290mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
292 const CFAllocatorRef alloc = kCFAllocatorDefault;
293 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
294 kCFStringEncodingUTF8, FALSE,
296 return CFStringCreateMutableCopy(alloc,
len, *s);
299# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
302rb_CFString_class_initialize_before_fork(
void)
325 const char small_str[] =
"/";
326 long len =
sizeof(small_str) - 1;
334 for (
int i = 0; i < 2; i++) {
335 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
336 mutable_CFString_release(m, s);
342rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
347 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
348 long oldlen = RSTRING_LEN(str);
350 CFStringNormalize(m, kCFStringNormalizationFormC);
351 all = CFRangeMake(0, CFStringGetLength(m));
352 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
354 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
355 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
357 mutable_CFString_release(m, s);
362rb_str_normalize_ospath(
const char *ptr,
long len)
365 const char *e = ptr +
len;
369 rb_enc_associate(str, enc);
373 int r = rb_enc_precise_mbclen(p, e, enc);
376 static const char invalid[3] =
"\xEF\xBF\xBD";
377 rb_str_append_normalized_ospath(str, p1, p-p1);
384 c = rb_enc_mbc_to_codepoint(p, e, enc);
385 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
386 (0x2F800 <= c && c <= 0x2FAFF)) {
388 rb_str_append_normalized_ospath(str, p1, p-p1);
399 rb_str_append_normalized_ospath(str, p1, p-p1);
406ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
409 if (p+3 > e)
return 0;
410 switch ((
unsigned char)*p) {
412 switch ((
unsigned char)p[1]) {
414 c = (
unsigned char)p[2];
416 if (c >= 0x8c && c <= 0x8f)
return 3;
418 if (c >= 0xaa && c <= 0xae)
return 3;
421 c = (
unsigned char)p[2];
423 if (c >= 0xaa && c <= 0xaf)
return 3;
429 if ((
unsigned char)p[1] == 0xbb &&
430 (
unsigned char)p[2] == 0xbf)
437# define NORMALIZE_UTF8PATH 0
440#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
451 int (*func)(
const char *,
void *);
457no_gvl_apply2files(
void *ptr)
461 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
462 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
471NORETURN(
static void utime_failed(
struct apply_arg *));
472static int utime_internal(
const char *,
void *);
476apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
480 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
488 for (aa->i = 0; aa->i < argc; aa->i++) {
489 VALUE path = rb_get_path(argv[aa->i]);
491 path = rb_str_encode_ospath(path);
492 aa->fn[aa->i].ptr = RSTRING_PTR(path);
493 aa->fn[aa->i].path = path;
496 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
499 if (func == utime_internal) {
503 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
522 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
526 rb_io_stat_data stat;
539rb_stat_new(
const struct stat *st)
545# define CP(m) .stx_ ## m = st->st_ ## m
546# define CP_32(m) .stx_ ## m = (__u32)st->st_ ## m
547# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
548 rb_st->stat = (
struct statx){
549 .stx_mask = STATX_BASIC_STATS,
566 rb_st->initialized =
true;
574rb_statx_new(
const rb_io_stat_data *st)
580 rb_st->initialized =
true;
586static rb_io_stat_data*
591 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
597statx_mtimespec(
const rb_io_stat_data *st)
599 return st->stx_mtime;
602# define statx_mtimespec stat_mtimespec
626 if (ts1.tv_sec == ts2.tv_sec) {
627 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
628 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
631 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
637#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
640# define NUM2DEVT(v) NUM2UINT(v)
643# define DEVT2NUM(v) UINT2NUM(v)
645#ifndef PRI_DEVT_PREFIX
646# define PRI_DEVT_PREFIX ""
660rb_stat_dev(
VALUE self)
663 unsigned int m = get_stat(self)->stx_dev_major;
664 unsigned int n = get_stat(self)->stx_dev_minor;
665 return DEVT2NUM(makedev(m, n));
666#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
667 return DEVT2NUM(get_stat(self)->st_dev);
668#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
669 return ULONG2NUM(get_stat(self)->st_dev);
671 return ULL2NUM(get_stat(self)->st_dev);
687rb_stat_dev_major(
VALUE self)
690 return UINT2NUM(get_stat(self)->stx_dev_major);
692 return UINT2NUM(major(get_stat(self)->st_dev));
710rb_stat_dev_minor(
VALUE self)
713 return UINT2NUM(get_stat(self)->stx_dev_minor);
715 return UINT2NUM(minor(get_stat(self)->st_dev));
732rb_stat_ino(
VALUE self)
734 rb_io_stat_data *ptr = get_stat(self);
735#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
737 return rb_integer_unpack(&ptr->st_ino, 2,
738 SIZEOF_STRUCT_STAT_ST_INO, 0,
742 return UIANY2NUM(ptr->ST_(ino));
760rb_stat_mode(
VALUE self)
762 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
778rb_stat_nlink(
VALUE self)
781 const rb_io_stat_data *ptr = get_stat(self);
783 return UIANY2NUM(ptr->ST_(nlink));
797rb_stat_uid(
VALUE self)
799 return UIDT2NUM(get_stat(self)->ST_(uid));
813rb_stat_gid(
VALUE self)
815 return GIDT2NUM(get_stat(self)->ST_(gid));
831rb_stat_rdev(
VALUE self)
834 unsigned int m = get_stat(self)->stx_rdev_major;
835 unsigned int n = get_stat(self)->stx_rdev_minor;
836 return DEVT2NUM(makedev(m, n));
837#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
839#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
840 return DEVT2NUM(get_stat(self)->ST_(rdev));
841#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
842 return ULONG2NUM(get_stat(self)->ST_(rdev));
844 return ULL2NUM(get_stat(self)->ST_(rdev));
860rb_stat_rdev_major(
VALUE self)
863 return UINT2NUM(get_stat(self)->stx_rdev_major);
864#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
865 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
883rb_stat_rdev_minor(
VALUE self)
886 return UINT2NUM(get_stat(self)->stx_rdev_minor);
887#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
888 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
904rb_stat_size(
VALUE self)
906 return OFFT2NUM(get_stat(self)->ST_(size));
921rb_stat_blksize(
VALUE self)
923#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
924 return ULONG2NUM(get_stat(self)->ST_(blksize));
942rb_stat_blocks(
VALUE self)
944#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
945# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
946 return ULL2NUM(get_stat(self)->ST_(blocks));
948 return ULONG2NUM(get_stat(self)->ST_(blocks));
956stat_atimespec(
const struct stat *st)
959 ts.tv_sec = st->st_atime;
960#if defined(HAVE_STRUCT_STAT_ST_ATIM)
961 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
962#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
963 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
964#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
965 ts.tv_nsec = (uint32_t)st->st_atimensec;
974statx_atimespec(
const rb_io_stat_data *st)
976 return st->stx_atime;
979# define statx_atimespec stat_atimespec
989stat_atime(
const struct stat *st)
991 return stat_time(stat_atimespec(st));
995stat_mtimespec(
const struct stat *st)
998 ts.tv_sec = st->st_mtime;
999#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1000 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1001#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1002 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1003#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1004 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1012stat_mtime(
const struct stat *st)
1014 return stat_time(stat_mtimespec(st));
1018stat_ctimespec(
const struct stat *st)
1021 ts.tv_sec = st->st_ctime;
1022#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1023 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1024#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1025 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1026#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1027 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1036statx_ctimespec(
const rb_io_stat_data *st)
1038 return st->stx_ctime;
1041# define statx_ctimespec stat_ctimespec
1045stat_ctime(
const struct stat *st)
1047 return stat_time(stat_ctimespec(st));
1050#define HAVE_STAT_BIRTHTIME
1051#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1053statx_birthtime(
const rb_io_stat_data *st)
1058#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1059static VALUE statx_birthtime(
const rb_io_stat_data *st);
1060#elif defined(_WIN32)
1061# define statx_birthtime stat_ctime
1063# undef HAVE_STAT_BIRTHTIME
1078rb_stat_atime(
VALUE self)
1080 return stat_time(statx_atimespec(get_stat(self)));
1094rb_stat_mtime(
VALUE self)
1096 return stat_time(statx_mtimespec(get_stat(self)));
1114rb_stat_ctime(
VALUE self)
1116 return stat_time(statx_ctimespec(get_stat(self)));
1119#if defined(HAVE_STAT_BIRTHTIME)
1143rb_stat_birthtime(
VALUE self)
1145 return statx_birthtime(get_stat(self));
1148# define rb_stat_birthtime rb_f_notimplement
1167rb_stat_inspect(
VALUE self)
1171 static const struct {
1175 {
"dev", rb_stat_dev},
1176 {
"ino", rb_stat_ino},
1177 {
"mode", rb_stat_mode},
1178 {
"nlink", rb_stat_nlink},
1179 {
"uid", rb_stat_uid},
1180 {
"gid", rb_stat_gid},
1181 {
"rdev", rb_stat_rdev},
1182 {
"size", rb_stat_size},
1183 {
"blksize", rb_stat_blksize},
1184 {
"blocks", rb_stat_blocks},
1185 {
"atime", rb_stat_atime},
1186 {
"mtime", rb_stat_mtime},
1187 {
"ctime", rb_stat_ctime},
1188#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1189 {
"birthtime", rb_stat_birthtime},
1195 if (!rb_st->initialized) {
1203 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1211 v = (*member[i].func)(self);
1213 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1215 else if (i == 0 || i == 6) {
1216 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1236no_gvl_fstat(
void *data)
1239 return (
VALUE)fstat(arg->file.fd, arg->st);
1243fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1247 data.file.fd = fptr->
fd;
1250 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1254no_gvl_stat(
void * data)
1257 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1261stat_without_gvl(
const char *path,
struct stat *st)
1265 data.file.path = path;
1268 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1271#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1272 defined(HAVE_STRUCT_STATX_STX_BTIME)
1274# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1277# ifdef HAVE_SYSCALL_H
1278# include <syscall.h>
1279# elif defined HAVE_SYS_SYSCALL_H
1280# include <sys/syscall.h>
1282# if defined __linux__
1283# include <linux/stat.h>
1285statx(
int dirfd,
const char *pathname,
int flags,
1286 unsigned int mask,
struct statx *statxbuf)
1288 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1293typedef struct no_gvl_rb_io_stat_data {
1299} no_gvl_rb_io_stat_data;
1302io_blocking_statx(
void *data)
1304 no_gvl_rb_io_stat_data *arg = data;
1305 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1309no_gvl_statx(
void *data)
1311 return (
void *)io_blocking_statx(data);
1315statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1317 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1320 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1324lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1326 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1329 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1333fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1335 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1338 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1341#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1344rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1349 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1354 result = fstatx_without_gvl(fptr, stx, mask);
1359 file = rb_str_encode_ospath(file);
1360 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1366# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1368NORETURN(
static void statx_notimplement(
const char *field_name));
1373statx_notimplement(
const char *field_name)
1376 "%s is unimplemented on this filesystem",
1381statx_birthtime(
const rb_io_stat_data *stx)
1383 if (!statx_has_birthtime(stx)) {
1385 statx_notimplement(
"birthtime");
1387 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1392# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1393# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1394# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1395# define rb_statx(file, stx, mask) rb_stat(file, stx)
1396# define STATX(path, st, mask) STAT(path, st)
1398#if defined(HAVE_STAT_BIRTHTIME)
1399# define statx_has_birthtime(st) 1
1401# define statx_has_birthtime(st) 0
1408# define FSTAT(fd, st) fstat(fd, st)
1417 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1422 result = fstat_without_gvl(fptr, st);
1427 file = rb_str_encode_ospath(file);
1428 result = stat_without_gvl(RSTRING_PTR(file), st);
1450 fname = rb_str_encode_ospath(fname);
1451 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1452 rb_sys_fail_path(fname);
1454 return rb_statx_new(&st);
1473rb_io_stat(
VALUE obj)
1479 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1480 rb_sys_fail_path(fptr->
pathv);
1482 return rb_statx_new(&st);
1487no_gvl_lstat(
void *ptr)
1490 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1494lstat_without_gvl(
const char *path,
struct stat *st)
1498 data.file.path = path;
1501 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1525 fname = rb_str_encode_ospath(fname);
1526 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1527 rb_sys_fail_path(fname);
1529 return rb_statx_new(&st);
1531 return rb_file_s_stat(klass, fname);
1550rb_file_lstat(
VALUE obj)
1559 path = rb_str_encode_ospath(fptr->
pathv);
1560 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1561 rb_sys_fail_path(fptr->
pathv);
1563 return rb_statx_new(&st);
1565 return rb_io_stat(obj);
1570rb_group_member(GETGROUPS_T gid)
1572#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1581 if (getgid() == gid || getegid() == gid)
1584 groups = getgroups(0, NULL);
1585 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1586 anum = getgroups(groups, gary);
1587 while (--anum >= 0) {
1588 if (gary[anum] == gid) {
1601# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1604#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1605#define USE_GETEUID 1
1610eaccess(
const char *path,
int mode)
1619 if (getuid() == euid && getgid() == getegid())
1620 return access(path, mode);
1622 if (STAT(path, &st) < 0)
1632 if (st.st_mode & S_IXUGO)
1638 if (st.st_uid == euid)
1640 else if (rb_group_member(st.st_gid))
1643 if ((
int)(st.st_mode & mode) == mode)
return 0;
1647 return access(path, mode);
1658nogvl_eaccess(
void *ptr)
1662 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1666rb_eaccess(
VALUE fname,
int mode)
1671 fname = rb_str_encode_ospath(fname);
1675 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1679nogvl_access(
void *ptr)
1683 return (
void *)(
VALUE)access(aa->path, aa->mode);
1687rb_access(
VALUE fname,
int mode)
1692 fname = rb_str_encode_ospath(fname);
1696 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1731# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1737 if (S_ISDIR(st.st_mode))
return Qtrue;
1758# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1764 if (S_ISFIFO(st.st_mode))
return Qtrue;
1787# define S_ISLNK(m) _S_ISLNK(m)
1790# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1793# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1803 fname = rb_str_encode_ospath(fname);
1805 if (S_ISLNK(st.st_mode))
return Qtrue;
1828# define S_ISSOCK(m) _S_ISSOCK(m)
1831# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1834# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1844 if (S_ISSOCK(st.st_mode))
return Qtrue;
1866# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1868# define S_ISBLK(m) (0)
1876 if (S_ISBLK(st.st_mode))
return Qtrue;
1896# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1902 if (S_ISCHR(st.st_mode))
return Qtrue;
1941 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
1956rb_file_readable_real_p(
VALUE obj,
VALUE fname)
1958 return RBOOL(rb_access(fname, R_OK) >= 0);
1962# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
1966# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
1986rb_file_world_readable_p(
VALUE obj,
VALUE fname)
1992 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
1993 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2013 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2028rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2030 return RBOOL(rb_access(fname, W_OK) >= 0);
2050rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2056 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2057 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2081 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2100rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2102 return RBOOL(rb_access(fname, X_OK) >= 0);
2106# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2127 return RBOOL(S_ISREG(st.st_mode));
2146 return RBOOL(st.st_size == 0);
2165 if (st.st_size == 0)
return Qnil;
2186 return RBOOL(st.st_uid == geteuid());
2195 return RBOOL(st.st_uid == getuid());
2216 if (rb_group_member(st.st_gid))
return Qtrue;
2221#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2223check3rdbyte(
VALUE fname,
int mode)
2228 return RBOOL(st.st_mode & mode);
2245 return check3rdbyte(fname, S_ISUID);
2264 return check3rdbyte(fname, S_ISGID);
2283 return check3rdbyte(fname, S_ISVTX);
2312 struct stat st1, st2;
2316 if (st1.st_dev != st2.st_dev)
return Qfalse;
2317 if (st1.st_ino != st2.st_ino)
return Qfalse;
2321 return rb_w32_file_identical_p(fname1, fname2);
2339 if (
rb_stat(fname, &st) < 0) {
2342 rb_syserr_fail_path(e, fname);
2348rb_file_ftype(mode_t mode)
2352 if (S_ISREG(mode)) {
2355 else if (S_ISDIR(mode)) {
2358 else if (S_ISCHR(mode)) {
2359 t =
"characterSpecial";
2362 else if (S_ISBLK(mode)) {
2367 else if (S_ISFIFO(mode)) {
2372 else if (S_ISLNK(mode)) {
2377 else if (S_ISSOCK(mode)) {
2409 fname = rb_str_encode_ospath(fname);
2411 rb_sys_fail_path(fname);
2414 return rb_file_ftype(st.st_mode);
2434 if (
rb_stat(fname, &st) < 0) {
2437 rb_syserr_fail_path(e, fname);
2439 return stat_time(stat_atimespec(&st));
2454rb_file_atime(
VALUE obj)
2460 if (fstat(fptr->
fd, &st) == -1) {
2461 rb_sys_fail_path(fptr->
pathv);
2463 return stat_time(stat_atimespec(&st));
2483 if (
rb_stat(fname, &st) < 0) {
2486 rb_syserr_fail_path(e, fname);
2488 return stat_time(stat_mtimespec(&st));
2502rb_file_mtime(
VALUE obj)
2508 if (fstat(fptr->
fd, &st) == -1) {
2509 rb_sys_fail_path(fptr->
pathv);
2511 return stat_time(stat_mtimespec(&st));
2535 if (
rb_stat(fname, &st) < 0) {
2538 rb_syserr_fail_path(e, fname);
2540 return stat_time(stat_ctimespec(&st));
2557rb_file_ctime(
VALUE obj)
2563 if (fstat(fptr->
fd, &st) == -1) {
2564 rb_sys_fail_path(fptr->
pathv);
2566 return stat_time(stat_ctimespec(&st));
2569#if defined(HAVE_STAT_BIRTHTIME)
2589 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2592 rb_syserr_fail_path(e, fname);
2594 return statx_birthtime(&st);
2597# define rb_file_s_birthtime rb_f_notimplement
2600#if defined(HAVE_STAT_BIRTHTIME)
2614rb_file_birthtime(
VALUE obj)
2620 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2621 rb_sys_fail_path(fptr->
pathv);
2623 return statx_birthtime(&st);
2626# define rb_file_birthtime rb_f_notimplement
2638 rb_io_flush_raw(file, 0);
2641 if (fstat(fptr->
fd, &st) == -1) {
2642 rb_sys_fail_path(fptr->
pathv);
2663file_size(
VALUE self)
2665 return OFFT2NUM(rb_file_size(self));
2674nogvl_chmod(
void *ptr)
2677 int ret = chmod(data->path, data->mode);
2678 return (
void *)(
VALUE)ret;
2682rb_chmod(
const char *path, mode_t mode)
2688 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2692chmod_internal(
const char *path,
void *mode)
2694 return chmod(path, *(mode_t *)mode);
2718 return apply2files(chmod_internal, argc, argv, &mode);
2722struct nogvl_fchmod_data {
2728io_blocking_fchmod(
void *ptr)
2730 struct nogvl_fchmod_data *data = ptr;
2731 int ret = fchmod(data->fd, data->mode);
2736rb_fchmod(
struct rb_io* io, mode_t mode)
2739 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2740 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2762#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2770 if (rb_fchmod(fptr, mode) == -1) {
2771 if (HAVE_FCHMOD ||
errno != ENOSYS)
2772 rb_sys_fail_path(fptr->
pathv);
2775 if (!HAVE_FCHMOD)
return INT2FIX(0);
2778#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2780 path = rb_str_encode_ospath(fptr->
pathv);
2781 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2782 rb_sys_fail_path(fptr->
pathv);
2788#if defined(HAVE_LCHMOD)
2790lchmod_internal(
const char *path,
void *mode)
2792 return lchmod(path, *(mode_t *)mode);
2813 return apply2files(lchmod_internal, argc, argv, &mode);
2816#define rb_file_s_lchmod rb_f_notimplement
2819static inline rb_uid_t
2823 return (rb_uid_t)-1;
2828static inline rb_gid_t
2832 return (rb_gid_t)-1;
2843chown_internal(
const char *path,
void *arg)
2846 return chown(path, args->owner, args->group);
2870 arg.owner = to_uid(*argv++);
2871 arg.group = to_gid(*argv++);
2873 return apply2files(chown_internal, argc, argv, &arg);
2885nogvl_chown(
void *ptr)
2888 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
2892rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
2895 .as = {.path = path},
2896 .new = {.owner = owner, .group = group},
2898 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
2903nogvl_fchown(
void *ptr)
2906 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
2910rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
2915 .new = {.owner = owner, .group = group},
2917 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
2951 path = rb_str_encode_ospath(fptr->
pathv);
2952 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
2953 rb_sys_fail_path(fptr->
pathv);
2955 if (rb_fchown(fptr->
fd, o, g) == -1)
2956 rb_sys_fail_path(fptr->
pathv);
2962#if defined(HAVE_LCHOWN)
2964lchown_internal(
const char *path,
void *arg)
2967 return lchown(path, args->owner, args->group);
2987 arg.owner = to_uid(*argv++);
2988 arg.group = to_gid(*argv++);
2990 return apply2files(lchown_internal, argc, argv, &arg);
2993#define rb_file_s_lchown rb_f_notimplement
3003NORETURN(
static void utime_failed(
struct apply_arg *));
3009 VALUE path = aa->fn[aa->i].path;
3012 if (ua->tsp && e == EINVAL) {
3015 VALUE atime = ua->atime;
3016 VALUE mtime = ua->mtime;
3018 if (!
NIL_P(atime)) {
3021 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3024 if (
NIL_P(a)) e[0] = m;
3040 rb_syserr_fail_path(e, path);
3044#if defined(HAVE_UTIMES)
3046# if !defined(HAVE_UTIMENSAT)
3048# elif defined(__APPLE__) && \
3049 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3051# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3052typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3056static
inline utimensat_func *
3063# define utimensat rb_utimensat()
3070utime_internal(
const char *path,
void *arg)
3073 const struct timespec *tsp = v->tsp;
3074 struct timeval tvbuf[2], *tvp = NULL;
3076#if defined(HAVE_UTIMENSAT)
3077# if defined(__APPLE__)
3078 const int try_utimensat = utimensat != NULL;
3079 const int try_utimensat_follow = utimensat != NULL;
3081# define TRY_UTIMENSAT 1
3082 static int try_utimensat = 1;
3083# ifdef AT_SYMLINK_NOFOLLOW
3084 static int try_utimensat_follow = 1;
3086 const int try_utimensat_follow = 0;
3091 if (v->follow ? try_utimensat_follow : try_utimensat) {
3092# ifdef AT_SYMLINK_NOFOLLOW
3094 flags = AT_SYMLINK_NOFOLLOW;
3098 int result = utimensat(AT_FDCWD, path, tsp, flags);
3099# ifdef TRY_UTIMENSAT
3100 if (result < 0 &&
errno == ENOSYS) {
3101# ifdef AT_SYMLINK_NOFOLLOW
3102 try_utimensat_follow = 0;
3114 tvbuf[0].tv_sec = tsp[0].tv_sec;
3115 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3116 tvbuf[1].tv_sec = tsp[1].tv_sec;
3117 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3121 if (v->follow)
return lutimes(path, tvp);
3123 return utimes(path, tvp);
3128#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3136utime_internal(
const char *path,
void *arg)
3140 struct utimbuf utbuf, *utp = NULL;
3142 utbuf.actime = tsp[0].tv_sec;
3143 utbuf.modtime = tsp[1].tv_sec;
3146 return utime(path, utp);
3151utime_internal_i(
int argc,
VALUE *argv,
int follow)
3154 struct timespec tss[2], *tsp = NULL;
3157 args.atime = *argv++;
3158 args.mtime = *argv++;
3160 args.follow = follow;
3162 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3165 if (args.atime == args.mtime)
3172 return apply2files(utime_internal, argc, argv, &args);
3189 return utime_internal_i(argc, argv, FALSE);
3192#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3208 return utime_internal_i(argc, argv, TRUE);
3211#define rb_file_s_lutime rb_f_notimplement
3214#ifdef RUBY_FUNCTION_NAME_STRING
3215# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3217# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3219#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3220NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3222syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3226 const int max_pathlen = MAX_PATH;
3228 const int max_pathlen = MAXPATHLEN;
3239#ifdef RUBY_FUNCTION_NAME_STRING
3240 rb_syserr_fail_path_in(func, e, str);
3242 rb_syserr_fail_path(e, str);
3264 from = rb_str_encode_ospath(from);
3265 to = rb_str_encode_ospath(to);
3268 sys_fail2(from, to);
3273#define rb_file_s_link rb_f_notimplement
3294 from = rb_str_encode_ospath(from);
3295 to = rb_str_encode_ospath(to);
3298 sys_fail2(from, to);
3303#define rb_file_s_symlink rb_f_notimplement
3321 return rb_readlink(path, rb_filesystem_encoding());
3324struct readlink_arg {
3331nogvl_readlink(
void *ptr)
3333 struct readlink_arg *ra = ptr;
3335 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3339readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3341 struct readlink_arg ra;
3343 ra.path = RSTRING_PTR(path);
3344 ra.buf = RSTRING_PTR(buf);
3347 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3358 path = rb_str_encode_ospath(path);
3359 v = rb_enc_str_new(0, size, enc);
3360 while ((rv = readlink_without_gvl(path, v, size)) == size
3362 || (rv < 0 &&
errno == ERANGE)
3371 rb_str_resize(v, 0);
3372 rb_syserr_fail_path(e, path);
3374 rb_str_resize(v, rv);
3379#define rb_file_s_readlink rb_f_notimplement
3383unlink_internal(
const char *path,
void *arg)
3385 return unlink(path);
3405rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3407 return apply2files(unlink_internal, argc, argv, 0);
3416no_gvl_rename(
void *ptr)
3420 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3441 f = rb_str_encode_ospath(from);
3442 t = rb_str_encode_ospath(to);
3445#if defined __CYGWIN__
3448 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3453 if (chmod(ra.dst, 0666) == 0 &&
3454 unlink(ra.dst) == 0 &&
3455 rename(ra.src, ra.dst) == 0)
3459 syserr_fail2(e, from, to);
3494 rb_error_arity(argc, 0, 1);
3502#if defined __CYGWIN__ || defined DOSISH
3504#define DOSISH_DRIVE_LETTER
3505#define FILE_ALT_SEPARATOR '\\'
3507#ifdef FILE_ALT_SEPARATOR
3508#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3510static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3513#define isdirsep(x) ((x) == '/')
3526# define USE_NTFS_ADS 1
3528# define USE_NTFS_ADS 0
3533#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3535#define istrailinggarbage(x) 0
3539# define isADS(x) ((x) == ':')
3544#define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc)))
3545#define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
3547#if defined(DOSISH_UNC)
3548#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3550#define has_unc(buf) 0
3553#ifdef DOSISH_DRIVE_LETTER
3555has_drive_letter(
const char *buf)
3557 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3570 char *drvcwd, *oldcwd;
3581 if (chdir(drive) == 0) {
3594not_same_drive(
VALUE path,
int drive)
3596 const char *p = RSTRING_PTR(path);
3597 if (RSTRING_LEN(path) < 2)
return 0;
3598 if (has_drive_letter(p)) {
3609skiproot(
const char *path,
const char *end,
rb_encoding *enc)
3611#ifdef DOSISH_DRIVE_LETTER
3612 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3614 while (path < end && isdirsep(*path)) path++;
3615 return (
char *)path;
3618#define nextdirsep rb_enc_path_next
3622 while (s < e && !isdirsep(*s)) {
3628#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3629#define skipprefix rb_enc_path_skip_prefix
3631#define skipprefix(path, end, enc) (path)
3634rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3636#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3638 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3640 while (path < end && isdirsep(*path)) path++;
3641 if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1]))
3642 path = rb_enc_path_next(path + 1, end, enc);
3643 return (
char *)path;
3646#ifdef DOSISH_DRIVE_LETTER
3647 if (has_drive_letter(path))
3648 return (
char *)(path + 2);
3651 return (
char *)path;
3655skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3657#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3658 char *p = skipprefix(path, end, enc);
3659 while (isdirsep(*p)) p++;
3662 return skiproot(path, end, enc);
3666#define strrdirsep rb_enc_path_last_separator
3668rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3671 while (path < end) {
3672 if (isdirsep(*path)) {
3673 const char *tmp = path++;
3674 while (path < end && isdirsep(*path)) path++;
3675 if (path >= end)
break;
3679 Inc(path, end, enc);
3686chompdirsep(
const char *path,
const char *end,
rb_encoding *enc)
3688 while (path < end) {
3689 if (isdirsep(*path)) {
3690 const char *last = path++;
3691 while (path < end && isdirsep(*path)) path++;
3692 if (path >= end)
return (
char *)last;
3695 Inc(path, end, enc);
3698 return (
char *)path;
3704 if (path < end && isdirsep(*path)) path++;
3705 return chompdirsep(path, end, enc);
3712 int encidx = rb_enc_to_index(enc);
3713 if (encidx == ENCINDEX_US_ASCII) {
3714 encidx = rb_enc_get_index(path1);
3715 if (encidx == ENCINDEX_US_ASCII)
3716 encidx = rb_enc_get_index(path2);
3717 enc = rb_enc_from_index(encidx);
3724ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
3726 while (path < end && *path ==
'.') path++;
3727 while (path < end && !isADS(*path)) {
3728 if (istrailinggarbage(*path)) {
3729 const char *last = path++;
3730 while (path < end && istrailinggarbage(*path)) path++;
3731 if (path >= end || isADS(*path))
return (
char *)last;
3733 else if (isdirsep(*path)) {
3734 const char *last = path++;
3735 while (path < end && isdirsep(*path)) path++;
3736 if (path >= end)
return (
char *)last;
3737 if (isADS(*path)) path++;
3740 Inc(path, end, enc);
3743 return (
char *)path;
3747#define BUFCHECK(cond) do {\
3750 do {buflen *= 2;} while (cond);\
3751 rb_str_resize(result, buflen);\
3752 buf = RSTRING_PTR(result);\
3754 pend = buf + buflen;\
3759 p = buf = RSTRING_PTR(result),\
3760 buflen = RSTRING_LEN(result),\
3764# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3766# define SKIPPATHSEP(p) 1
3769#define BUFCOPY(srcptr, srclen) do { \
3770 const int skip = SKIPPATHSEP(p); \
3771 rb_str_set_len(result, p-buf+skip); \
3772 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3774 memcpy(p, (srcptr), (srclen)); \
3778#define WITH_ROOTDIFF(stmt) do { \
3779 long rootdiff = root - buf; \
3781 root = buf + rootdiff; \
3785copy_home_path(
VALUE result,
const char *dir)
3788#if defined DOSISH || defined __CYGWIN__
3795 dirlen = strlen(dir);
3796 rb_str_resize(result, dirlen);
3797 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3798 encidx = rb_filesystem_encindex();
3799 rb_enc_associate_index(result, encidx);
3800#if defined DOSISH || defined __CYGWIN__
3801 enc = rb_enc_from_index(encidx);
3802 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) {
3815 VALUE dirname = rb_getpwdirnam_for_login(user);
3816 if (dirname ==
Qnil) {
3817 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3819 const char *dir = RSTRING_PTR(dirname);
3821 extern char *getlogin(
void);
3822 const char *pwPtr = 0;
3824 # define endpwent() ((void)0)
3825 const char *dir, *username = RSTRING_PTR(user);
3836 if ((login = getlogin()) && strcasecmp(username, login) == 0)
3837 dir = pwPtr = getenv(
"HOME");
3839 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3842 copy_home_path(result, dir);
3848rb_default_home_dir(
VALUE result)
3850 const char *dir = getenv(
"HOME");
3852#if defined HAVE_PWD_H
3866 VALUE login_name = rb_getlogin();
3868# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
3873 if (
NIL_P(login_name)) {
3874 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
3878 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
3879 if (
NIL_P(pw_dir)) {
3880 pw_dir = rb_getpwdiruid();
3881 if (
NIL_P(pw_dir)) {
3882 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
3887 copy_home_path(result, RSTRING_PTR(pw_dir));
3888 rb_str_resize(pw_dir, 0);
3893 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
3895 return copy_home_path(result, dir);
3901#if NORMALIZE_UTF8PATH
3902 VALUE path = rb_str_normalize_ospath(ptr,
len);
3903 rb_enc_associate(path, fsenc);
3906 return rb_enc_str_new(ptr,
len, fsenc);
3913 char *buf, *cwdp = dir;
3917 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
3918 dirname = ospath_new(dir, dirlen, fsenc);
3919 if (!rb_enc_compatible(fname, dirname)) {
3923 rb_enc_check(fname, dirname);
3924 rb_bug(
"unreachable");
3926 rb_encoding *direnc = fs_enc_check(fname, dirname);
3927 if (direnc != fsenc) {
3931 else if (NORMALIZE_UTF8PATH) {
3936 do {buflen *= 2;}
while (dirlen > buflen);
3937 rb_str_resize(result, buflen);
3938 buf = RSTRING_PTR(result);
3939 memcpy(buf, cwdp, dirlen);
3941 if (!
NIL_P(dirname)) rb_str_resize(dirname, 0);
3942 rb_enc_associate(result, *enc);
3943 return buf + dirlen;
3947rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
3949 const char *s, *b, *fend;
3950 char *buf, *p, *pend, *root;
3951 size_t buflen, bdiff;
3952 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
3955 fend = s + RSTRING_LEN(fname);
3956 enc = rb_enc_get(fname);
3959 if (s[0] ==
'~' && abs_mode == 0) {
3961 if (isdirsep(s[1]) || s[1] ==
'\0') {
3966 rb_default_home_dir(result);
3969 s = nextdirsep(b = s, fend, enc);
3972 BUFCHECK(bdiff + userlen >= buflen);
3973 memcpy(p, b, userlen);
3976 rb_enc_associate(result, enc);
3977 rb_home_dir_of(result, result);
3981 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
3983 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
3984 (
int)userlen, b, fname);
3987 rb_raise(rb_eArgError,
"non-absolute home");
3993#ifdef DOSISH_DRIVE_LETTER
3995 else if (has_drive_letter(s)) {
3996 if (isdirsep(s[2])) {
3999 BUFCHECK(bdiff + 2 >= buflen);
4003 rb_enc_copy(result, fname);
4008 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4009 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4017 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4022 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4025 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4030 else if (!rb_is_absolute_path(s)) {
4031 if (!
NIL_P(dname)) {
4032 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4033 rb_enc_associate(result, fs_enc_check(result, fname));
4038 char *e = append_fspath(result, fname,
ruby_getcwd(), &enc, fsenc);
4042#if defined DOSISH || defined __CYGWIN__
4046 p = skipprefix(buf, p, enc);
4050 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4055 do s++;
while (isdirsep(*s));
4058 BUFCHECK(bdiff >= buflen);
4059 memset(buf,
'/',
len);
4061 rb_enc_associate(result, fs_enc_check(result, fname));
4063 if (p > buf && p[-1] ==
'/')
4067 BUFCHECK(bdiff + 1 >= buflen);
4072 BUFCHECK(bdiff + 1 >= buflen);
4074 root = skipprefix(buf, p+1, enc);
4086 if (*(s+1) ==
'\0' || isdirsep(*(s+1))) {
4090 if (!(n = strrdirsep(root, p, enc))) {
4100 do ++s;
while (istrailinggarbage(*s));
4105#if defined DOSISH || defined __CYGWIN__
4120 while (s < fend && istrailinggarbage(*s)) s++;
4130#if defined DOSISH || defined __CYGWIN__
4134 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4142 int n = ignored_char_p(s, fend, enc);
4145 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4161 static const char prime[] =
":$DATA";
4162 enum {prime_len =
sizeof(prime) -1};
4166 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4169 if (isADS(*(s - (prime_len+1)))) {
4172 else if (memchr(b,
':', s - prime_len - b)) {
4181 if (p == skiproot(buf, p + !!*p, enc) - 1) p++;
4185 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4190 WIN32_FIND_DATAW wfd;
4193#ifdef HAVE_CYGWIN_CONV_PATH
4194 char *w32buf = NULL;
4195 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4197 char w32buf[MAXPATHLEN];
4201 int lnk_added = 0, is_symlink = 0;
4205 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4211 path = *buf ? buf :
"/";
4212#ifdef HAVE_CYGWIN_CONV_PATH
4213 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4216 if (lnk_added) bufsize += 4;
4218 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4223 bufsize = MAXPATHLEN;
4224 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4228 if (is_symlink && b == w32buf) {
4230 strlcat(w32buf, p, bufsize);
4232 strlcat(w32buf,
".lnk", bufsize);
4243 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4244 tmp = rb_str_encode_ospath(result);
4246 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4248 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4250 h = FindFirstFileW(wstr, &wfd);
4252 if (h != INVALID_HANDLE_VALUE) {
4255 len = lstrlenW(wfd.cFileName);
4257 if (lnk_added &&
len > 4 &&
4258 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4259 wfd.cFileName[
len -= 4] = L
'\0';
4266 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4267 if (tmp == result) {
4268 BUFCHECK(bdiff +
len >= buflen);
4269 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4273 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4274 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4275 rb_utf8_encoding(), 0,
Qnil);
4277 rb_str_resize(tmp, 0);
4290 rb_enc_check(fname, result);
4296#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
4299str_shrink(
VALUE str)
4301 rb_str_resize(str, RSTRING_LEN(str));
4305#define expand_path(fname, dname, abs_mode, long_name, result) \
4306 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4308#define check_expand_path_args(fname, dname) \
4309 (((fname) = rb_get_path(fname)), \
4310 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4313file_expand_path_1(
VALUE fname)
4315 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, EXPAND_PATH_BUFFER());
4321 check_expand_path_args(fname, dname);
4322 return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4326rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4328 return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4332rb_file_s_expand_path(
int argc,
const VALUE *argv)
4335 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4369 return rb_file_s_expand_path(c, v);
4375 check_expand_path_args(fname, dname);
4376 return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4380rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4383 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4402 return rb_file_s_absolute_path(c, v);
4418 VALUE path = rb_get_path(fname);
4420 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4424enum rb_realpath_mode {
4428 RB_REALPATH_MODE_MAX
4432realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4433 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4435 const char *pend = unresolved + strlen(unresolved);
4439 while (unresolved < pend) {
4440 const char *testname = unresolved;
4441 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4442 long testnamelen = unresolved_firstsep - unresolved;
4443 const char *unresolved_nextname = unresolved_firstsep;
4444 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4445 unresolved_nextname++;
4446 unresolved = unresolved_nextname;
4447 if (testnamelen == 1 && testname[0] ==
'.') {
4449 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4450 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4451 const char *resolved_str = RSTRING_PTR(*resolvedp);
4452 const char *resolved_names = resolved_str + *prefixlenp;
4453 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc);
4454 long len = lastsep ? lastsep - resolved_names : 0;
4455 rb_str_resize(*resolvedp, *prefixlenp +
len);
4461 if (*prefixlenp < RSTRING_LEN(testpath))
4463#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4464 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4465 const char *prefix = RSTRING_PTR(testpath);
4466 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4471 checkval = rb_hash_aref(loopcheck, testpath);
4472 if (!
NIL_P(checkval)) {
4473 if (checkval ==
ID2SYM(resolving)) {
4474 if (mode == RB_REALPATH_CHECK) {
4478 rb_syserr_fail_path(ELOOP, testpath);
4487 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4490 if (e == ENOENT && !
NIL_P(fallback)) {
4491 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4496 if (mode == RB_REALPATH_CHECK)
return -1;
4498 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4499 rb_syserr_fail_path(e, testpath);
4500 *resolvedp = testpath;
4504 rb_syserr_fail_path(e, testpath);
4508 if (S_ISLNK(sbuf.st_mode)) {
4511 const char *link_prefix, *link_names;
4512 long link_prefixlen;
4513 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4514 link = rb_readlink(testpath, enc);
4515 link_prefix = RSTRING_PTR(link);
4516 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4517 link_prefixlen = link_names - link_prefix;
4518 if (link_prefixlen > 0) {
4522 tmpenc = fs_enc_check(*resolvedp, link);
4525 *prefixlenp = link_prefixlen;
4527 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4528 loopcheck, mode, !*unresolved_firstsep))
4537 rb_hash_aset(loopcheck, s, s);
4538 *resolvedp = testpath;
4547rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4551 VALUE unresolved_path;
4556 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4557 char *ptr, *prefixptr = NULL, *pend;
4562 if (!
NIL_P(basedir)) {
4567 enc = rb_enc_get(unresolved_path);
4568 unresolved_path = TO_OSPATH(unresolved_path);
4570 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4571 if (ptr != path_names) {
4572 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4576 if (!
NIL_P(basedir)) {
4578 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4579 if (ptr != basedir_names) {
4585 curdir = rb_dir_getwd_ospath();
4587 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4592 pend = prefixptr + prefixlen;
4593 ptr = chompdirsep(prefixptr, pend, enc);
4595 prefixlen = ++ptr - prefixptr;
4598#ifdef FILE_ALT_SEPARATOR
4599 while (prefixptr < ptr) {
4600 if (*prefixptr == FILE_ALT_SEPARATOR) {
4603 Inc(prefixptr, pend, enc);
4607 switch (rb_enc_to_index(enc)) {
4608 case ENCINDEX_ASCII_8BIT:
4609 case ENCINDEX_US_ASCII:
4610 rb_enc_associate_index(resolved, rb_filesystem_encindex());
4613 loopcheck = rb_hash_new();
4615 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
4618 if (basedir_names) {
4619 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
4622 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
4625 if (origenc && origenc != rb_enc_get(resolved)) {
4627 rb_enc_associate(resolved, origenc);
4641#ifndef HAVE_REALPATH
4643rb_check_realpath_emulate_try(
VALUE arg)
4646 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
4650rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
4654#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
4655 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
4657# include <sys/syslimits.h>
4658# define NEEDS_REALPATH_BUFFER 1
4662rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4665 VALUE unresolved_path;
4666 char *resolved_ptr = NULL;
4668# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
4669 char resolved_buffer[PATH_MAX];
4671 char *
const resolved_buffer = NULL;
4674 if (mode == RB_REALPATH_DIR) {
4675 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4679 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
4680 unresolved_path = rb_file_join(
rb_assoc_new(basedir, unresolved_path));
4682 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4684 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
4693 if (
errno == ENOTSUP ||
4695 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4696 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4699 if (mode == RB_REALPATH_CHECK) {
4702 rb_sys_fail_path(unresolved_path);
4704 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4705# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
4709# if !defined(__linux__) && !defined(__APPLE__)
4713 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4714 if (mode == RB_REALPATH_CHECK) {
4717 rb_sys_fail_path(unresolved_path);
4721 if (origenc && origenc != rb_enc_get(resolved)) {
4725 rb_enc_associate(resolved, origenc);
4728 if (is_broken_string(resolved)) {
4729 rb_enc_associate(resolved, rb_filesystem_encoding());
4730 if (is_broken_string(resolved)) {
4731 rb_enc_associate(resolved, rb_ascii8bit_encoding());
4738 if (mode == RB_REALPATH_CHECK) {
4742 arg[2] = (
VALUE)origenc;
4745 rb_check_realpath_emulate_rescue,
Qnil);
4748 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4754rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
4756 const enum rb_realpath_mode mode =
4757 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4758 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4764 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4781rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
4784 VALUE path = argv[0];
4786 return rb_realpath_internal(basedir, path, 1);
4802rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
4805 VALUE path = argv[0];
4807 return rb_realpath_internal(basedir, path, 0);
4811rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
4815 const char *s, *last;
4817 if (!e || !l2)
return 0;
4819 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4820 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
4821 if (c ==
'.')
return l0;
4826 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4831 if (l1 < l2)
return l1;
4834 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
4835#if CASEFOLD_FILESYSTEM
4836#define fncomp strncasecmp
4838#define fncomp strncmp
4840 if (fncomp(s, e, l2) == 0) {
4847ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
4849 const char *p, *q, *e, *end;
4850#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4855 end = name + (alllen ? (size_t)*alllen : strlen(name));
4856 name = skipprefix(name, end, enc);
4857#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4860 while (isdirsep(*name))
4865#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4869#ifdef DOSISH_DRIVE_LETTER
4870 else if (*p ==
':') {
4883 if (!(p = strrdirsep(name, end, enc))) {
4887 while (isdirsep(*p)) p++;
4890 n = ntfs_tail(p, end, enc) - p;
4892 n = chompdirsep(p, end, enc) - p;
4894 for (q = p; q - p < n && *q ==
'.'; q++);
4895 for (e = 0; q - p < n; Inc(q, end, enc)) {
4896 if (*q ==
'.') e = q;
4929 VALUE fname, fext, basename;
4930 const char *name, *p;
4938 enc = check_path_encoding(fext);
4942 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
4943 enc = rb_enc_get(fname);
4946 if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
4949 p = ruby_enc_find_basename(name, &f, &n, enc);
4957 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
4966 rb_enc_copy(basename, fname);
4970static VALUE rb_file_dirname_n(
VALUE fname,
int n);
4992rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
4998 return rb_file_dirname_n(argv[0], n);
5004 return rb_file_dirname_n(fname, 1);
5008rb_file_dirname_n(
VALUE fname,
int n)
5010 const char *name, *root, *p, *end;
5016 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5019 end = name + RSTRING_LEN(fname);
5020 enc = rb_enc_get(fname);
5021 root = skiproot(name, end, enc);
5023 if (root > name + 1 && isdirsep(*name))
5024 root = skipprefix(name = root - 2, end, enc);
5026 if (root > name + 1)
5029 if (n > (end - root + 1) / 2) {
5039 if (!(p = strrdirsep(root, end, enc))) p = root;
5042 seps =
ALLOCV_N(
const char *, sepsv, n);
5043 for (i = 0; i < n; ++i) seps[i] = root;
5045 for (p = root; p < end; ) {
5047 const char *tmp = p++;
5048 while (p < end && isdirsep(*p)) p++;
5049 if (p >= end)
break;
5064#ifdef DOSISH_DRIVE_LETTER
5065 if (has_drive_letter(name) && isdirsep(*(name + 2))) {
5066 const char *top = skiproot(name + 2, end, enc);
5073#ifdef DOSISH_DRIVE_LETTER
5074 if (has_drive_letter(name) && root == name + 2 && p - name == 2)
5077 rb_enc_copy(dirname, fname);
5096 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5098 p = strrdirsep(name, end, enc);
5102 do name = ++p;
while (isdirsep(*p));
5105 while (*p && *p ==
'.') p++;
5107 if (*p ==
'.' || istrailinggarbage(*p)) {
5109 const char *last = p++, *dot = last;
5110 while (istrailinggarbage(*p)) {
5111 if (*p ==
'.') dot = p;
5114 if (!*p || isADS(*p)) {
5118 if (*last ==
'.' || dot > last) e = dot;
5125 else if (isADS(*p)) {
5129 else if (isdirsep(*p))
5136 if (!e || e == name)
5175 const char *name, *e;
5181 len = RSTRING_LEN(fname);
5182 e = ruby_enc_find_extname(name, &
len, rb_enc_get(fname));
5203 return rb_get_path(fname);
5225file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5227 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5228 return rb_file_join(arg);
5232rb_file_join(
VALUE ary)
5236 const char *name, *tail;
5246 check_path_encoding(tmp);
5247 len += RSTRING_LEN(tmp);
5255 RBASIC_CLEAR_CLASS(result);
5258 switch (OBJ_BUILTIN_TYPE(tmp)) {
5260 if (!checked) check_path_encoding(tmp);
5265 rb_raise(rb_eArgError,
"recursive array");
5277 rb_enc_copy(result, tmp);
5280 tail = chompdirsep(name, name +
len, rb_enc_get(result));
5281 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5288 enc = fs_enc_check(result, tmp);
5290 rb_enc_associate(result, enc);
5311 return rb_file_join(args);
5314#if defined(HAVE_TRUNCATE)
5315struct truncate_arg {
5321nogvl_truncate(
void *ptr)
5323 struct truncate_arg *ta = ptr;
5324 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5345 struct truncate_arg ta;
5350 path = rb_str_encode_ospath(path);
5353 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5355 rb_sys_fail_path(path);
5359#define rb_file_s_truncate rb_f_notimplement
5362#if defined(HAVE_FTRUNCATE)
5363struct ftruncate_arg {
5369nogvl_ftruncate(
void *ptr)
5371 struct ftruncate_arg *fa = ptr;
5373 return (
VALUE)ftruncate(fa->fd, fa->pos);
5394 struct ftruncate_arg fa;
5401 rb_io_flush_raw(obj, 0);
5403 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5404 rb_sys_fail_path(fptr->
pathv);
5409#define rb_file_truncate rb_f_notimplement
5426#include <winerror.h>
5430rb_thread_flock(
void *data)
5433 int old_errno =
errno;
5435 int *op = data, ret = flock(op[0], op[1]);
5438 if (GetLastError() == ERROR_NOT_LOCKED) {
5496 op[1] = op1 =
NUM2INT(operation);
5501 rb_io_flush_raw(obj, 0);
5503 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
5508#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5511 if (op1 & LOCK_NB)
return Qfalse;
5514 time.tv_usec = 100 * 1000;
5520#if defined(ERESTART)
5526 rb_syserr_fail_path(e, fptr->
pathv);
5533test_check(
int n,
int argc,
VALUE *argv)
5539 for (i=1; i<n; i++) {
5546#define CHECK(n) test_check((n), argc, argv)
5638 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
5642 return rb_file_blockdev_p(0, argv[1]);
5645 return rb_file_chardev_p(0, argv[1]);
5648 return rb_file_directory_p(0, argv[1]);
5651 return rb_file_exist_p(0, argv[1]);
5654 return rb_file_file_p(0, argv[1]);
5657 return rb_file_sgid_p(0, argv[1]);
5660 return rb_file_grpowned_p(0, argv[1]);
5663 return rb_file_sticky_p(0, argv[1]);
5666 return rb_file_symlink_p(0, argv[1]);
5669 return rb_file_owned_p(0, argv[1]);
5672 return rb_file_rowned_p(0, argv[1]);
5675 return rb_file_pipe_p(0, argv[1]);
5678 return rb_file_readable_p(0, argv[1]);
5681 return rb_file_readable_real_p(0, argv[1]);
5684 return rb_file_size_p(0, argv[1]);
5687 return rb_file_socket_p(0, argv[1]);
5690 return rb_file_suid_p(0, argv[1]);
5693 return rb_file_writable_p(0, argv[1]);
5696 return rb_file_writable_real_p(0, argv[1]);
5699 return rb_file_executable_p(0, argv[1]);
5702 return rb_file_executable_real_p(0, argv[1]);
5705 return rb_file_zero_p(0, argv[1]);
5709 if (strchr(
"MAC", cmd)) {
5711 VALUE fname = argv[1];
5714 if (
rb_stat(fname, &st) == -1) {
5717 rb_syserr_fail_path(e, fname);
5722 return stat_atime(&st);
5724 return stat_mtime(&st);
5726 return stat_ctime(&st);
5732 return rb_file_identical_p(0, argv[1], argv[2]);
5735 if (strchr(
"=<>", cmd)) {
5736 struct stat st1, st2;
5743 t1 = stat_mtimespec(&st1);
5744 t2 = stat_mtimespec(&st2);
5748 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
5752 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
5753 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
5757 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
5758 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
5765 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
5768 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
5787rb_stat_s_alloc(
VALUE klass)
5808 fname = rb_str_encode_ospath(fname);
5810 rb_sys_fail_path(fname);
5817 rb_st->initialized =
true;
5834 *copy_rb_st = *orig_rb_st;
5853rb_stat_ftype(
VALUE obj)
5855 return rb_file_ftype(get_stat(obj)->ST_(mode));
5872 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
5888 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
5914 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
5935 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
5958 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
5979 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
5997rb_stat_owned(
VALUE obj)
5999 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6004rb_stat_rowned(
VALUE obj)
6006 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6023rb_stat_grpowned(
VALUE obj)
6026 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6045 rb_io_stat_data *st = get_stat(obj);
6048 if (geteuid() == 0)
return Qtrue;
6051 if (rb_stat_owned(obj))
6052 return RBOOL(st->ST_(mode) & S_IRUSR);
6055 if (rb_stat_grpowned(obj))
6056 return RBOOL(st->ST_(mode) & S_IRGRP);
6059 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6078 rb_io_stat_data *st = get_stat(obj);
6081 if (getuid() == 0)
return Qtrue;
6084 if (rb_stat_rowned(obj))
6085 return RBOOL(st->ST_(mode) & S_IRUSR);
6088 if (rb_group_member(get_stat(obj)->ST_(gid)))
6089 return RBOOL(st->ST_(mode) & S_IRGRP);
6092 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6111rb_stat_wr(
VALUE obj)
6114 rb_io_stat_data *st = get_stat(obj);
6115 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6116 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6136 rb_io_stat_data *st = get_stat(obj);
6139 if (geteuid() == 0)
return Qtrue;
6142 if (rb_stat_owned(obj))
6143 return RBOOL(st->ST_(mode) & S_IWUSR);
6146 if (rb_stat_grpowned(obj))
6147 return RBOOL(st->ST_(mode) & S_IWGRP);
6150 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6169 rb_io_stat_data *st = get_stat(obj);
6172 if (getuid() == 0)
return Qtrue;
6175 if (rb_stat_rowned(obj))
6176 return RBOOL(st->ST_(mode) & S_IWUSR);
6179 if (rb_group_member(get_stat(obj)->ST_(gid)))
6180 return RBOOL(st->ST_(mode) & S_IWGRP);
6183 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6202rb_stat_ww(
VALUE obj)
6205 rb_io_stat_data *st = get_stat(obj);
6206 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6207 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6229 rb_io_stat_data *st = get_stat(obj);
6232 if (geteuid() == 0) {
6233 return RBOOL(st->ST_(mode) & S_IXUGO);
6237 if (rb_stat_owned(obj))
6238 return RBOOL(st->ST_(mode) & S_IXUSR);
6241 if (rb_stat_grpowned(obj))
6242 return RBOOL(st->ST_(mode) & S_IXGRP);
6245 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6261 rb_io_stat_data *st = get_stat(obj);
6264 if (getuid() == 0) {
6265 return RBOOL(st->ST_(mode) & S_IXUGO);
6269 if (rb_stat_rowned(obj))
6270 return RBOOL(st->ST_(mode) & S_IXUSR);
6273 if (rb_group_member(get_stat(obj)->ST_(gid)))
6274 return RBOOL(st->ST_(mode) & S_IXGRP);
6277 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6296 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6314 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6333 rb_off_t size = get_stat(obj)->ST_(size);
6335 if (size == 0)
return Qnil;
6351rb_stat_suid(
VALUE obj)
6354 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6372rb_stat_sgid(
VALUE obj)
6375 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6393rb_stat_sticky(
VALUE obj)
6396 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
6401#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6402#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6413nogvl_mkfifo(
void *ptr)
6415 struct mkfifo_arg *ma = ptr;
6417 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
6434 struct mkfifo_arg ma;
6443 path = rb_str_encode_ospath(path);
6444 ma.path = RSTRING_PTR(path);
6445 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
6446 rb_sys_fail_path(path);
6451#define rb_file_s_mkfifo rb_f_notimplement
6454static VALUE rb_mFConst;
6457rb_file_const(
const char *name,
VALUE value)
6459 rb_define_const(rb_mFConst, name, value);
6463rb_is_absolute_path(
const char *path)
6465#ifdef DOSISH_DRIVE_LETTER
6466 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
6469 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
6472 if (path[0] ==
'/')
return 1;
6477#ifndef ENABLE_PATH_CHECK
6478# if defined DOSISH || defined __CYGWIN__
6479# define ENABLE_PATH_CHECK 0
6481# define ENABLE_PATH_CHECK 1
6485#if ENABLE_PATH_CHECK
6487path_check_0(
VALUE path)
6495 if (!rb_is_absolute_path(p0)) {
6505 p0 = RSTRING_PTR(path);
6507 e0 = p0 + RSTRING_LEN(path);
6508 enc = rb_enc_get(path);
6513 if (STAT(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH)
6515 && !(p && (st.st_mode & S_ISVTX))
6517 && !access(p0, W_OK)) {
6518 rb_enc_warn(enc,
"Insecure world writable dir %s in PATH, mode 0%"
6519#
if SIZEOF_DEV_T > SIZEOF_INT
6529 s = strrdirsep(p0, e0, enc);
6531 if (!s || s == p0)
return 1;
6540rb_path_check(
const char *path)
6542 rb_warn_deprecated_to_remove_at(3.6,
"rb_path_check", NULL);
6543#if ENABLE_PATH_CHECK
6544 const char *p0, *p, *pend;
6547 if (!path)
return 1;
6549 pend = path + strlen(path);
6551 p = strchr(path, sep);
6559 if (p0 > pend)
break;
6560 p = strchr(p0, sep);
6568ruby_is_fd_loadable(
int fd)
6575 if (fstat(fd, &st) < 0)
6578 if (S_ISREG(st.st_mode))
6581 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6584 if (S_ISDIR(st.st_mode))
6595rb_file_load_ok(
const char *path)
6602 int mode = (O_RDONLY |
6603#if defined O_NONBLOCK
6605#elif defined O_NDELAY
6611 if (!rb_gc_for_fd(
errno))
return 0;
6613 if (fd < 0)
return 0;
6616 ret = ruby_is_fd_loadable(fd);
6623is_explicit_relative(
const char *path)
6625 if (*path++ !=
'.')
return 0;
6626 if (*path ==
'.') path++;
6627 return isdirsep(*path);
6633 int encidx = rb_enc_get_index(orig);
6634 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
6635 encidx = rb_filesystem_encindex();
6636 rb_enc_associate_index(path, encidx);
6644rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
6647 VALUE fname = *filep, load_path, tmp;
6651 if (!ext[0])
return 0;
6654 fname = file_expand_path_1(fname);
6655 f = RSTRING_PTR(fname);
6660 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6661 if (!expanded) fname = file_expand_path_1(fname);
6662 fnlen = RSTRING_LEN(fname);
6663 for (i=0; ext[i]; i++) {
6665 if (rb_file_load_ok(RSTRING_PTR(fname))) {
6666 *filep = copy_path_class(fname, *filep);
6674 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6675 if (!load_path)
return 0;
6678 RBASIC_CLEAR_CLASS(fname);
6679 fnlen = RSTRING_LEN(fname);
6681 rb_enc_associate_index(tmp, rb_usascii_encindex());
6682 for (j=0; ext[j]; j++) {
6684 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6688 if (RSTRING_LEN(str) == 0)
continue;
6689 rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6690 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6691 *filep = copy_path_class(tmp, *filep);
6697 rb_str_resize(tmp, 0);
6705 VALUE tmp, load_path;
6710 tmp = file_expand_path_1(path);
6711 path = copy_path_class(tmp, path);
6712 f = RSTRING_PTR(path);
6716 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6717 if (!rb_file_load_ok(f))
return 0;
6719 path = copy_path_class(file_expand_path_1(path), path);
6723 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6728 rb_enc_associate_index(tmp, rb_usascii_encindex());
6729 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6732 if (RSTRING_LEN(str) > 0) {
6733 rb_file_expand_path_internal(path, str, 0, 0, tmp);
6734 f = RSTRING_PTR(tmp);
6735 if (rb_file_load_ok(f))
goto found;
6738 rb_str_resize(tmp, 0);
6746 return copy_path_class(tmp, path);
6749#define define_filetest_function(name, func, argc) do { \
6750 rb_define_module_function(rb_mFileTest, name, func, argc); \
6751 rb_define_singleton_method(rb_cFile, name, func, argc); \
6754const char ruby_null_device[] =
6757#elif defined AMIGA || defined __amigaos__
7561#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
7562 rb_CFString_class_initialize_before_fork();
7570 define_filetest_function(
"directory?", rb_file_directory_p, 1);
7571 define_filetest_function(
"exist?", rb_file_exist_p, 1);
7572 define_filetest_function(
"readable?", rb_file_readable_p, 1);
7573 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
7574 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
7575 define_filetest_function(
"writable?", rb_file_writable_p, 1);
7576 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
7577 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
7578 define_filetest_function(
"executable?", rb_file_executable_p, 1);
7579 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
7580 define_filetest_function(
"file?", rb_file_file_p, 1);
7581 define_filetest_function(
"zero?", rb_file_zero_p, 1);
7582 define_filetest_function(
"empty?", rb_file_zero_p, 1);
7583 define_filetest_function(
"size?", rb_file_size_p, 1);
7584 define_filetest_function(
"size", rb_file_s_size, 1);
7585 define_filetest_function(
"owned?", rb_file_owned_p, 1);
7586 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
7588 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
7589 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
7590 define_filetest_function(
"socket?", rb_file_socket_p, 1);
7592 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
7593 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
7595 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
7596 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
7597 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
7599 define_filetest_function(
"identical?", rb_file_identical_p, 2);
7637 separator = rb_fstring_lit(
"/");
7639 rb_define_const(
rb_cFile,
"Separator", separator);
7641 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
7993 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
7995 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
7997 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
7999 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8001 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8003 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8004#if defined(O_NDELAY) || defined(O_NONBLOCK)
8006# define O_NONBLOCK O_NDELAY
8009 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8012 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8015 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8021 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8022#ifndef O_SHARE_DELETE
8023# define O_SHARE_DELETE 0
8026 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8029 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8033 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8037 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8041 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8045 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8049 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8053 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8057 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8059 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8061 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8063 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8066 rb_define_const(rb_mFConst,
"NULL", rb_fstring_cstr(ruby_null_device));
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
#define rb_define_global_function(mid, func, arity)
Defines rb_mKernel #mid.
#define PATH_SEP
The delimiter of PATH environment variable.
#define PATH_SEP_CHAR
Identical to PATH_SEP, except it is of type char.
#define GIDT2NUM
Converts a C's gid_t into an instance of rb_cInteger.
#define NUM2GIDT
Converts an instance of rb_cNumeric into C's gid_t.
void rb_include_module(VALUE klass, VALUE module)
Includes a module to a class.
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
VALUE rb_define_module(const char *name)
Defines a top-level module.
VALUE rb_define_module_under(VALUE outer, const char *name)
Defines a module under the namespace of outer.
#define rb_str_new2
Old name of rb_str_new_cstr.
#define T_FILE
Old name of RUBY_T_FILE.
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
#define NUM2ULONG
Old name of RB_NUM2ULONG.
#define ALLOCV
Old name of RB_ALLOCV.
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
#define T_STRING
Old name of RUBY_T_STRING.
#define xfree
Old name of ruby_xfree.
#define Qundef
Old name of RUBY_Qundef.
#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 rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
#define ULONG2NUM
Old name of RB_ULONG2NUM.
#define UNREACHABLE_RETURN
Old name of RBIMPL_UNREACHABLE_RETURN.
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
#define LONG2FIX
Old name of RB_INT2FIX.
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
#define STRCASECMP
Old name of st_locale_insensitive_strcasecmp.
#define rb_usascii_str_new2
Old name of rb_usascii_str_new_cstr.
#define ISALPHA
Old name of rb_isalpha.
#define ULL2NUM
Old name of RB_ULL2NUM.
#define TOLOWER
Old name of rb_tolower.
#define Qtrue
Old name of RUBY_Qtrue.
#define NUM2INT
Old name of RB_NUM2INT.
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define T_ARRAY
Old name of RUBY_T_ARRAY.
#define NIL_P
Old name of RB_NIL_P.
#define ALLOCV_N
Old name of RB_ALLOCV_N.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
#define ISPRINT
Old name of rb_isprint.
#define NUM2CHR
Old name of RB_NUM2CHR.
#define ENC_CODERANGE_CLEAR(obj)
Old name of RB_ENC_CODERANGE_CLEAR.
#define UINT2NUM
Old name of RB_UINT2NUM.
#define CONST_ID
Old name of RUBY_CONST_ID.
#define rb_str_new4
Old name of rb_str_new_frozen.
#define ALLOCV_END
Old name of RB_ALLOCV_END.
VALUE rb_eNotImpError
NotImplementedError exception.
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
VALUE rb_eIOError
IOError exception.
VALUE rb_eTypeError
TypeError exception.
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
void rb_enc_raise(rb_encoding *enc, VALUE exc, const char *fmt,...)
Identical to rb_raise(), except it additionally takes an encoding.
VALUE rb_eSystemCallError
SystemCallError exception.
VALUE rb_class_new_instance(int argc, const VALUE *argv, VALUE klass)
Allocates, then initialises an instance of the given class.
VALUE rb_cStat
File::Stat class.
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
VALUE rb_inspect(VALUE obj)
Generates a human-readable textual representation of the given object.
VALUE rb_mFileTest
FileTest module.
VALUE rb_equal(VALUE lhs, VALUE rhs)
This function is an optimised version of calling #==.
VALUE rb_obj_is_kind_of(VALUE obj, VALUE klass)
Queries if the given object is an instance (of possibly descendants) of the given class.
VALUE rb_obj_freeze(VALUE obj)
Just calls rb_obj_freeze_inline() inside.
VALUE rb_mComparable
Comparable module.
VALUE rb_cFile
File class.
VALUE rb_cString
String class.
static char * rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the left boundary of a character.
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
Encoding conversion main routine.
int rb_enc_str_asciionly_p(VALUE str)
Queries if the passed string is "ASCII only".
VALUE rb_funcall(VALUE recv, ID mid, int n,...)
Calls a method.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
#define INTEGER_PACK_NATIVE_BYTE_ORDER
Means either INTEGER_PACK_MSBYTE_FIRST or INTEGER_PACK_LSBYTE_FIRST, depending on the host processor'...
#define INTEGER_PACK_2COMP
Uses 2's complement representation.
#define INTEGER_PACK_LSWORD_FIRST
Stores/interprets the least significant word as the first word.
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
void rb_update_max_fd(int fd)
Informs the interpreter that the passed fd can be the max.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Opens a file that closes on exec.
VALUE rb_str_new_shared(VALUE str)
Identical to rb_str_new_cstr(), except it takes a Ruby's string instead of C's.
VALUE rb_str_plus(VALUE lhs, VALUE rhs)
Generates a new string, concatenating the former to the latter.
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
VALUE rb_str_tmp_new(long len)
Allocates a "temporary" string.
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
VALUE rb_str_ellipsize(VALUE str, long len)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
size_t rb_str_capacity(VALUE str)
Queries the capacity of the given string.
VALUE rb_str_dup(VALUE str)
Duplicates a string.
VALUE rb_str_cat(VALUE dst, const char *src, long srclen)
Destructively appends the passed contents to the string.
VALUE rb_str_replace(VALUE dst, VALUE src)
Replaces the contents of the former object with the stringised contents of the latter.
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
void rb_str_set_len(VALUE str, long len)
Overwrites the length of the string.
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
int rb_str_cmp(VALUE lhs, VALUE rhs)
Compares two strings, as in strcmp(3).
#define rb_str_dup_frozen
Just another name of rb_str_new_frozen.
void rb_str_modify_expand(VALUE str, long capa)
Identical to rb_str_modify(), except it additionally expands the capacity of the receiver.
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
#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_exec_recursive(VALUE(*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h)
"Recursion" API entry point.
void rb_thread_wait_for(struct timeval time)
Identical to rb_thread_sleep(), except it takes struct timeval instead.
VALUE rb_time_nano_new(time_t sec, long nsec)
Identical to rb_time_new(), except it accepts the time in nanoseconds resolution.
struct timespec rb_time_timespec(VALUE time)
Identical to rb_time_timeval(), except for return type.
void rb_define_alloc_func(VALUE klass, rb_alloc_func_t func)
Sets the allocator function of a class.
#define GetOpenFile
This is an old name of RB_IO_POINTER.
#define FMODE_WRITABLE
The IO is opened for writing.
#define RB_IO_POINTER(obj, fp)
Queries the underlying IO pointer.
void rb_io_check_closed(rb_io_t *fptr)
This badly named function asserts that the passed IO is open.
int len
Length of the buffer.
char * ruby_getcwd(void)
This is our own version of getcwd(3) that uses ruby_xmalloc() instead of system malloc (benefits our ...
#define strdup(s)
Just another name of ruby_strdup.
#define ALLOCA_N(type, n)
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
#define PRI_MODET_PREFIX
A rb_sprintf() format prefix to be used for a mode_t parameter.
#define NUM2MODET
Converts a C's mode_t into an instance of rb_cInteger.
#define MODET2NUM
Converts an instance of rb_cNumeric into C's mode_t.
VALUE rb_rescue(type *q, VALUE w, type *e, VALUE r)
An equivalent of rescue clause.
#define OFFT2NUM
Converts a C's off_t into an instance of rb_cInteger.
#define NUM2OFFT
Converts an instance of rb_cNumeric into C's off_t.
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
#define RARRAY_LEN
Just another name of rb_array_len.
#define RARRAY_AREF(a, i)
#define StringValue(v)
Ensures that the parameter object is a String.
#define StringValuePtr(v)
Identical to StringValue, except it returns a char*.
#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 RUBY_TYPED_DEFAULT_FREE
This is a value you can set to rb_data_type_struct::dfree.
#define TypedData_Get_Struct(obj, type, data_type, sval)
Obtains a C struct from inside of a wrapper Ruby object.
#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...
const char * rb_obj_classname(VALUE obj)
Queries the name of the class of the passed object.
#define FilePathValue(v)
Ensures that the parameter object is a path.
#define errno
Ractor-aware version of errno.
#define FilePathStringValue(v)
This macro actually does the same thing as FilePathValue now.
#define _(args)
This was a transition path from K&R to ANSI.
This is the struct that holds necessary info for a struct.
Ruby's IO, metadata and buffers.
enum rb_io_mode mode
mode flags: FMODE_XXXs
VALUE pathv
pathname for file
#define UIDT2NUM
Converts a C's uid_t into an instance of rb_cInteger.
#define NUM2UIDT
Converts an instance of rb_cNumeric into C's uid_t.
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
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.
#define RBIMPL_WARNING_IGNORED(flag)
Suppresses a warning.
#define RBIMPL_WARNING_PUSH()
Pushes compiler warning state.
#define RBIMPL_WARNING_POP()
Pops compiler warning state.