14#include "ruby/internal/config.h"
18# include "missing/file.h"
27# include <sys/cygwin.h>
32# if !(defined(__has_feature) && defined(__has_attribute))
37# define API_AVAILABLE(...)
38# define API_DEPRECATED(...)
40# include <CoreFoundation/CFString.h>
57#ifdef HAVE_SYS_PARAM_H
58# include <sys/param.h>
61# define MAXPATHLEN 1024
66#elif defined HAVE_SYS_UTIME_H
67# include <sys/utime.h>
74#ifdef HAVE_SYS_SYSMACROS_H
75# include <sys/sysmacros.h>
81#ifdef HAVE_SYS_MKDEV_H
82# include <sys/mkdev.h>
85#if defined(HAVE_FCNTL_H)
89#if defined(HAVE_SYS_TIME_H)
93#if !defined HAVE_LSTAT && !defined lstat
99# include "win32/file.h"
100# define STAT(p, s) rb_w32_ustati128((p), (s))
102# define lstat(p, s) rb_w32_ulstati128((p), (s))
104# define access(p, m) rb_w32_uaccess((p), (m))
106# define truncate(p, n) rb_w32_utruncate((p), (n))
108# define chmod(p, m) rb_w32_uchmod((p), (m))
110# define chown(p, o, g) rb_w32_uchown((p), (o), (g))
112# define lchown(p, o, g) rb_w32_ulchown((p), (o), (g))
114# define utimensat(s, p, t, f) rb_w32_uutimensat((s), (p), (t), (f))
116# define link(f, t) rb_w32_ulink((f), (t))
118# define unlink(p) rb_w32_uunlink(p)
120# define readlink(f, t, l) rb_w32_ureadlink((f), (t), (l))
122# define rename(f, t) rb_w32_urename((f), (t))
124# define symlink(s, l) rb_w32_usymlink((s), (l))
132# define STAT(p, s) stat((p), (s))
135#ifdef HAVE_STRUCT_STATX_STX_BTIME
136# define ST_(name) stx_ ## name
139# define ST_(name) st_ ## name
143#if defined _WIN32 || defined __APPLE__
145# define TO_OSPATH(str) rb_str_encode_ospath(str)
148# define TO_OSPATH(str) (str)
152#if defined DOSISH || defined __CYGWIN__
157#if defined HAVE_REALPATH && defined __sun && defined __SVR4
170#include "internal/compilers.h"
171#include "internal/dir.h"
172#include "internal/error.h"
173#include "internal/file.h"
174#include "internal/io.h"
175#include "internal/load.h"
176#include "internal/object.h"
177#include "internal/process.h"
178#include "internal/thread.h"
179#include "internal/vm.h"
184#define UIANY2NUM(x) \
185 ((sizeof(x) <= sizeof(unsigned int)) ? \
186 UINT2NUM((unsigned)(x)) : \
187 (sizeof(x) <= sizeof(unsigned long)) ? \
188 ULONG2NUM((unsigned long)(x)) : \
189 ULL2NUM((unsigned LONG_LONG)(x)))
196file_path_convert(
VALUE name)
201 if (ENCINDEX_US_ASCII != fname_encidx &&
202 ENCINDEX_ASCII_8BIT != fname_encidx &&
203 (fs_encidx = rb_filesystem_encindex()) != fname_encidx &&
204 rb_default_internal_encoding() &&
208 rb_encoding *fname_encoding = rb_enc_from_index(fname_encidx);
209 rb_encoding *fs_encoding = rb_enc_from_index(fs_encidx);
217check_path_encoding(
VALUE str)
220 if (!rb_enc_asciicompat(enc)) {
228rb_get_path_check_to_string(
VALUE obj)
237 tmp = rb_check_funcall_default(obj, to_path, 0, 0, obj);
243rb_get_path_check_convert(
VALUE obj)
245 obj = file_path_convert(obj);
247 check_path_encoding(obj);
248 if (!rb_str_to_cstr(obj)) {
249 rb_raise(rb_eArgError,
"path name contains null byte");
258 return rb_get_path(obj);
264 return rb_get_path_check_convert(rb_get_path_check_to_string(obj));
272#if 0 && defined _WIN32
273 if (encidx == ENCINDEX_ASCII_8BIT) {
274 encidx = rb_filesystem_encindex();
277 if (encidx != ENCINDEX_ASCII_8BIT && encidx != ENCINDEX_UTF_8) {
287# define NORMALIZE_UTF8PATH 1
289# ifdef HAVE_WORKING_FORK
290static CFMutableStringRef
291mutable_CFString_new(CFStringRef *s,
const char *ptr,
long len)
293 const CFAllocatorRef alloc = kCFAllocatorDefault;
294 *s = CFStringCreateWithBytesNoCopy(alloc, (
const UInt8 *)ptr,
len,
295 kCFStringEncodingUTF8, FALSE,
297 return CFStringCreateMutableCopy(alloc,
len, *s);
300# define mutable_CFString_release(m, s) (CFRelease(m), CFRelease(s))
303rb_CFString_class_initialize_before_fork(
void)
326 const char small_str[] =
"/";
327 long len =
sizeof(small_str) - 1;
335 for (
int i = 0; i < 2; i++) {
336 CFMutableStringRef m = mutable_CFString_new(&s, small_str,
len);
337 mutable_CFString_release(m, s);
343rb_str_append_normalized_ospath(
VALUE str,
const char *ptr,
long len)
348 CFMutableStringRef m = mutable_CFString_new(&s, ptr,
len);
349 long oldlen = RSTRING_LEN(str);
351 CFStringNormalize(m, kCFStringNormalizationFormC);
352 all = CFRangeMake(0, CFStringGetLength(m));
353 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE, NULL, 0, &buflen);
355 CFStringGetBytes(m, all, kCFStringEncodingUTF8,
'?', FALSE,
356 (UInt8 *)(RSTRING_PTR(str) + oldlen), buflen, &buflen);
358 mutable_CFString_release(m, s);
363rb_str_normalize_ospath(
const char *ptr,
long len)
366 const char *e = ptr +
len;
370 rb_enc_associate(str, enc);
374 int r = rb_enc_precise_mbclen(p, e, enc);
378 rb_str_append_normalized_ospath(str, p1, p-p1);
385 c = rb_enc_mbc_to_codepoint(p, e, enc);
386 if ((0x2000 <= c && c <= 0x2FFF) || (0xF900 <= c && c <= 0xFAFF) ||
387 (0x2F800 <= c && c <= 0x2FAFF)) {
389 rb_str_append_normalized_ospath(str, p1, p-p1);
400 rb_str_append_normalized_ospath(str, p1, p-p1);
407ignored_char_p(
const char *p,
const char *e,
rb_encoding *enc)
410 if (p+3 > e)
return 0;
411 switch ((
unsigned char)*p) {
413 switch ((
unsigned char)p[1]) {
415 c = (
unsigned char)p[2];
417 if (c >= 0x8c && c <= 0x8f)
return 3;
419 if (c >= 0xaa && c <= 0xae)
return 3;
422 c = (
unsigned char)p[2];
424 if (c >= 0xaa && c <= 0xaf)
return 3;
430 if ((
unsigned char)p[1] == 0xbb &&
431 (
unsigned char)p[2] == 0xbf)
438# define NORMALIZE_UTF8PATH 0
441#define apply2args(n) (rb_check_arity(argc, n, UNLIMITED_ARGUMENTS), argc-=n)
452 int (*func)(
const char *,
void *);
458no_gvl_apply2files(
void *ptr)
462 for (aa->i = 0; aa->i < aa->argc; aa->i++) {
463 if (aa->func(aa->fn[aa->i].ptr, aa->arg) < 0) {
472NORETURN(
static void utime_failed(
struct apply_arg *));
473static int utime_internal(
const char *,
void *);
477apply2files(
int (*func)(
const char *,
void *),
int argc,
VALUE *argv,
void *arg)
481 const long len = (long)(offsetof(
struct apply_arg, fn) + (size * argc));
489 for (aa->i = 0; aa->i < argc; aa->i++) {
490 VALUE path = rb_get_path(argv[aa->i]);
492 path = rb_str_encode_ospath(path);
493 aa->fn[aa->i].ptr = RSTRING_PTR(path);
494 aa->fn[aa->i].path = path;
497 IO_WITHOUT_GVL(no_gvl_apply2files, aa);
500 if (func == utime_internal) {
504 rb_syserr_fail_path(aa->errnum, aa->fn[aa->i].path);
523 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_EMBEDDABLE
527 rb_io_stat_data stat;
540rb_stat_new(
const struct stat *st)
546# define CP(m) .stx_ ## m = st->st_ ## m
547# define CP_32(m) .stx_ ## m = (uint32_t)st->st_ ## m
548# define CP_TS(m) .stx_ ## m = stat_ ## m ## spec(st)
549 rb_st->stat = (
struct statx){
550 .stx_mask = STATX_BASIC_STATS,
567 rb_st->initialized =
true;
575rb_statx_new(
const rb_io_stat_data *st)
581 rb_st->initialized =
true;
587static rb_io_stat_data*
592 if (!rb_st->initialized) rb_raise(
rb_eTypeError,
"uninitialized File::Stat");
598statx_mtimespec(
const rb_io_stat_data *st)
600 return st->stx_mtime;
603# define statx_mtimespec stat_mtimespec
627 if (ts1.tv_sec == ts2.tv_sec) {
628 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
629 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
632 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
638#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
641# define NUM2DEVT(v) NUM2UINT(v)
644# define DEVT2NUM(v) UINT2NUM(v)
646#ifndef PRI_DEVT_PREFIX
647# define PRI_DEVT_PREFIX ""
661rb_stat_dev(
VALUE self)
664 unsigned int m = get_stat(self)->stx_dev_major;
665 unsigned int n = get_stat(self)->stx_dev_minor;
667#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
668 return DEVT2NUM(get_stat(self)->st_dev);
669#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
670 return ULONG2NUM(get_stat(self)->st_dev);
672 return ULL2NUM(get_stat(self)->st_dev);
688rb_stat_dev_major(
VALUE self)
691 return UINT2NUM(get_stat(self)->stx_dev_major);
693 return UINT2NUM(major(get_stat(self)->st_dev));
711rb_stat_dev_minor(
VALUE self)
714 return UINT2NUM(get_stat(self)->stx_dev_minor);
716 return UINT2NUM(minor(get_stat(self)->st_dev));
733rb_stat_ino(
VALUE self)
735 rb_io_stat_data *ptr = get_stat(self);
736#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
738 return rb_integer_unpack(&ptr->st_ino, 2,
739 SIZEOF_STRUCT_STAT_ST_INO, 0,
743 return UIANY2NUM(ptr->ST_(ino));
761rb_stat_mode(
VALUE self)
763 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
779rb_stat_nlink(
VALUE self)
782 const rb_io_stat_data *ptr = get_stat(self);
784 return UIANY2NUM(ptr->ST_(nlink));
798rb_stat_uid(
VALUE self)
800 return UIDT2NUM(get_stat(self)->ST_(uid));
814rb_stat_gid(
VALUE self)
816 return GIDT2NUM(get_stat(self)->ST_(gid));
832rb_stat_rdev(
VALUE self)
835 unsigned int m = get_stat(self)->stx_rdev_major;
836 unsigned int n = get_stat(self)->stx_rdev_minor;
838#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
840#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
841 return DEVT2NUM(get_stat(self)->ST_(rdev));
842#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
843 return ULONG2NUM(get_stat(self)->ST_(rdev));
845 return ULL2NUM(get_stat(self)->ST_(rdev));
861rb_stat_rdev_major(
VALUE self)
864 return UINT2NUM(get_stat(self)->stx_rdev_major);
865#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
866 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
884rb_stat_rdev_minor(
VALUE self)
887 return UINT2NUM(get_stat(self)->stx_rdev_minor);
888#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
889 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
905rb_stat_size(
VALUE self)
907 return OFFT2NUM(get_stat(self)->ST_(size));
922rb_stat_blksize(
VALUE self)
924#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
925 return ULONG2NUM(get_stat(self)->ST_(blksize));
943rb_stat_blocks(
VALUE self)
945#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
946# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
947 return ULL2NUM(get_stat(self)->ST_(blocks));
949 return ULONG2NUM(get_stat(self)->ST_(blocks));
957stat_atimespec(
const struct stat *st)
960 ts.tv_sec = st->st_atime;
961#if defined(HAVE_STRUCT_STAT_ST_ATIM)
962 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
963#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
964 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
965#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
966 ts.tv_nsec = (uint32_t)st->st_atimensec;
975statx_atimespec(
const rb_io_stat_data *st)
977 return st->stx_atime;
980# define statx_atimespec stat_atimespec
990stat_atime(
const struct stat *st)
992 return stat_time(stat_atimespec(st));
996stat_mtimespec(
const struct stat *st)
999 ts.tv_sec = st->st_mtime;
1000#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1001 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1002#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1003 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1004#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1005 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1013stat_mtime(
const struct stat *st)
1015 return stat_time(stat_mtimespec(st));
1019stat_ctimespec(
const struct stat *st)
1022 ts.tv_sec = st->st_ctime;
1023#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1024 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1025#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1026 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1027#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1028 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1037statx_ctimespec(
const rb_io_stat_data *st)
1039 return st->stx_ctime;
1042# define statx_ctimespec stat_ctimespec
1046stat_ctime(
const struct stat *st)
1048 return stat_time(stat_ctimespec(st));
1051#define HAVE_STAT_BIRTHTIME
1052#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1054statx_birthtime(
const rb_io_stat_data *st)
1059#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1060static VALUE statx_birthtime(
const rb_io_stat_data *st);
1061#elif defined(_WIN32)
1062# define statx_birthtime stat_ctime
1064# undef HAVE_STAT_BIRTHTIME
1079rb_stat_atime(
VALUE self)
1081 return stat_time(statx_atimespec(get_stat(self)));
1095rb_stat_mtime(
VALUE self)
1097 return stat_time(statx_mtimespec(get_stat(self)));
1115rb_stat_ctime(
VALUE self)
1117 return stat_time(statx_ctimespec(get_stat(self)));
1120#if defined(HAVE_STAT_BIRTHTIME)
1144rb_stat_birthtime(
VALUE self)
1146 return statx_birthtime(get_stat(self));
1149# define rb_stat_birthtime rb_f_notimplement
1168rb_stat_inspect(
VALUE self)
1172 static const struct {
1176 {
"dev", rb_stat_dev},
1177 {
"ino", rb_stat_ino},
1178 {
"mode", rb_stat_mode},
1179 {
"nlink", rb_stat_nlink},
1180 {
"uid", rb_stat_uid},
1181 {
"gid", rb_stat_gid},
1182 {
"rdev", rb_stat_rdev},
1183 {
"size", rb_stat_size},
1184 {
"blksize", rb_stat_blksize},
1185 {
"blocks", rb_stat_blocks},
1186 {
"atime", rb_stat_atime},
1187 {
"mtime", rb_stat_mtime},
1188 {
"ctime", rb_stat_ctime},
1189#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1190 {
"birthtime", rb_stat_birthtime},
1196 if (!rb_st->initialized) {
1204 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1212 v = (*member[i].func)(self);
1214 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1216 else if (i == 0 || i == 6) {
1217 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1237no_gvl_fstat(
void *data)
1240 return (
VALUE)fstat(arg->file.fd, arg->st);
1244fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1248 data.file.fd = fptr->
fd;
1251 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1255no_gvl_stat(
void * data)
1258 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1262stat_without_gvl(
const char *path,
struct stat *st)
1266 data.file.path = path;
1269 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1272#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1273 defined(HAVE_STRUCT_STATX_STX_BTIME)
1275# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1278# ifdef HAVE_SYSCALL_H
1279# include <syscall.h>
1280# elif defined HAVE_SYS_SYSCALL_H
1281# include <sys/syscall.h>
1283# if defined __linux__
1284# include <linux/stat.h>
1286statx(
int dirfd,
const char *pathname,
int flags,
1287 unsigned int mask,
struct statx *statxbuf)
1289 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1294typedef struct no_gvl_rb_io_stat_data {
1300} no_gvl_rb_io_stat_data;
1303io_blocking_statx(
void *data)
1305 no_gvl_rb_io_stat_data *arg = data;
1306 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1310no_gvl_statx(
void *data)
1312 return (
void *)io_blocking_statx(data);
1316statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1318 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1321 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1325lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1327 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1330 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1334fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1336 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1339 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1342#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1345rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1350 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1355 result = fstatx_without_gvl(fptr, stx, mask);
1360 file = rb_str_encode_ospath(file);
1361 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1367# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1369NORETURN(
static void statx_notimplement(
const char *field_name));
1374statx_notimplement(
const char *field_name)
1377 "%s is unimplemented on this filesystem",
1382statx_birthtime(
const rb_io_stat_data *stx)
1384 if (!statx_has_birthtime(stx)) {
1386 statx_notimplement(
"birthtime");
1388 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1393# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1394# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1395# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1396# define rb_statx(file, stx, mask) rb_stat(file, stx)
1397# define STATX(path, st, mask) STAT(path, st)
1399#if defined(HAVE_STAT_BIRTHTIME)
1400# define statx_has_birthtime(st) 1
1402# define statx_has_birthtime(st) 0
1409# define FSTAT(fd, st) fstat(fd, st)
1418 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1423 result = fstat_without_gvl(fptr, st);
1428 file = rb_str_encode_ospath(file);
1429 result = stat_without_gvl(RSTRING_PTR(file), st);
1451 fname = rb_str_encode_ospath(fname);
1452 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1453 rb_sys_fail_path(fname);
1455 return rb_statx_new(&st);
1474rb_io_stat(
VALUE obj)
1480 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1481 rb_sys_fail_path(fptr->
pathv);
1483 return rb_statx_new(&st);
1488no_gvl_lstat(
void *ptr)
1491 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1495lstat_without_gvl(
const char *path,
struct stat *st)
1499 data.file.path = path;
1502 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1526 fname = rb_str_encode_ospath(fname);
1527 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1528 rb_sys_fail_path(fname);
1530 return rb_statx_new(&st);
1532 return rb_file_s_stat(klass, fname);
1551rb_file_lstat(
VALUE obj)
1560 path = rb_str_encode_ospath(fptr->
pathv);
1561 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1562 rb_sys_fail_path(fptr->
pathv);
1564 return rb_statx_new(&st);
1566 return rb_io_stat(obj);
1571rb_group_member(GETGROUPS_T gid)
1573#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1582 if (getgid() == gid || getegid() == gid)
1585 groups = getgroups(0, NULL);
1586 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1587 anum = getgroups(groups, gary);
1588 while (--anum >= 0) {
1589 if (gary[anum] == gid) {
1602# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1605#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1606#define USE_GETEUID 1
1611eaccess(
const char *path,
int mode)
1620 if (getuid() == euid && getgid() == getegid())
1621 return access(path, mode);
1623 if (STAT(path, &st) < 0)
1633 if (st.st_mode & S_IXUGO)
1639 if (st.st_uid == euid)
1641 else if (rb_group_member(st.st_gid))
1644 if ((
int)(st.st_mode & mode) == mode)
return 0;
1648 return access(path, mode);
1659nogvl_eaccess(
void *ptr)
1663 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1667rb_eaccess(
VALUE fname,
int mode)
1672 fname = rb_str_encode_ospath(fname);
1676 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1680nogvl_access(
void *ptr)
1684 return (
void *)(
VALUE)access(aa->path, aa->mode);
1688rb_access(
VALUE fname,
int mode)
1693 fname = rb_str_encode_ospath(fname);
1697 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1732# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1738 if (S_ISDIR(st.st_mode))
return Qtrue;
1759# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1765 if (S_ISFIFO(st.st_mode))
return Qtrue;
1788# define S_ISLNK(m) _S_ISLNK(m)
1791# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1794# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1804 fname = rb_str_encode_ospath(fname);
1806 if (S_ISLNK(st.st_mode))
return Qtrue;
1829# define S_ISSOCK(m) _S_ISSOCK(m)
1832# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1835# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1845 if (S_ISSOCK(st.st_mode))
return Qtrue;
1867# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1869# define S_ISBLK(m) (0)
1877 if (S_ISBLK(st.st_mode))
return Qtrue;
1897# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1903 if (S_ISCHR(st.st_mode))
return Qtrue;
1942 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
1957rb_file_readable_real_p(
VALUE obj,
VALUE fname)
1959 return RBOOL(rb_access(fname, R_OK) >= 0);
1963# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
1967# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
1987rb_file_world_readable_p(
VALUE obj,
VALUE fname)
1993 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
1994 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2014 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2029rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2031 return RBOOL(rb_access(fname, W_OK) >= 0);
2051rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2057 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2058 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2082 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2101rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2103 return RBOOL(rb_access(fname, X_OK) >= 0);
2107# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2128 return RBOOL(S_ISREG(st.st_mode));
2147 return RBOOL(st.st_size == 0);
2166 if (st.st_size == 0)
return Qnil;
2187 return RBOOL(st.st_uid == geteuid());
2196 return RBOOL(st.st_uid == getuid());
2217 if (rb_group_member(st.st_gid))
return Qtrue;
2222#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2224check3rdbyte(
VALUE fname,
int mode)
2229 return RBOOL(st.st_mode & mode);
2246 return check3rdbyte(fname, S_ISUID);
2265 return check3rdbyte(fname, S_ISGID);
2284 return check3rdbyte(fname, S_ISVTX);
2313 struct stat st1, st2;
2317 if (st1.st_dev != st2.st_dev)
return Qfalse;
2318 if (st1.st_ino != st2.st_ino)
return Qfalse;
2322 return rb_w32_file_identical_p(fname1, fname2);
2340 if (
rb_stat(fname, &st) < 0) {
2343 rb_syserr_fail_path(e, fname);
2349rb_file_ftype(mode_t mode)
2353 if (S_ISREG(mode)) {
2356 else if (S_ISDIR(mode)) {
2359 else if (S_ISCHR(mode)) {
2360 t =
"characterSpecial";
2363 else if (S_ISBLK(mode)) {
2368 else if (S_ISFIFO(mode)) {
2373 else if (S_ISLNK(mode)) {
2378 else if (S_ISSOCK(mode)) {
2410 fname = rb_str_encode_ospath(fname);
2412 rb_sys_fail_path(fname);
2415 return rb_file_ftype(st.st_mode);
2435 if (
rb_stat(fname, &st) < 0) {
2438 rb_syserr_fail_path(e, fname);
2440 return stat_time(stat_atimespec(&st));
2455rb_file_atime(
VALUE obj)
2461 if (fstat(fptr->
fd, &st) == -1) {
2462 rb_sys_fail_path(fptr->
pathv);
2464 return stat_time(stat_atimespec(&st));
2484 if (
rb_stat(fname, &st) < 0) {
2487 rb_syserr_fail_path(e, fname);
2489 return stat_time(stat_mtimespec(&st));
2503rb_file_mtime(
VALUE obj)
2509 if (fstat(fptr->
fd, &st) == -1) {
2510 rb_sys_fail_path(fptr->
pathv);
2512 return stat_time(stat_mtimespec(&st));
2536 if (
rb_stat(fname, &st) < 0) {
2539 rb_syserr_fail_path(e, fname);
2541 return stat_time(stat_ctimespec(&st));
2558rb_file_ctime(
VALUE obj)
2564 if (fstat(fptr->
fd, &st) == -1) {
2565 rb_sys_fail_path(fptr->
pathv);
2567 return stat_time(stat_ctimespec(&st));
2570#if defined(HAVE_STAT_BIRTHTIME)
2590 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2593 rb_syserr_fail_path(e, fname);
2595 return statx_birthtime(&st);
2598# define rb_file_s_birthtime rb_f_notimplement
2601#if defined(HAVE_STAT_BIRTHTIME)
2615rb_file_birthtime(
VALUE obj)
2621 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2622 rb_sys_fail_path(fptr->
pathv);
2624 return statx_birthtime(&st);
2627# define rb_file_birthtime rb_f_notimplement
2639 rb_io_flush_raw(file, 0);
2642 if (fstat(fptr->
fd, &st) == -1) {
2643 rb_sys_fail_path(fptr->
pathv);
2664file_size(
VALUE self)
2666 return OFFT2NUM(rb_file_size(self));
2675nogvl_chmod(
void *ptr)
2678 int ret = chmod(data->path, data->mode);
2679 return (
void *)(
VALUE)ret;
2683rb_chmod(
const char *path, mode_t mode)
2689 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2693chmod_internal(
const char *path,
void *mode)
2695 return chmod(path, *(mode_t *)mode);
2719 return apply2files(chmod_internal, argc, argv, &mode);
2723struct nogvl_fchmod_data {
2729io_blocking_fchmod(
void *ptr)
2731 struct nogvl_fchmod_data *data = ptr;
2732 int ret = fchmod(data->fd, data->mode);
2737rb_fchmod(
struct rb_io* io, mode_t mode)
2740 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2741 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2763#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2771 if (rb_fchmod(fptr, mode) == -1) {
2772 if (HAVE_FCHMOD ||
errno != ENOSYS)
2773 rb_sys_fail_path(fptr->
pathv);
2776 if (!HAVE_FCHMOD)
return INT2FIX(0);
2779#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2781 path = rb_str_encode_ospath(fptr->
pathv);
2782 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2783 rb_sys_fail_path(fptr->
pathv);
2789#if defined(HAVE_LCHMOD)
2791lchmod_internal(
const char *path,
void *mode)
2793 return lchmod(path, *(mode_t *)mode);
2814 return apply2files(lchmod_internal, argc, argv, &mode);
2817#define rb_file_s_lchmod rb_f_notimplement
2820static inline rb_uid_t
2824 return (rb_uid_t)-1;
2829static inline rb_gid_t
2833 return (rb_gid_t)-1;
2844chown_internal(
const char *path,
void *arg)
2847 return chown(path, args->owner, args->group);
2871 arg.owner = to_uid(*argv++);
2872 arg.group = to_gid(*argv++);
2874 return apply2files(chown_internal, argc, argv, &arg);
2886nogvl_chown(
void *ptr)
2889 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
2893rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
2896 .as = {.path = path},
2897 .new = {.owner = owner, .group = group},
2899 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
2904nogvl_fchown(
void *ptr)
2907 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
2911rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
2916 .new = {.owner = owner, .group = group},
2918 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
2952 path = rb_str_encode_ospath(fptr->
pathv);
2953 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
2954 rb_sys_fail_path(fptr->
pathv);
2956 if (rb_fchown(fptr->
fd, o, g) == -1)
2957 rb_sys_fail_path(fptr->
pathv);
2963#if defined(HAVE_LCHOWN)
2965lchown_internal(
const char *path,
void *arg)
2968 return lchown(path, args->owner, args->group);
2988 arg.owner = to_uid(*argv++);
2989 arg.group = to_gid(*argv++);
2991 return apply2files(lchown_internal, argc, argv, &arg);
2994#define rb_file_s_lchown rb_f_notimplement
3004NORETURN(
static void utime_failed(
struct apply_arg *));
3010 VALUE path = aa->fn[aa->i].path;
3013 if (ua->tsp && e == EINVAL) {
3016 VALUE atime = ua->atime;
3017 VALUE mtime = ua->mtime;
3019 if (!
NIL_P(atime)) {
3022 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3025 if (
NIL_P(a)) e[0] = m;
3041 rb_syserr_fail_path(e, path);
3045#if defined(HAVE_UTIMES)
3047# if !defined(HAVE_UTIMENSAT)
3049# elif defined(__APPLE__) && \
3050 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3052# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3053typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3057static
inline utimensat_func *
3064# define utimensat rb_utimensat()
3071utime_internal(
const char *path,
void *arg)
3074 const struct timespec *tsp = v->tsp;
3075 struct timeval tvbuf[2], *tvp = NULL;
3077#if defined(HAVE_UTIMENSAT)
3078# if defined(__APPLE__)
3079 const int try_utimensat = utimensat != NULL;
3080 const int try_utimensat_follow = utimensat != NULL;
3082# define TRY_UTIMENSAT 1
3083 static int try_utimensat = 1;
3084# ifdef AT_SYMLINK_NOFOLLOW
3085 static int try_utimensat_follow = 1;
3087 const int try_utimensat_follow = 0;
3092 if (v->follow ? try_utimensat_follow : try_utimensat) {
3093# ifdef AT_SYMLINK_NOFOLLOW
3095 flags = AT_SYMLINK_NOFOLLOW;
3099 int result = utimensat(AT_FDCWD, path, tsp, flags);
3100# ifdef TRY_UTIMENSAT
3101 if (result < 0 &&
errno == ENOSYS) {
3102# ifdef AT_SYMLINK_NOFOLLOW
3103 try_utimensat_follow = 0;
3115 tvbuf[0].tv_sec = tsp[0].tv_sec;
3116 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3117 tvbuf[1].tv_sec = tsp[1].tv_sec;
3118 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3122 if (v->follow)
return lutimes(path, tvp);
3124 return utimes(path, tvp);
3129#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3137utime_internal(
const char *path,
void *arg)
3141 struct utimbuf utbuf, *utp = NULL;
3143 utbuf.actime = tsp[0].tv_sec;
3144 utbuf.modtime = tsp[1].tv_sec;
3147 return utime(path, utp);
3152utime_internal_i(
int argc,
VALUE *argv,
int follow)
3155 struct timespec tss[2], *tsp = NULL;
3158 args.atime = *argv++;
3159 args.mtime = *argv++;
3161 args.follow = follow;
3163 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3166 if (args.atime == args.mtime)
3173 return apply2files(utime_internal, argc, argv, &args);
3190 return utime_internal_i(argc, argv, FALSE);
3193#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3209 return utime_internal_i(argc, argv, TRUE);
3212#define rb_file_s_lutime rb_f_notimplement
3215#ifdef RUBY_FUNCTION_NAME_STRING
3216# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3218# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3220#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3221NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3223syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3227 const int max_pathlen = MAX_PATH;
3229 const int max_pathlen = MAXPATHLEN;
3240#ifdef RUBY_FUNCTION_NAME_STRING
3241 rb_syserr_fail_path_in(func, e, str);
3243 rb_syserr_fail_path(e, str);
3265 from = rb_str_encode_ospath(from);
3266 to = rb_str_encode_ospath(to);
3269 sys_fail2(from, to);
3274#define rb_file_s_link rb_f_notimplement
3295 from = rb_str_encode_ospath(from);
3296 to = rb_str_encode_ospath(to);
3299 sys_fail2(from, to);
3304#define rb_file_s_symlink rb_f_notimplement
3322 return rb_readlink(path, rb_filesystem_encoding());
3325struct readlink_arg {
3332nogvl_readlink(
void *ptr)
3334 struct readlink_arg *ra = ptr;
3336 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3340readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3342 struct readlink_arg ra;
3344 ra.path = RSTRING_PTR(path);
3345 ra.buf = RSTRING_PTR(buf);
3348 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3359 path = rb_str_encode_ospath(path);
3360 v = rb_enc_str_new(0, size, enc);
3361 while ((rv = readlink_without_gvl(path, v, size)) == size
3363 || (rv < 0 &&
errno == ERANGE)
3372 rb_str_resize(v, 0);
3373 rb_syserr_fail_path(e, path);
3375 rb_str_resize(v, rv);
3380#define rb_file_s_readlink rb_f_notimplement
3384unlink_internal(
const char *path,
void *arg)
3386 return unlink(path);
3406rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3408 return apply2files(unlink_internal, argc, argv, 0);
3417no_gvl_rename(
void *ptr)
3421 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3442 f = rb_str_encode_ospath(from);
3443 t = rb_str_encode_ospath(to);
3446#if defined __CYGWIN__
3449 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3454 if (chmod(ra.dst, 0666) == 0 &&
3455 unlink(ra.dst) == 0 &&
3456 rename(ra.src, ra.dst) == 0)
3460 syserr_fail2(e, from, to);
3495 rb_error_arity(argc, 0, 1);
3503#if defined __CYGWIN__ || defined DOSISH
3505#define DOSISH_DRIVE_LETTER
3506#define FILE_ALT_SEPARATOR '\\'
3508#ifdef FILE_ALT_SEPARATOR
3509#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3511static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3514#define isdirsep(x) ((x) == '/')
3527# define USE_NTFS_ADS 1
3529# define USE_NTFS_ADS 0
3534#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3536#define istrailinggarbage(x) 0
3540# define isADS(x) ((x) == ':')
3545#define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc)))
3546#define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
3548#if defined(DOSISH_UNC)
3549#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3551#define has_unc(buf) 0
3554#ifdef DOSISH_DRIVE_LETTER
3556has_drive_letter(
const char *buf)
3558 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3571 char *drvcwd, *oldcwd;
3582 if (chdir(drive) == 0) {
3595not_same_drive(
VALUE path,
int drive)
3597 const char *p = RSTRING_PTR(path);
3598 if (RSTRING_LEN(path) < 2)
return 0;
3599 if (has_drive_letter(p)) {
3610skiproot(
const char *path,
const char *end,
rb_encoding *enc)
3612#ifdef DOSISH_DRIVE_LETTER
3613 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3615 while (path < end && isdirsep(*path)) path++;
3616 return (
char *)path;
3619#define nextdirsep rb_enc_path_next
3623 while (s < e && !isdirsep(*s)) {
3629#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3630#define skipprefix rb_enc_path_skip_prefix
3632#define skipprefix(path, end, enc) (path)
3635rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3637#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3639 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3641 while (path < end && isdirsep(*path)) path++;
3642 if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1]))
3643 path = rb_enc_path_next(path + 1, end, enc);
3644 return (
char *)path;
3647#ifdef DOSISH_DRIVE_LETTER
3648 if (has_drive_letter(path))
3649 return (
char *)(path + 2);
3652 return (
char *)path;
3656skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3658#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3659 char *p = skipprefix(path, end, enc);
3660 while (isdirsep(*p)) p++;
3663 return skiproot(path, end, enc);
3667#define strrdirsep rb_enc_path_last_separator
3669rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3672 while (path < end) {
3673 if (isdirsep(*path)) {
3674 const char *tmp = path++;
3675 while (path < end && isdirsep(*path)) path++;
3676 if (path >= end)
break;
3680 Inc(path, end, enc);
3687chompdirsep(
const char *path,
const char *end,
rb_encoding *enc)
3689 while (path < end) {
3690 if (isdirsep(*path)) {
3691 const char *last = path++;
3692 while (path < end && isdirsep(*path)) path++;
3693 if (path >= end)
return (
char *)last;
3696 Inc(path, end, enc);
3699 return (
char *)path;
3705 if (path < end && isdirsep(*path)) path++;
3706 return chompdirsep(path, end, enc);
3713 int encidx = rb_enc_to_index(enc);
3714 if (encidx == ENCINDEX_US_ASCII) {
3715 encidx = rb_enc_get_index(path1);
3716 if (encidx == ENCINDEX_US_ASCII)
3717 encidx = rb_enc_get_index(path2);
3718 enc = rb_enc_from_index(encidx);
3725ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
3727 while (path < end && *path ==
'.') path++;
3728 while (path < end && !isADS(*path)) {
3729 if (istrailinggarbage(*path)) {
3730 const char *last = path++;
3731 while (path < end && istrailinggarbage(*path)) path++;
3732 if (path >= end || isADS(*path))
return (
char *)last;
3734 else if (isdirsep(*path)) {
3735 const char *last = path++;
3736 while (path < end && isdirsep(*path)) path++;
3737 if (path >= end)
return (
char *)last;
3738 if (isADS(*path)) path++;
3741 Inc(path, end, enc);
3744 return (
char *)path;
3748#define BUFCHECK(cond) do {\
3751 do {buflen *= 2;} while (cond);\
3752 rb_str_resize(result, buflen);\
3753 buf = RSTRING_PTR(result);\
3755 pend = buf + buflen;\
3760 p = buf = RSTRING_PTR(result),\
3761 buflen = RSTRING_LEN(result),\
3765# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3767# define SKIPPATHSEP(p) 1
3770#define BUFCOPY(srcptr, srclen) do { \
3771 const int skip = SKIPPATHSEP(p); \
3772 rb_str_set_len(result, p-buf+skip); \
3773 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3775 memcpy(p, (srcptr), (srclen)); \
3779#define WITH_ROOTDIFF(stmt) do { \
3780 long rootdiff = root - buf; \
3782 root = buf + rootdiff; \
3786copy_home_path(
VALUE result,
const char *dir)
3789#if defined DOSISH || defined __CYGWIN__
3796 dirlen = strlen(dir);
3797 rb_str_resize(result, dirlen);
3798 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3799 encidx = rb_filesystem_encindex();
3800 rb_enc_associate_index(result, encidx);
3801#if defined DOSISH || defined __CYGWIN__
3802 enc = rb_enc_from_index(encidx);
3803 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) {
3816 VALUE dirname = rb_getpwdirnam_for_login(user);
3817 if (dirname ==
Qnil) {
3818 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3820 const char *dir = RSTRING_PTR(dirname);
3822 extern char *getlogin(
void);
3823 const char *pwPtr = 0;
3825 # define endpwent() ((void)0)
3826 const char *dir, *username = RSTRING_PTR(user);
3837 if ((login = getlogin()) && strcasecmp(username, login) == 0)
3838 dir = pwPtr = getenv(
"HOME");
3840 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3843 copy_home_path(result, dir);
3849rb_default_home_dir(
VALUE result)
3851 const char *dir = getenv(
"HOME");
3853#if defined HAVE_PWD_H
3867 VALUE login_name = rb_getlogin();
3869# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
3874 if (
NIL_P(login_name)) {
3875 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
3879 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
3880 if (
NIL_P(pw_dir)) {
3881 pw_dir = rb_getpwdiruid();
3882 if (
NIL_P(pw_dir)) {
3883 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
3888 copy_home_path(result, RSTRING_PTR(pw_dir));
3889 rb_str_resize(pw_dir, 0);
3894 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
3896 return copy_home_path(result, dir);
3902#if NORMALIZE_UTF8PATH
3903 VALUE path = rb_str_normalize_ospath(ptr,
len);
3904 rb_enc_associate(path, fsenc);
3907 return rb_enc_str_new(ptr,
len, fsenc);
3914 char *buf, *cwdp = dir;
3918 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
3919 dirname = ospath_new(dir, dirlen, fsenc);
3920 if (!rb_enc_compatible(fname, dirname)) {
3924 rb_enc_check(fname, dirname);
3925 rb_bug(
"unreachable");
3927 rb_encoding *direnc = fs_enc_check(fname, dirname);
3928 if (direnc != fsenc) {
3932 else if (NORMALIZE_UTF8PATH) {
3937 do {buflen *= 2;}
while (dirlen > buflen);
3938 rb_str_resize(result, buflen);
3939 buf = RSTRING_PTR(result);
3940 memcpy(buf, cwdp, dirlen);
3942 if (!
NIL_P(dirname)) rb_str_resize(dirname, 0);
3943 rb_enc_associate(result, *enc);
3944 return buf + dirlen;
3948rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
3950 const char *s, *b, *fend;
3951 char *buf, *p, *pend, *root;
3952 size_t buflen, bdiff;
3953 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
3956 fend = s + RSTRING_LEN(fname);
3957 enc = rb_enc_get(fname);
3960 if (s[0] ==
'~' && abs_mode == 0) {
3962 if (isdirsep(s[1]) || s[1] ==
'\0') {
3967 rb_default_home_dir(result);
3970 s = nextdirsep(b = s, fend, enc);
3973 BUFCHECK(bdiff + userlen >= buflen);
3974 memcpy(p, b, userlen);
3977 rb_enc_associate(result, enc);
3978 rb_home_dir_of(result, result);
3982 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
3984 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
3985 (
int)userlen, b, fname);
3988 rb_raise(rb_eArgError,
"non-absolute home");
3994#ifdef DOSISH_DRIVE_LETTER
3996 else if (has_drive_letter(s)) {
3997 if (isdirsep(s[2])) {
4000 BUFCHECK(bdiff + 2 >= buflen);
4004 rb_enc_copy(result, fname);
4009 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4010 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4018 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4023 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4026 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4031 else if (!rb_is_absolute_path(s)) {
4032 if (!
NIL_P(dname)) {
4033 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4034 rb_enc_associate(result, fs_enc_check(result, fname));
4039 char *e = append_fspath(result, fname,
ruby_getcwd(), &enc, fsenc);
4043#if defined DOSISH || defined __CYGWIN__
4047 p = skipprefix(buf, p, enc);
4051 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4056 do s++;
while (isdirsep(*s));
4059 BUFCHECK(bdiff >= buflen);
4060 memset(buf,
'/',
len);
4062 rb_enc_associate(result, fs_enc_check(result, fname));
4064 if (p > buf && p[-1] ==
'/')
4068 BUFCHECK(bdiff + 1 >= buflen);
4073 BUFCHECK(bdiff + 1 >= buflen);
4075 root = skipprefix(buf, p+1, enc);
4087 if (*(s+1) ==
'\0' || isdirsep(*(s+1))) {
4091 if (!(n = strrdirsep(root, p, enc))) {
4101 do ++s;
while (istrailinggarbage(*s));
4106#if defined DOSISH || defined __CYGWIN__
4121 while (s < fend && istrailinggarbage(*s)) s++;
4131#if defined DOSISH || defined __CYGWIN__
4135 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4143 int n = ignored_char_p(s, fend, enc);
4146 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4162 static const char prime[] =
":$DATA";
4163 enum {prime_len =
sizeof(prime) -1};
4167 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4170 if (isADS(*(s - (prime_len+1)))) {
4173 else if (memchr(b,
':', s - prime_len - b)) {
4182 if (p == skiproot(buf, p + !!*p, enc) - 1) p++;
4186 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4191 WIN32_FIND_DATAW wfd;
4194#ifdef HAVE_CYGWIN_CONV_PATH
4195 char *w32buf = NULL;
4196 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4198 char w32buf[MAXPATHLEN];
4202 int lnk_added = 0, is_symlink = 0;
4206 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4212 path = *buf ? buf :
"/";
4213#ifdef HAVE_CYGWIN_CONV_PATH
4214 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4217 if (lnk_added) bufsize += 4;
4219 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4224 bufsize = MAXPATHLEN;
4225 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4229 if (is_symlink && b == w32buf) {
4231 strlcat(w32buf, p, bufsize);
4233 strlcat(w32buf,
".lnk", bufsize);
4244 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4245 tmp = rb_str_encode_ospath(result);
4247 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4249 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4251 h = FindFirstFileW(wstr, &wfd);
4253 if (h != INVALID_HANDLE_VALUE) {
4256 len = lstrlenW(wfd.cFileName);
4258 if (lnk_added &&
len > 4 &&
4259 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4260 wfd.cFileName[
len -= 4] = L
'\0';
4267 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4268 if (tmp == result) {
4269 BUFCHECK(bdiff +
len >= buflen);
4270 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4274 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4275 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4276 rb_utf8_encoding(), 0,
Qnil);
4278 rb_str_resize(tmp, 0);
4291 rb_enc_check(fname, result);
4297#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
4300str_shrink(
VALUE str)
4302 rb_str_resize(str, RSTRING_LEN(str));
4306#define expand_path(fname, dname, abs_mode, long_name, result) \
4307 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4309#define check_expand_path_args(fname, dname) \
4310 (((fname) = rb_get_path(fname)), \
4311 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4314file_expand_path_1(
VALUE fname)
4316 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, EXPAND_PATH_BUFFER());
4322 check_expand_path_args(fname, dname);
4323 return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4327rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4329 return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4333rb_file_s_expand_path(
int argc,
const VALUE *argv)
4336 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4370 return rb_file_s_expand_path(c, v);
4376 check_expand_path_args(fname, dname);
4377 return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4381rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4384 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4403 return rb_file_s_absolute_path(c, v);
4419 VALUE path = rb_get_path(fname);
4421 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4425enum rb_realpath_mode {
4429 RB_REALPATH_MODE_MAX
4433realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4434 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4436 const char *pend = unresolved + strlen(unresolved);
4440 while (unresolved < pend) {
4441 const char *testname = unresolved;
4442 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4443 long testnamelen = unresolved_firstsep - unresolved;
4444 const char *unresolved_nextname = unresolved_firstsep;
4445 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4446 unresolved_nextname++;
4447 unresolved = unresolved_nextname;
4448 if (testnamelen == 1 && testname[0] ==
'.') {
4450 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4451 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4452 const char *resolved_str = RSTRING_PTR(*resolvedp);
4453 const char *resolved_names = resolved_str + *prefixlenp;
4454 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc);
4455 long len = lastsep ? lastsep - resolved_names : 0;
4456 rb_str_resize(*resolvedp, *prefixlenp +
len);
4462 if (*prefixlenp < RSTRING_LEN(testpath))
4464#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4465 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4466 const char *prefix = RSTRING_PTR(testpath);
4467 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4472 checkval = rb_hash_aref(loopcheck, testpath);
4473 if (!
NIL_P(checkval)) {
4474 if (checkval ==
ID2SYM(resolving)) {
4475 if (mode == RB_REALPATH_CHECK) {
4479 rb_syserr_fail_path(ELOOP, testpath);
4488 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4491 if (e == ENOENT && !
NIL_P(fallback)) {
4492 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4497 if (mode == RB_REALPATH_CHECK)
return -1;
4499 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4500 rb_syserr_fail_path(e, testpath);
4501 *resolvedp = testpath;
4505 rb_syserr_fail_path(e, testpath);
4509 if (S_ISLNK(sbuf.st_mode)) {
4512 const char *link_prefix, *link_names;
4513 long link_prefixlen;
4514 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4515 link = rb_readlink(testpath, enc);
4516 link_prefix = RSTRING_PTR(link);
4517 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4518 link_prefixlen = link_names - link_prefix;
4519 if (link_prefixlen > 0) {
4523 tmpenc = fs_enc_check(*resolvedp, link);
4526 *prefixlenp = link_prefixlen;
4528 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4529 loopcheck, mode, !*unresolved_firstsep))
4538 rb_hash_aset(loopcheck, s, s);
4539 *resolvedp = testpath;
4548rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4552 VALUE unresolved_path;
4557 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4558 char *ptr, *prefixptr = NULL, *pend;
4563 if (!
NIL_P(basedir)) {
4568 enc = rb_enc_get(unresolved_path);
4569 unresolved_path = TO_OSPATH(unresolved_path);
4571 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4572 if (ptr != path_names) {
4573 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4577 if (!
NIL_P(basedir)) {
4579 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4580 if (ptr != basedir_names) {
4586 curdir = rb_dir_getwd_ospath();
4588 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4593 pend = prefixptr + prefixlen;
4594 ptr = chompdirsep(prefixptr, pend, enc);
4596 prefixlen = ++ptr - prefixptr;
4599#ifdef FILE_ALT_SEPARATOR
4600 while (prefixptr < ptr) {
4601 if (*prefixptr == FILE_ALT_SEPARATOR) {
4604 Inc(prefixptr, pend, enc);
4608 switch (rb_enc_to_index(enc)) {
4609 case ENCINDEX_ASCII_8BIT:
4610 case ENCINDEX_US_ASCII:
4611 rb_enc_associate_index(resolved, rb_filesystem_encindex());
4614 loopcheck = rb_hash_new();
4616 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
4619 if (basedir_names) {
4620 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
4623 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
4626 if (origenc && origenc != rb_enc_get(resolved)) {
4628 rb_enc_associate(resolved, origenc);
4642#ifndef HAVE_REALPATH
4644rb_check_realpath_emulate_try(
VALUE arg)
4647 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
4651rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
4655#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
4656 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
4658# include <sys/syslimits.h>
4659# define NEEDS_REALPATH_BUFFER 1
4663rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4666 VALUE unresolved_path;
4667 char *resolved_ptr = NULL;
4669# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
4670 char resolved_buffer[PATH_MAX];
4672 char *
const resolved_buffer = NULL;
4675 if (mode == RB_REALPATH_DIR) {
4676 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4680 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
4681 unresolved_path = rb_file_join(
rb_assoc_new(basedir, unresolved_path));
4683 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4685 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
4694 if (
errno == ENOTSUP ||
4696 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4697 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4700 if (mode == RB_REALPATH_CHECK) {
4703 rb_sys_fail_path(unresolved_path);
4705 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4706# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
4710# if !defined(__linux__) && !defined(__APPLE__)
4714 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4715 if (mode == RB_REALPATH_CHECK) {
4718 rb_sys_fail_path(unresolved_path);
4722 if (origenc && origenc != rb_enc_get(resolved)) {
4726 rb_enc_associate(resolved, origenc);
4729 if (is_broken_string(resolved)) {
4730 rb_enc_associate(resolved, rb_filesystem_encoding());
4731 if (is_broken_string(resolved)) {
4732 rb_enc_associate(resolved, rb_ascii8bit_encoding());
4739 if (mode == RB_REALPATH_CHECK) {
4743 arg[2] = (
VALUE)origenc;
4746 rb_check_realpath_emulate_rescue,
Qnil);
4749 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4755rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
4757 const enum rb_realpath_mode mode =
4758 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4759 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4765 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4782rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
4785 VALUE path = argv[0];
4787 return rb_realpath_internal(basedir, path, 1);
4803rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
4806 VALUE path = argv[0];
4808 return rb_realpath_internal(basedir, path, 0);
4812rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
4816 const char *s, *last;
4818 if (!e || !l2)
return 0;
4820 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4821 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
4822 if (c ==
'.')
return l0;
4827 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4832 if (l1 < l2)
return l1;
4835 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
4836#if CASEFOLD_FILESYSTEM
4837#define fncomp strncasecmp
4839#define fncomp strncmp
4841 if (fncomp(s, e, l2) == 0) {
4848ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
4850 const char *p, *q, *e, *end;
4851#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4856 end = name + (alllen ? (size_t)*alllen : strlen(name));
4857 name = skipprefix(name, end, enc);
4858#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4861 while (isdirsep(*name))
4866#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4870#ifdef DOSISH_DRIVE_LETTER
4871 else if (*p ==
':') {
4884 if (!(p = strrdirsep(name, end, enc))) {
4888 while (isdirsep(*p)) p++;
4891 n = ntfs_tail(p, end, enc) - p;
4893 n = chompdirsep(p, end, enc) - p;
4895 for (q = p; q - p < n && *q ==
'.'; q++);
4896 for (e = 0; q - p < n; Inc(q, end, enc)) {
4897 if (*q ==
'.') e = q;
4930 VALUE fname, fext, basename;
4931 const char *name, *p;
4939 enc = check_path_encoding(fext);
4943 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
4944 enc = rb_enc_get(fname);
4947 if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
4950 p = ruby_enc_find_basename(name, &f, &n, enc);
4958 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
4967 rb_enc_copy(basename, fname);
4971static VALUE rb_file_dirname_n(
VALUE fname,
int n);
4993rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
4999 return rb_file_dirname_n(argv[0], n);
5005 return rb_file_dirname_n(fname, 1);
5009rb_file_dirname_n(
VALUE fname,
int n)
5011 const char *name, *root, *p, *end;
5017 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5020 end = name + RSTRING_LEN(fname);
5021 enc = rb_enc_get(fname);
5022 root = skiproot(name, end, enc);
5024 if (root > name + 1 && isdirsep(*name))
5025 root = skipprefix(name = root - 2, end, enc);
5027 if (root > name + 1)
5030 if (n > (end - root + 1) / 2) {
5040 if (!(p = strrdirsep(root, end, enc))) p = root;
5043 seps =
ALLOCV_N(
const char *, sepsv, n);
5044 for (i = 0; i < n; ++i) seps[i] = root;
5046 for (p = root; p < end; ) {
5048 const char *tmp = p++;
5049 while (p < end && isdirsep(*p)) p++;
5050 if (p >= end)
break;
5065#ifdef DOSISH_DRIVE_LETTER
5066 if (has_drive_letter(name) && isdirsep(*(name + 2))) {
5067 const char *top = skiproot(name + 2, end, enc);
5074#ifdef DOSISH_DRIVE_LETTER
5075 if (has_drive_letter(name) && root == name + 2 && p - name == 2)
5078 rb_enc_copy(dirname, fname);
5097 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5099 p = strrdirsep(name, end, enc);
5103 do name = ++p;
while (isdirsep(*p));
5106 while (*p && *p ==
'.') p++;
5108 if (*p ==
'.' || istrailinggarbage(*p)) {
5110 const char *last = p++, *dot = last;
5111 while (istrailinggarbage(*p)) {
5112 if (*p ==
'.') dot = p;
5115 if (!*p || isADS(*p)) {
5119 if (*last ==
'.' || dot > last) e = dot;
5126 else if (isADS(*p)) {
5130 else if (isdirsep(*p))
5137 if (!e || e == name)
5176 const char *name, *e;
5182 len = RSTRING_LEN(fname);
5183 e = ruby_enc_find_extname(name, &
len, rb_enc_get(fname));
5204 return rb_get_path(fname);
5226file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5228 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5229 return rb_file_join(arg);
5233rb_file_join(
VALUE ary)
5237 const char *name, *tail;
5247 check_path_encoding(tmp);
5248 len += RSTRING_LEN(tmp);
5256 RBASIC_CLEAR_CLASS(result);
5259 switch (OBJ_BUILTIN_TYPE(tmp)) {
5261 if (!checked) check_path_encoding(tmp);
5266 rb_raise(rb_eArgError,
"recursive array");
5278 rb_enc_copy(result, tmp);
5281 tail = chompdirsep(name, name +
len, rb_enc_get(result));
5282 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5289 enc = fs_enc_check(result, tmp);
5291 rb_enc_associate(result, enc);
5312 return rb_file_join(args);
5315#if defined(HAVE_TRUNCATE)
5316struct truncate_arg {
5322nogvl_truncate(
void *ptr)
5324 struct truncate_arg *ta = ptr;
5325 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5346 struct truncate_arg ta;
5351 path = rb_str_encode_ospath(path);
5354 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5356 rb_sys_fail_path(path);
5360#define rb_file_s_truncate rb_f_notimplement
5363#if defined(HAVE_FTRUNCATE)
5364struct ftruncate_arg {
5370nogvl_ftruncate(
void *ptr)
5372 struct ftruncate_arg *fa = ptr;
5374 return (
VALUE)ftruncate(fa->fd, fa->pos);
5395 struct ftruncate_arg fa;
5402 rb_io_flush_raw(obj, 0);
5404 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5405 rb_sys_fail_path(fptr->
pathv);
5410#define rb_file_truncate rb_f_notimplement
5427#include <winerror.h>
5431rb_thread_flock(
void *data)
5434 int old_errno =
errno;
5436 int *op = data, ret = flock(op[0], op[1]);
5439 if (GetLastError() == ERROR_NOT_LOCKED) {
5497 op[1] = op1 =
NUM2INT(operation);
5502 rb_io_flush_raw(obj, 0);
5504 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
5509#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5512 if (op1 & LOCK_NB)
return Qfalse;
5515 time.tv_usec = 100 * 1000;
5521#if defined(ERESTART)
5527 rb_syserr_fail_path(e, fptr->
pathv);
5534test_check(
int n,
int argc,
VALUE *argv)
5540 for (i=1; i<n; i++) {
5547#define CHECK(n) test_check((n), argc, argv)
5639 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
5643 return rb_file_blockdev_p(0, argv[1]);
5646 return rb_file_chardev_p(0, argv[1]);
5649 return rb_file_directory_p(0, argv[1]);
5652 return rb_file_exist_p(0, argv[1]);
5655 return rb_file_file_p(0, argv[1]);
5658 return rb_file_sgid_p(0, argv[1]);
5661 return rb_file_grpowned_p(0, argv[1]);
5664 return rb_file_sticky_p(0, argv[1]);
5667 return rb_file_symlink_p(0, argv[1]);
5670 return rb_file_owned_p(0, argv[1]);
5673 return rb_file_rowned_p(0, argv[1]);
5676 return rb_file_pipe_p(0, argv[1]);
5679 return rb_file_readable_p(0, argv[1]);
5682 return rb_file_readable_real_p(0, argv[1]);
5685 return rb_file_size_p(0, argv[1]);
5688 return rb_file_socket_p(0, argv[1]);
5691 return rb_file_suid_p(0, argv[1]);
5694 return rb_file_writable_p(0, argv[1]);
5697 return rb_file_writable_real_p(0, argv[1]);
5700 return rb_file_executable_p(0, argv[1]);
5703 return rb_file_executable_real_p(0, argv[1]);
5706 return rb_file_zero_p(0, argv[1]);
5710 if (strchr(
"MAC", cmd)) {
5712 VALUE fname = argv[1];
5715 if (
rb_stat(fname, &st) == -1) {
5718 rb_syserr_fail_path(e, fname);
5723 return stat_atime(&st);
5725 return stat_mtime(&st);
5727 return stat_ctime(&st);
5733 return rb_file_identical_p(0, argv[1], argv[2]);
5736 if (strchr(
"=<>", cmd)) {
5737 struct stat st1, st2;
5744 t1 = stat_mtimespec(&st1);
5745 t2 = stat_mtimespec(&st2);
5749 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
5753 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
5754 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
5758 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
5759 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
5766 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
5769 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
5788rb_stat_s_alloc(
VALUE klass)
5809 fname = rb_str_encode_ospath(fname);
5811 rb_sys_fail_path(fname);
5818 rb_st->initialized =
true;
5835 *copy_rb_st = *orig_rb_st;
5854rb_stat_ftype(
VALUE obj)
5856 return rb_file_ftype(get_stat(obj)->ST_(mode));
5873 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
5889 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
5915 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
5936 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
5959 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
5980 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
5998rb_stat_owned(
VALUE obj)
6000 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6005rb_stat_rowned(
VALUE obj)
6007 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6024rb_stat_grpowned(
VALUE obj)
6027 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6046 rb_io_stat_data *st = get_stat(obj);
6049 if (geteuid() == 0)
return Qtrue;
6052 if (rb_stat_owned(obj))
6053 return RBOOL(st->ST_(mode) & S_IRUSR);
6056 if (rb_stat_grpowned(obj))
6057 return RBOOL(st->ST_(mode) & S_IRGRP);
6060 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6079 rb_io_stat_data *st = get_stat(obj);
6082 if (getuid() == 0)
return Qtrue;
6085 if (rb_stat_rowned(obj))
6086 return RBOOL(st->ST_(mode) & S_IRUSR);
6089 if (rb_group_member(get_stat(obj)->ST_(gid)))
6090 return RBOOL(st->ST_(mode) & S_IRGRP);
6093 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6112rb_stat_wr(
VALUE obj)
6115 rb_io_stat_data *st = get_stat(obj);
6116 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6117 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6137 rb_io_stat_data *st = get_stat(obj);
6140 if (geteuid() == 0)
return Qtrue;
6143 if (rb_stat_owned(obj))
6144 return RBOOL(st->ST_(mode) & S_IWUSR);
6147 if (rb_stat_grpowned(obj))
6148 return RBOOL(st->ST_(mode) & S_IWGRP);
6151 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6170 rb_io_stat_data *st = get_stat(obj);
6173 if (getuid() == 0)
return Qtrue;
6176 if (rb_stat_rowned(obj))
6177 return RBOOL(st->ST_(mode) & S_IWUSR);
6180 if (rb_group_member(get_stat(obj)->ST_(gid)))
6181 return RBOOL(st->ST_(mode) & S_IWGRP);
6184 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6203rb_stat_ww(
VALUE obj)
6206 rb_io_stat_data *st = get_stat(obj);
6207 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6208 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6230 rb_io_stat_data *st = get_stat(obj);
6233 if (geteuid() == 0) {
6234 return RBOOL(st->ST_(mode) & S_IXUGO);
6238 if (rb_stat_owned(obj))
6239 return RBOOL(st->ST_(mode) & S_IXUSR);
6242 if (rb_stat_grpowned(obj))
6243 return RBOOL(st->ST_(mode) & S_IXGRP);
6246 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6262 rb_io_stat_data *st = get_stat(obj);
6265 if (getuid() == 0) {
6266 return RBOOL(st->ST_(mode) & S_IXUGO);
6270 if (rb_stat_rowned(obj))
6271 return RBOOL(st->ST_(mode) & S_IXUSR);
6274 if (rb_group_member(get_stat(obj)->ST_(gid)))
6275 return RBOOL(st->ST_(mode) & S_IXGRP);
6278 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6297 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6315 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6334 rb_off_t size = get_stat(obj)->ST_(size);
6336 if (size == 0)
return Qnil;
6352rb_stat_suid(
VALUE obj)
6355 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6373rb_stat_sgid(
VALUE obj)
6376 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6394rb_stat_sticky(
VALUE obj)
6397 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
6402#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6403#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6414nogvl_mkfifo(
void *ptr)
6416 struct mkfifo_arg *ma = ptr;
6418 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
6435 struct mkfifo_arg ma;
6444 path = rb_str_encode_ospath(path);
6445 ma.path = RSTRING_PTR(path);
6446 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
6447 rb_sys_fail_path(path);
6452#define rb_file_s_mkfifo rb_f_notimplement
6455static VALUE rb_mFConst;
6458rb_file_const(
const char *name,
VALUE value)
6460 rb_define_const(rb_mFConst, name, value);
6464rb_is_absolute_path(
const char *path)
6466#ifdef DOSISH_DRIVE_LETTER
6467 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
6470 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
6473 if (path[0] ==
'/')
return 1;
6478#ifndef ENABLE_PATH_CHECK
6479# if defined DOSISH || defined __CYGWIN__
6480# define ENABLE_PATH_CHECK 0
6482# define ENABLE_PATH_CHECK 1
6486#if ENABLE_PATH_CHECK
6488path_check_0(
VALUE path)
6496 if (!rb_is_absolute_path(p0)) {
6506 p0 = RSTRING_PTR(path);
6508 e0 = p0 + RSTRING_LEN(path);
6509 enc = rb_enc_get(path);
6514 if (STAT(p0, &st) == 0 && S_ISDIR(st.st_mode) && (st.st_mode & S_IWOTH)
6516 && !(p && (st.st_mode & S_ISVTX))
6518 && !access(p0, W_OK)) {
6519 rb_enc_warn(enc,
"Insecure world writable dir %s in PATH, mode 0%"
6520#
if SIZEOF_DEV_T > SIZEOF_INT
6530 s = strrdirsep(p0, e0, enc);
6532 if (!s || s == p0)
return 1;
6541rb_path_check(
const char *path)
6543 rb_warn_deprecated_to_remove_at(3.6,
"rb_path_check", NULL);
6544#if ENABLE_PATH_CHECK
6545 const char *p0, *p, *pend;
6548 if (!path)
return 1;
6550 pend = path + strlen(path);
6552 p = strchr(path, sep);
6560 if (p0 > pend)
break;
6561 p = strchr(p0, sep);
6569ruby_is_fd_loadable(
int fd)
6576 if (fstat(fd, &st) < 0)
6579 if (S_ISREG(st.st_mode))
6582 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6585 if (S_ISDIR(st.st_mode))
6596rb_file_load_ok(
const char *path)
6603 int mode = (O_RDONLY |
6604#if defined O_NONBLOCK
6606#elif defined O_NDELAY
6612 if (!rb_gc_for_fd(
errno))
return 0;
6614 if (fd < 0)
return 0;
6617 ret = ruby_is_fd_loadable(fd);
6624is_explicit_relative(
const char *path)
6626 if (*path++ !=
'.')
return 0;
6627 if (*path ==
'.') path++;
6628 return isdirsep(*path);
6634 int encidx = rb_enc_get_index(orig);
6635 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
6636 encidx = rb_filesystem_encindex();
6637 rb_enc_associate_index(path, encidx);
6645rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
6648 VALUE fname = *filep, load_path, tmp;
6652 if (!ext[0])
return 0;
6655 fname = file_expand_path_1(fname);
6656 f = RSTRING_PTR(fname);
6661 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6662 if (!expanded) fname = file_expand_path_1(fname);
6663 fnlen = RSTRING_LEN(fname);
6664 for (i=0; ext[i]; i++) {
6666 if (rb_file_load_ok(RSTRING_PTR(fname))) {
6667 *filep = copy_path_class(fname, *filep);
6675 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6676 if (!load_path)
return 0;
6679 RBASIC_CLEAR_CLASS(fname);
6680 fnlen = RSTRING_LEN(fname);
6682 rb_enc_associate_index(tmp, rb_usascii_encindex());
6683 for (j=0; ext[j]; j++) {
6685 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6689 if (RSTRING_LEN(str) == 0)
continue;
6690 rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6691 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6692 *filep = copy_path_class(tmp, *filep);
6698 rb_str_resize(tmp, 0);
6706 VALUE tmp, load_path;
6711 tmp = file_expand_path_1(path);
6712 path = copy_path_class(tmp, path);
6713 f = RSTRING_PTR(path);
6717 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6718 if (!rb_file_load_ok(f))
return 0;
6720 path = copy_path_class(file_expand_path_1(path), path);
6724 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6729 rb_enc_associate_index(tmp, rb_usascii_encindex());
6730 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6733 if (RSTRING_LEN(str) > 0) {
6734 rb_file_expand_path_internal(path, str, 0, 0, tmp);
6735 f = RSTRING_PTR(tmp);
6736 if (rb_file_load_ok(f))
goto found;
6739 rb_str_resize(tmp, 0);
6747 return copy_path_class(tmp, path);
6750#define define_filetest_function(name, func, argc) do { \
6751 rb_define_module_function(rb_mFileTest, name, func, argc); \
6752 rb_define_singleton_method(rb_cFile, name, func, argc); \
6755const char ruby_null_device[] =
6758#elif defined AMIGA || defined __amigaos__
7562#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
7563 rb_CFString_class_initialize_before_fork();
7571 define_filetest_function(
"directory?", rb_file_directory_p, 1);
7572 define_filetest_function(
"exist?", rb_file_exist_p, 1);
7573 define_filetest_function(
"readable?", rb_file_readable_p, 1);
7574 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
7575 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
7576 define_filetest_function(
"writable?", rb_file_writable_p, 1);
7577 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
7578 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
7579 define_filetest_function(
"executable?", rb_file_executable_p, 1);
7580 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
7581 define_filetest_function(
"file?", rb_file_file_p, 1);
7582 define_filetest_function(
"zero?", rb_file_zero_p, 1);
7583 define_filetest_function(
"empty?", rb_file_zero_p, 1);
7584 define_filetest_function(
"size?", rb_file_size_p, 1);
7585 define_filetest_function(
"size", rb_file_s_size, 1);
7586 define_filetest_function(
"owned?", rb_file_owned_p, 1);
7587 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
7589 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
7590 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
7591 define_filetest_function(
"socket?", rb_file_socket_p, 1);
7593 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
7594 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
7596 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
7597 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
7598 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
7600 define_filetest_function(
"identical?", rb_file_identical_p, 2);
7638 separator = rb_fstring_lit(
"/");
7640 rb_define_const(
rb_cFile,
"Separator", separator);
7642 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
7994 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
7996 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
7998 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
8000 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
8002 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
8004 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
8005#if defined(O_NDELAY) || defined(O_NONBLOCK)
8007# define O_NONBLOCK O_NDELAY
8010 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
8013 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
8016 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
8022 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
8023#ifndef O_SHARE_DELETE
8024# define O_SHARE_DELETE 0
8027 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
8030 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
8034 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
8038 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
8042 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
8046 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
8050 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
8054 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8058 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8060 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8062 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8064 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8067 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.
Defines RBIMPL_ATTR_NONSTRING.
#define RBIMPL_ATTR_NONSTRING()
Wraps (or simulates) __attribute__((nonstring))
#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.