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
641 if (ts1.tv_sec == ts2.tv_sec) {
642 if (ts1.tv_nsec == ts2.tv_nsec)
return INT2FIX(0);
643 if (ts1.tv_nsec < ts2.tv_nsec)
return INT2FIX(-1);
646 if (ts1.tv_sec < ts2.tv_sec)
return INT2FIX(-1);
652#define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
655# define NUM2DEVT(v) NUM2UINT(v)
658# define DEVT2NUM(v) UINT2NUM(v)
660#ifndef PRI_DEVT_PREFIX
661# define PRI_DEVT_PREFIX ""
675rb_stat_dev(
VALUE self)
678 unsigned int m = get_stat(self)->stx_dev_major;
679 unsigned int n = get_stat(self)->stx_dev_minor;
681#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_DEV_T
682 return DEVT2NUM(get_stat(self)->st_dev);
683#elif SIZEOF_STRUCT_STAT_ST_DEV <= SIZEOF_LONG
684 return ULONG2NUM(get_stat(self)->st_dev);
686 return ULL2NUM(get_stat(self)->st_dev);
702rb_stat_dev_major(
VALUE self)
705 return UINT2NUM(get_stat(self)->stx_dev_major);
707 return UINT2NUM(major(get_stat(self)->st_dev));
725rb_stat_dev_minor(
VALUE self)
728 return UINT2NUM(get_stat(self)->stx_dev_minor);
730 return UINT2NUM(minor(get_stat(self)->st_dev));
747rb_stat_ino(
VALUE self)
749 rb_io_stat_data *ptr = get_stat(self);
750#ifdef HAVE_STRUCT_STAT_ST_INOHIGH
752 return rb_integer_unpack(&ptr->st_ino, 2,
753 SIZEOF_STRUCT_STAT_ST_INO, 0,
757 return UIANY2NUM(ptr->ST_(ino));
775rb_stat_mode(
VALUE self)
777 return UINT2NUM(ST2UINT(get_stat(self)->ST_(mode)));
793rb_stat_nlink(
VALUE self)
796 const rb_io_stat_data *ptr = get_stat(self);
798 return UIANY2NUM(ptr->ST_(nlink));
812rb_stat_uid(
VALUE self)
814 return UIDT2NUM(get_stat(self)->ST_(uid));
828rb_stat_gid(
VALUE self)
830 return GIDT2NUM(get_stat(self)->ST_(gid));
846rb_stat_rdev(
VALUE self)
849 unsigned int m = get_stat(self)->stx_rdev_major;
850 unsigned int n = get_stat(self)->stx_rdev_minor;
852#elif !defined(HAVE_STRUCT_STAT_ST_RDEV)
854#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_DEV_T
855 return DEVT2NUM(get_stat(self)->ST_(rdev));
856#elif SIZEOF_STRUCT_STAT_ST_RDEV <= SIZEOF_LONG
857 return ULONG2NUM(get_stat(self)->ST_(rdev));
859 return ULL2NUM(get_stat(self)->ST_(rdev));
875rb_stat_rdev_major(
VALUE self)
878 return UINT2NUM(get_stat(self)->stx_rdev_major);
879#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(major)
880 return UINT2NUM(major(get_stat(self)->ST_(rdev)));
898rb_stat_rdev_minor(
VALUE self)
901 return UINT2NUM(get_stat(self)->stx_rdev_minor);
902#elif defined(HAVE_STRUCT_STAT_ST_RDEV) && defined(minor)
903 return UINT2NUM(minor(get_stat(self)->ST_(rdev)));
919rb_stat_size(
VALUE self)
921 return OFFT2NUM(get_stat(self)->ST_(size));
936rb_stat_blksize(
VALUE self)
938#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
939 return ULONG2NUM(get_stat(self)->ST_(blksize));
957rb_stat_blocks(
VALUE self)
959#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
960# if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
961 return ULL2NUM(get_stat(self)->ST_(blocks));
963 return ULONG2NUM(get_stat(self)->ST_(blocks));
971stat_atimespec(
const struct stat *st)
974 ts.tv_sec = st->st_atime;
975#if defined(HAVE_STRUCT_STAT_ST_ATIM)
976 ts.tv_nsec = (uint32_t)st->st_atim.tv_nsec;
977#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
978 ts.tv_nsec = (uint32_t)st->st_atimespec.tv_nsec;
979#elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
980 ts.tv_nsec = (uint32_t)st->st_atimensec;
989statx_atimespec(
const rb_io_stat_data *st)
991 return st->stx_atime;
994# define statx_atimespec stat_atimespec
1004stat_atime(
const struct stat *st)
1006 return stat_time(stat_atimespec(st));
1010stat_mtimespec(
const struct stat *st)
1013 ts.tv_sec = st->st_mtime;
1014#if defined(HAVE_STRUCT_STAT_ST_MTIM)
1015 ts.tv_nsec = (uint32_t)st->st_mtim.tv_nsec;
1016#elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
1017 ts.tv_nsec = (uint32_t)st->st_mtimespec.tv_nsec;
1018#elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
1019 ts.tv_nsec = (uint32_t)st->st_mtimensec;
1027stat_mtime(
const struct stat *st)
1029 return stat_time(stat_mtimespec(st));
1033stat_ctimespec(
const struct stat *st)
1036 ts.tv_sec = st->st_ctime;
1037#if defined(HAVE_STRUCT_STAT_ST_CTIM)
1038 ts.tv_nsec = (uint32_t)st->st_ctim.tv_nsec;
1039#elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
1040 ts.tv_nsec = (uint32_t)st->st_ctimespec.tv_nsec;
1041#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
1042 ts.tv_nsec = (uint32_t)st->st_ctimensec;
1051statx_ctimespec(
const rb_io_stat_data *st)
1053 return st->stx_ctime;
1056# define statx_ctimespec stat_ctimespec
1060stat_ctime(
const struct stat *st)
1062 return stat_time(stat_ctimespec(st));
1065#define HAVE_STAT_BIRTHTIME
1066#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1068statx_birthtime(
const rb_io_stat_data *st)
1073#elif defined(HAVE_STRUCT_STATX_STX_BTIME)
1074static VALUE statx_birthtime(
const rb_io_stat_data *st);
1075#elif defined(_WIN32)
1076# define statx_birthtime stat_ctime
1078# undef HAVE_STAT_BIRTHTIME
1093rb_stat_atime(
VALUE self)
1095 return stat_time(statx_atimespec(get_stat(self)));
1109rb_stat_mtime(
VALUE self)
1111 return stat_time(statx_mtimespec(get_stat(self)));
1129rb_stat_ctime(
VALUE self)
1131 return stat_time(statx_ctimespec(get_stat(self)));
1134#if defined(HAVE_STAT_BIRTHTIME)
1158rb_stat_birthtime(
VALUE self)
1160 return statx_birthtime(get_stat(self));
1163# define rb_stat_birthtime rb_f_notimplement
1182rb_stat_inspect(
VALUE self)
1186 static const struct {
1190 {
"dev", rb_stat_dev},
1191 {
"ino", rb_stat_ino},
1192 {
"mode", rb_stat_mode},
1193 {
"nlink", rb_stat_nlink},
1194 {
"uid", rb_stat_uid},
1195 {
"gid", rb_stat_gid},
1196 {
"rdev", rb_stat_rdev},
1197 {
"size", rb_stat_size},
1198 {
"blksize", rb_stat_blksize},
1199 {
"blocks", rb_stat_blocks},
1200 {
"atime", rb_stat_atime},
1201 {
"mtime", rb_stat_mtime},
1202 {
"ctime", rb_stat_ctime},
1203#if defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC)
1204 {
"birthtime", rb_stat_birthtime},
1210 if (!rb_st->initialized) {
1218 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
1226 v = (*member[i].func)(self);
1228 rb_str_catf(str,
"0%lo", (
unsigned long)
NUM2ULONG(v));
1230 else if (i == 0 || i == 6) {
1231 rb_str_catf(str,
"0x%"PRI_DEVT_PREFIX
"x", NUM2DEVT(v));
1251no_gvl_fstat(
void *data)
1254 return (
VALUE)fstat(arg->file.fd, arg->st);
1258fstat_without_gvl(
rb_io_t *fptr,
struct stat *st)
1262 data.file.fd = fptr->
fd;
1265 return (
int)rb_io_blocking_region(fptr, no_gvl_fstat, &data);
1269no_gvl_stat(
void * data)
1272 return (
void *)(
VALUE)STAT(arg->file.path, arg->st);
1276stat_without_gvl(
const char *path,
struct stat *st)
1280 data.file.path = path;
1283 return IO_WITHOUT_GVL_INT(no_gvl_stat, &data);
1286#if !defined(HAVE_STRUCT_STAT_ST_BIRTHTIMESPEC) && \
1287 defined(HAVE_STRUCT_STATX_STX_BTIME)
1289# define STATX(path, st, mask) statx(AT_FDCWD, path, 0, mask, st)
1292# ifdef HAVE_SYSCALL_H
1293# include <syscall.h>
1294# elif defined HAVE_SYS_SYSCALL_H
1295# include <sys/syscall.h>
1297# if defined __linux__
1298# include <linux/stat.h>
1300statx(
int dirfd,
const char *pathname,
int flags,
1301 unsigned int mask,
struct statx *statxbuf)
1303 return (
int)syscall(__NR_statx, dirfd, pathname, flags, mask, statxbuf);
1308typedef struct no_gvl_rb_io_stat_data {
1314} no_gvl_rb_io_stat_data;
1317io_blocking_statx(
void *data)
1319 no_gvl_rb_io_stat_data *arg = data;
1320 return (
VALUE)statx(arg->fd, arg->path, arg->flags, arg->mask, arg->stx);
1324no_gvl_statx(
void *data)
1326 return (
void *)io_blocking_statx(data);
1330statx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1332 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, 0, mask};
1335 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1339lstatx_without_gvl(
const char *path, rb_io_stat_data *stx,
unsigned int mask)
1341 no_gvl_rb_io_stat_data data = {stx, AT_FDCWD, path, AT_SYMLINK_NOFOLLOW, mask};
1344 return IO_WITHOUT_GVL_INT(no_gvl_statx, &data);
1348fstatx_without_gvl(
rb_io_t *fptr, rb_io_stat_data *stx,
unsigned int mask)
1350 no_gvl_rb_io_stat_data data = {stx, fptr->
fd,
"", AT_EMPTY_PATH, mask};
1353 return (
int)rb_io_blocking_region(fptr, io_blocking_statx, &data);
1356#define FSTATX(fd, st) statx(fd, "", AT_EMPTY_PATH, STATX_ALL, st)
1359rb_statx(
VALUE file,
struct statx *stx,
unsigned int mask)
1364 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1369 result = fstatx_without_gvl(fptr, stx, mask);
1374 file = rb_str_encode_ospath(file);
1375 result = statx_without_gvl(RSTRING_PTR(file), stx, mask);
1381# define statx_has_birthtime(st) ((st)->stx_mask & STATX_BTIME)
1383NORETURN(
static void statx_notimplement(
const char *field_name));
1388statx_notimplement(
const char *field_name)
1391 "%s is unimplemented on this filesystem",
1396statx_birthtime(
const rb_io_stat_data *stx)
1398 if (!statx_has_birthtime(stx)) {
1400 statx_notimplement(
"birthtime");
1402 return rb_time_nano_new((time_t)stx->stx_btime.tv_sec, stx->stx_btime.tv_nsec);
1407# define statx_without_gvl(path, st, mask) stat_without_gvl(path, st)
1408# define fstatx_without_gvl(fptr, st, mask) fstat_without_gvl(fptr, st)
1409# define lstatx_without_gvl(path, st, mask) lstat_without_gvl(path, st)
1410# define rb_statx(file, stx, mask) rb_stat(file, stx)
1411# define STATX(path, st, mask) STAT(path, st)
1413#if defined(HAVE_STAT_BIRTHTIME)
1414# define statx_has_birthtime(st) 1
1416# define statx_has_birthtime(st) 0
1423# define FSTAT(fd, st) fstat(fd, st)
1432 tmp = rb_check_convert_type_with_id(file,
T_FILE,
"IO", idTo_io);
1437 result = fstat_without_gvl(fptr, st);
1442 file = rb_str_encode_ospath(file);
1443 result = stat_without_gvl(RSTRING_PTR(file), st);
1465 fname = rb_str_encode_ospath(fname);
1466 if (statx_without_gvl(RSTRING_PTR(fname), &st, STATX_ALL) < 0) {
1467 rb_sys_fail_path(fname);
1469 return rb_statx_new(&st);
1488rb_io_stat(
VALUE obj)
1494 if (fstatx_without_gvl(fptr, &st, STATX_ALL) == -1) {
1495 rb_sys_fail_path(fptr->
pathv);
1497 return rb_statx_new(&st);
1502no_gvl_lstat(
void *ptr)
1505 return (
void *)(
VALUE)lstat(arg->file.path, arg->st);
1509lstat_without_gvl(
const char *path,
struct stat *st)
1513 data.file.path = path;
1516 return IO_WITHOUT_GVL_INT(no_gvl_lstat, &data);
1540 fname = rb_str_encode_ospath(fname);
1541 if (lstatx_without_gvl(
StringValueCStr(fname), &st, STATX_ALL) == -1) {
1542 rb_sys_fail_path(fname);
1544 return rb_statx_new(&st);
1546 return rb_file_s_stat(klass, fname);
1565rb_file_lstat(
VALUE obj)
1574 path = rb_str_encode_ospath(fptr->
pathv);
1575 if (lstatx_without_gvl(RSTRING_PTR(path), &st, STATX_ALL) == -1) {
1576 rb_sys_fail_path(fptr->
pathv);
1578 return rb_statx_new(&st);
1580 return rb_io_stat(obj);
1585rb_group_member(GETGROUPS_T gid)
1587#if defined(_WIN32) || !defined(HAVE_GETGROUPS)
1596 if (getgid() == gid || getegid() == gid)
1599 groups = getgroups(0, NULL);
1600 gary =
ALLOCV_N(GETGROUPS_T, v, groups);
1601 anum = getgroups(groups, gary);
1602 while (--anum >= 0) {
1603 if (gary[anum] == gid) {
1616# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1619#if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1620#define USE_GETEUID 1
1625eaccess(
const char *path,
int mode)
1634 if (getuid() == euid && getgid() == getegid())
1635 return access(path, mode);
1637 if (STAT(path, &st) < 0)
1647 if (st.st_mode & S_IXUGO)
1653 if (st.st_uid == euid)
1655 else if (rb_group_member(st.st_gid))
1658 if ((
int)(st.st_mode & mode) == mode)
return 0;
1662 return access(path, mode);
1673nogvl_eaccess(
void *ptr)
1677 return (
void *)(
VALUE)eaccess(aa->path, aa->mode);
1681rb_eaccess(
VALUE fname,
int mode)
1686 fname = rb_str_encode_ospath(fname);
1690 return IO_WITHOUT_GVL_INT(nogvl_eaccess, &aa);
1694nogvl_access(
void *ptr)
1698 return (
void *)(
VALUE)access(aa->path, aa->mode);
1702rb_access(
VALUE fname,
int mode)
1707 fname = rb_str_encode_ospath(fname);
1711 return IO_WITHOUT_GVL_INT(nogvl_access, &aa);
1746# define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1752 if (S_ISDIR(st.st_mode))
return Qtrue;
1773# define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1779 if (S_ISFIFO(st.st_mode))
return Qtrue;
1802# define S_ISLNK(m) _S_ISLNK(m)
1805# define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1808# define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1818 fname = rb_str_encode_ospath(fname);
1820 if (S_ISLNK(st.st_mode))
return Qtrue;
1843# define S_ISSOCK(m) _S_ISSOCK(m)
1846# define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1849# define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1859 if (S_ISSOCK(st.st_mode))
return Qtrue;
1881# define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1883# define S_ISBLK(m) (0)
1891 if (S_ISBLK(st.st_mode))
return Qtrue;
1911# define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1917 if (S_ISCHR(st.st_mode))
return Qtrue;
1956 return RBOOL(rb_eaccess(fname, R_OK) >= 0);
1971rb_file_readable_real_p(
VALUE obj,
VALUE fname)
1973 return RBOOL(rb_access(fname, R_OK) >= 0);
1977# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
1981# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
2001rb_file_world_readable_p(
VALUE obj,
VALUE fname)
2007 if ((st.st_mode & (S_IROTH)) == S_IROTH) {
2008 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2028 return RBOOL(rb_eaccess(fname, W_OK) >= 0);
2043rb_file_writable_real_p(
VALUE obj,
VALUE fname)
2045 return RBOOL(rb_access(fname, W_OK) >= 0);
2065rb_file_world_writable_p(
VALUE obj,
VALUE fname)
2071 if ((st.st_mode & (S_IWOTH)) == S_IWOTH) {
2072 return UINT2NUM(st.st_mode & (S_IRUGO|S_IWUGO|S_IXUGO));
2096 return RBOOL(rb_eaccess(fname, X_OK) >= 0);
2115rb_file_executable_real_p(
VALUE obj,
VALUE fname)
2117 return RBOOL(rb_access(fname, X_OK) >= 0);
2121# define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
2142 return RBOOL(S_ISREG(st.st_mode));
2161 return RBOOL(st.st_size == 0);
2180 if (st.st_size == 0)
return Qnil;
2201 return RBOOL(st.st_uid == geteuid());
2210 return RBOOL(st.st_uid == getuid());
2231 if (rb_group_member(st.st_gid))
return Qtrue;
2236#if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
2238check3rdbyte(
VALUE fname,
int mode)
2243 return RBOOL(st.st_mode & mode);
2260 return check3rdbyte(fname, S_ISUID);
2279 return check3rdbyte(fname, S_ISGID);
2298 return check3rdbyte(fname, S_ISVTX);
2327 struct stat st1, st2;
2331 if (st1.st_dev != st2.st_dev)
return Qfalse;
2332 if (st1.st_ino != st2.st_ino)
return Qfalse;
2336 return rb_w32_file_identical_p(fname1, fname2);
2354 if (
rb_stat(fname, &st) < 0) {
2357 rb_syserr_fail_path(e, fname);
2363rb_file_ftype(mode_t mode)
2367 if (S_ISREG(mode)) {
2370 else if (S_ISDIR(mode)) {
2373 else if (S_ISCHR(mode)) {
2374 t =
"characterSpecial";
2377 else if (S_ISBLK(mode)) {
2382 else if (S_ISFIFO(mode)) {
2387 else if (S_ISLNK(mode)) {
2392 else if (S_ISSOCK(mode)) {
2424 fname = rb_str_encode_ospath(fname);
2426 rb_sys_fail_path(fname);
2429 return rb_file_ftype(st.st_mode);
2449 if (
rb_stat(fname, &st) < 0) {
2452 rb_syserr_fail_path(e, fname);
2454 return stat_time(stat_atimespec(&st));
2469rb_file_atime(
VALUE obj)
2475 if (fstat(fptr->
fd, &st) == -1) {
2476 rb_sys_fail_path(fptr->
pathv);
2478 return stat_time(stat_atimespec(&st));
2498 if (
rb_stat(fname, &st) < 0) {
2501 rb_syserr_fail_path(e, fname);
2503 return stat_time(stat_mtimespec(&st));
2517rb_file_mtime(
VALUE obj)
2523 if (fstat(fptr->
fd, &st) == -1) {
2524 rb_sys_fail_path(fptr->
pathv);
2526 return stat_time(stat_mtimespec(&st));
2550 if (
rb_stat(fname, &st) < 0) {
2553 rb_syserr_fail_path(e, fname);
2555 return stat_time(stat_ctimespec(&st));
2572rb_file_ctime(
VALUE obj)
2578 if (fstat(fptr->
fd, &st) == -1) {
2579 rb_sys_fail_path(fptr->
pathv);
2581 return stat_time(stat_ctimespec(&st));
2584#if defined(HAVE_STAT_BIRTHTIME)
2604 if (rb_statx(fname, &st, STATX_BTIME) < 0) {
2607 rb_syserr_fail_path(e, fname);
2609 return statx_birthtime(&st);
2612# define rb_file_s_birthtime rb_f_notimplement
2615#if defined(HAVE_STAT_BIRTHTIME)
2629rb_file_birthtime(
VALUE obj)
2635 if (fstatx_without_gvl(fptr, &st, STATX_BTIME) == -1) {
2636 rb_sys_fail_path(fptr->
pathv);
2638 return statx_birthtime(&st);
2641# define rb_file_birthtime rb_f_notimplement
2653 rb_io_flush_raw(file, 0);
2656 if (fstat(fptr->
fd, &st) == -1) {
2657 rb_sys_fail_path(fptr->
pathv);
2678file_size(
VALUE self)
2680 return OFFT2NUM(rb_file_size(self));
2689nogvl_chmod(
void *ptr)
2692 int ret = chmod(data->path, data->mode);
2693 return (
void *)(
VALUE)ret;
2697rb_chmod(
const char *path, mode_t mode)
2703 return IO_WITHOUT_GVL_INT(nogvl_chmod, &data);
2707chmod_internal(
const char *path,
void *mode)
2709 return chmod(path, *(mode_t *)mode);
2733 return apply2files(chmod_internal, argc, argv, &mode);
2737struct nogvl_fchmod_data {
2743io_blocking_fchmod(
void *ptr)
2745 struct nogvl_fchmod_data *data = ptr;
2746 int ret = fchmod(data->fd, data->mode);
2751rb_fchmod(
struct rb_io* io, mode_t mode)
2754 struct nogvl_fchmod_data data = {.fd = io->
fd, .mode = mode};
2755 return (
int)rb_thread_io_blocking_region(io, io_blocking_fchmod, &data);
2777#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2785 if (rb_fchmod(fptr, mode) == -1) {
2786 if (HAVE_FCHMOD ||
errno != ENOSYS)
2787 rb_sys_fail_path(fptr->
pathv);
2790 if (!HAVE_FCHMOD)
return INT2FIX(0);
2793#if !defined HAVE_FCHMOD || !HAVE_FCHMOD
2795 path = rb_str_encode_ospath(fptr->
pathv);
2796 if (rb_chmod(RSTRING_PTR(path), mode) == -1)
2797 rb_sys_fail_path(fptr->
pathv);
2803#if defined(HAVE_LCHMOD)
2805lchmod_internal(
const char *path,
void *mode)
2807 return lchmod(path, *(mode_t *)mode);
2828 return apply2files(lchmod_internal, argc, argv, &mode);
2831#define rb_file_s_lchmod rb_f_notimplement
2834static inline rb_uid_t
2838 return (rb_uid_t)-1;
2843static inline rb_gid_t
2847 return (rb_gid_t)-1;
2858chown_internal(
const char *path,
void *arg)
2861 return chown(path, args->owner, args->group);
2885 arg.owner = to_uid(*argv++);
2886 arg.group = to_gid(*argv++);
2888 return apply2files(chown_internal, argc, argv, &arg);
2900nogvl_chown(
void *ptr)
2903 return (
void *)(
VALUE)chown(data->as.path, data->new.owner, data->new.group);
2907rb_chown(
const char *path, rb_uid_t owner, rb_gid_t group)
2910 .as = {.path = path},
2911 .new = {.owner = owner, .group = group},
2913 return IO_WITHOUT_GVL_INT(nogvl_chown, &data);
2918nogvl_fchown(
void *ptr)
2921 return (
void *)(
VALUE)fchown(data->as.fd, data->new.owner, data->new.group);
2925rb_fchown(
int fd, rb_uid_t owner, rb_gid_t group)
2930 .new = {.owner = owner, .group = group},
2932 return IO_WITHOUT_GVL_INT(nogvl_fchown, &data);
2966 path = rb_str_encode_ospath(fptr->
pathv);
2967 if (rb_chown(RSTRING_PTR(path), o, g) == -1)
2968 rb_sys_fail_path(fptr->
pathv);
2970 if (rb_fchown(fptr->
fd, o, g) == -1)
2971 rb_sys_fail_path(fptr->
pathv);
2977#if defined(HAVE_LCHOWN)
2979lchown_internal(
const char *path,
void *arg)
2982 return lchown(path, args->owner, args->group);
3002 arg.owner = to_uid(*argv++);
3003 arg.group = to_gid(*argv++);
3005 return apply2files(lchown_internal, argc, argv, &arg);
3008#define rb_file_s_lchown rb_f_notimplement
3018NORETURN(
static void utime_failed(
struct apply_arg *));
3024 VALUE path = aa->fn[aa->i].path;
3027 if (ua->tsp && e == EINVAL) {
3030 VALUE atime = ua->atime;
3031 VALUE mtime = ua->mtime;
3033 if (!
NIL_P(atime)) {
3036 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
3039 if (
NIL_P(a)) e[0] = m;
3055 rb_syserr_fail_path(e, path);
3059#if defined(HAVE_UTIMES)
3061# if !defined(HAVE_UTIMENSAT)
3063# elif defined(__APPLE__) && \
3064 (!defined(MAC_OS_X_VERSION_13_0) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_13_0))
3066# if __has_attribute(availability) && __has_warning("-Wunguarded-availability-new")
3067typedef int utimensat_func(
int,
const char *,
const struct timespec [2],
int);
3071static
inline utimensat_func *
3078# define utimensat rb_utimensat()
3085utime_internal(
const char *path,
void *arg)
3088 const struct timespec *tsp = v->tsp;
3089 struct timeval tvbuf[2], *tvp = NULL;
3091#if defined(HAVE_UTIMENSAT)
3092# if defined(__APPLE__)
3093 const int try_utimensat = utimensat != NULL;
3094 const int try_utimensat_follow = utimensat != NULL;
3096# define TRY_UTIMENSAT 1
3097 static int try_utimensat = 1;
3098# ifdef AT_SYMLINK_NOFOLLOW
3099 static int try_utimensat_follow = 1;
3101 const int try_utimensat_follow = 0;
3106 if (v->follow ? try_utimensat_follow : try_utimensat) {
3107# ifdef AT_SYMLINK_NOFOLLOW
3109 flags = AT_SYMLINK_NOFOLLOW;
3113 int result = utimensat(AT_FDCWD, path, tsp, flags);
3114# ifdef TRY_UTIMENSAT
3115 if (result < 0 &&
errno == ENOSYS) {
3116# ifdef AT_SYMLINK_NOFOLLOW
3117 try_utimensat_follow = 0;
3129 tvbuf[0].tv_sec = tsp[0].tv_sec;
3130 tvbuf[0].tv_usec = (int)(tsp[0].tv_nsec / 1000);
3131 tvbuf[1].tv_sec = tsp[1].tv_sec;
3132 tvbuf[1].tv_usec = (int)(tsp[1].tv_nsec / 1000);
3136 if (v->follow)
return lutimes(path, tvp);
3138 return utimes(path, tvp);
3143#if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
3151utime_internal(
const char *path,
void *arg)
3155 struct utimbuf utbuf, *utp = NULL;
3157 utbuf.actime = tsp[0].tv_sec;
3158 utbuf.modtime = tsp[1].tv_sec;
3161 return utime(path, utp);
3166utime_internal_i(
int argc,
VALUE *argv,
int follow)
3169 struct timespec tss[2], *tsp = NULL;
3172 args.atime = *argv++;
3173 args.mtime = *argv++;
3175 args.follow = follow;
3177 if (!
NIL_P(args.atime) || !
NIL_P(args.mtime)) {
3180 if (args.atime == args.mtime)
3187 return apply2files(utime_internal, argc, argv, &args);
3204 return utime_internal_i(argc, argv, FALSE);
3207#if defined(HAVE_UTIMES) && (defined(HAVE_LUTIMES) || (defined(HAVE_UTIMENSAT) && defined(AT_SYMLINK_NOFOLLOW)))
3223 return utime_internal_i(argc, argv, TRUE);
3226#define rb_file_s_lutime rb_f_notimplement
3229#ifdef RUBY_FUNCTION_NAME_STRING
3230# define syserr_fail2(e, s1, s2) syserr_fail2_in(RUBY_FUNCTION_NAME_STRING, e, s1, s2)
3232# define syserr_fail2_in(func, e, s1, s2) syserr_fail2(e, s1, s2)
3234#define sys_fail2(s1, s2) syserr_fail2(errno, s1, s2)
3235NORETURN(
static void syserr_fail2_in(
const char *,
int,
VALUE,
VALUE));
3237syserr_fail2_in(
const char *func,
int e,
VALUE s1,
VALUE s2)
3241 const int max_pathlen = MAX_PATH;
3243 const int max_pathlen = MAXPATHLEN;
3254#ifdef RUBY_FUNCTION_NAME_STRING
3255 rb_syserr_fail_path_in(func, e, str);
3257 rb_syserr_fail_path(e, str);
3279 from = rb_str_encode_ospath(from);
3280 to = rb_str_encode_ospath(to);
3283 sys_fail2(from, to);
3288#define rb_file_s_link rb_f_notimplement
3309 from = rb_str_encode_ospath(from);
3310 to = rb_str_encode_ospath(to);
3313 sys_fail2(from, to);
3318#define rb_file_s_symlink rb_f_notimplement
3336 return rb_readlink(path, rb_filesystem_encoding());
3339struct readlink_arg {
3346nogvl_readlink(
void *ptr)
3348 struct readlink_arg *ra = ptr;
3350 return (
void *)(
VALUE)readlink(ra->path, ra->buf, ra->size);
3354readlink_without_gvl(
VALUE path,
VALUE buf,
size_t size)
3356 struct readlink_arg ra;
3358 ra.path = RSTRING_PTR(path);
3359 ra.buf = RSTRING_PTR(buf);
3362 return (ssize_t)IO_WITHOUT_GVL(nogvl_readlink, &ra);
3373 path = rb_str_encode_ospath(path);
3374 v = rb_enc_str_new(0, size, enc);
3375 while ((rv = readlink_without_gvl(path, v, size)) == size
3377 || (rv < 0 &&
errno == ERANGE)
3386 rb_str_resize(v, 0);
3387 rb_syserr_fail_path(e, path);
3389 rb_str_resize(v, rv);
3394#define rb_file_s_readlink rb_f_notimplement
3398unlink_internal(
const char *path,
void *arg)
3400 return unlink(path);
3420rb_file_s_unlink(
int argc,
VALUE *argv,
VALUE klass)
3422 return apply2files(unlink_internal, argc, argv, 0);
3431no_gvl_rename(
void *ptr)
3435 return (
void *)(
VALUE)rename(ra->src, ra->dst);
3456 f = rb_str_encode_ospath(from);
3457 t = rb_str_encode_ospath(to);
3460#if defined __CYGWIN__
3463 if (IO_WITHOUT_GVL_INT(no_gvl_rename, &ra) < 0) {
3468 if (chmod(ra.dst, 0666) == 0 &&
3469 unlink(ra.dst) == 0 &&
3470 rename(ra.src, ra.dst) == 0)
3474 syserr_fail2(e, from, to);
3509 rb_error_arity(argc, 0, 1);
3517#if defined __CYGWIN__ || defined DOSISH
3519#define DOSISH_DRIVE_LETTER
3520#define FILE_ALT_SEPARATOR '\\'
3522#ifdef FILE_ALT_SEPARATOR
3523#define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
3525static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
3528#define isdirsep(x) ((x) == '/')
3541# define USE_NTFS_ADS 1
3543# define USE_NTFS_ADS 0
3548#define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
3550#define istrailinggarbage(x) 0
3554# define isADS(x) ((x) == ':')
3559#define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc)))
3560#define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
3562#if defined(DOSISH_UNC)
3563#define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
3565#define has_unc(buf) 0
3568#ifdef DOSISH_DRIVE_LETTER
3570has_drive_letter(
const char *buf)
3572 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
3585 char *drvcwd, *oldcwd;
3596 if (chdir(drive) == 0) {
3609not_same_drive(
VALUE path,
int drive)
3611 const char *p = RSTRING_PTR(path);
3612 if (RSTRING_LEN(path) < 2)
return 0;
3613 if (has_drive_letter(p)) {
3624skiproot(
const char *path,
const char *end,
rb_encoding *enc)
3626#ifdef DOSISH_DRIVE_LETTER
3627 if (path + 2 <= end && has_drive_letter(path)) path += 2;
3629 while (path < end && isdirsep(*path)) path++;
3630 return (
char *)path;
3633#define nextdirsep rb_enc_path_next
3637 while (s < e && !isdirsep(*s)) {
3643#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3644#define skipprefix rb_enc_path_skip_prefix
3646#define skipprefix(path, end, enc) (path)
3649rb_enc_path_skip_prefix(
const char *path,
const char *end,
rb_encoding *enc)
3651#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3653 if (path + 2 <= end && isdirsep(path[0]) && isdirsep(path[1])) {
3655 while (path < end && isdirsep(*path)) path++;
3656 if ((path = rb_enc_path_next(path, end, enc)) < end && path[0] && path[1] && !isdirsep(path[1]))
3657 path = rb_enc_path_next(path + 1, end, enc);
3658 return (
char *)path;
3661#ifdef DOSISH_DRIVE_LETTER
3662 if (has_drive_letter(path))
3663 return (
char *)(path + 2);
3666 return (
char *)path;
3670skipprefixroot(
const char *path,
const char *end,
rb_encoding *enc)
3672#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
3673 char *p = skipprefix(path, end, enc);
3674 while (isdirsep(*p)) p++;
3677 return skiproot(path, end, enc);
3681#define strrdirsep rb_enc_path_last_separator
3683rb_enc_path_last_separator(
const char *path,
const char *end,
rb_encoding *enc)
3686 while (path < end) {
3687 if (isdirsep(*path)) {
3688 const char *tmp = path++;
3689 while (path < end && isdirsep(*path)) path++;
3690 if (path >= end)
break;
3694 Inc(path, end, enc);
3701chompdirsep(
const char *path,
const char *end,
rb_encoding *enc)
3703 while (path < end) {
3704 if (isdirsep(*path)) {
3705 const char *last = path++;
3706 while (path < end && isdirsep(*path)) path++;
3707 if (path >= end)
return (
char *)last;
3710 Inc(path, end, enc);
3713 return (
char *)path;
3719 if (path < end && isdirsep(*path)) path++;
3720 return chompdirsep(path, end, enc);
3727 int encidx = rb_enc_to_index(enc);
3728 if (encidx == ENCINDEX_US_ASCII) {
3729 encidx = rb_enc_get_index(path1);
3730 if (encidx == ENCINDEX_US_ASCII)
3731 encidx = rb_enc_get_index(path2);
3732 enc = rb_enc_from_index(encidx);
3739ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
3741 while (path < end && *path ==
'.') path++;
3742 while (path < end && !isADS(*path)) {
3743 if (istrailinggarbage(*path)) {
3744 const char *last = path++;
3745 while (path < end && istrailinggarbage(*path)) path++;
3746 if (path >= end || isADS(*path))
return (
char *)last;
3748 else if (isdirsep(*path)) {
3749 const char *last = path++;
3750 while (path < end && isdirsep(*path)) path++;
3751 if (path >= end)
return (
char *)last;
3752 if (isADS(*path)) path++;
3755 Inc(path, end, enc);
3758 return (
char *)path;
3762#define BUFCHECK(cond) do {\
3765 do {buflen *= 2;} while (cond);\
3766 rb_str_resize(result, buflen);\
3767 buf = RSTRING_PTR(result);\
3769 pend = buf + buflen;\
3774 p = buf = RSTRING_PTR(result),\
3775 buflen = RSTRING_LEN(result),\
3779# define SKIPPATHSEP(p) ((*(p)) ? 1 : 0)
3781# define SKIPPATHSEP(p) 1
3784#define BUFCOPY(srcptr, srclen) do { \
3785 const int skip = SKIPPATHSEP(p); \
3786 rb_str_set_len(result, p-buf+skip); \
3787 BUFCHECK(bdiff + ((srclen)+skip) >= buflen); \
3789 memcpy(p, (srcptr), (srclen)); \
3793#define WITH_ROOTDIFF(stmt) do { \
3794 long rootdiff = root - buf; \
3796 root = buf + rootdiff; \
3800copy_home_path(
VALUE result,
const char *dir)
3803#if defined DOSISH || defined __CYGWIN__
3810 dirlen = strlen(dir);
3811 rb_str_resize(result, dirlen);
3812 memcpy(buf = RSTRING_PTR(result), dir, dirlen);
3813 encidx = rb_filesystem_encindex();
3814 rb_enc_associate_index(result, encidx);
3815#if defined DOSISH || defined __CYGWIN__
3816 enc = rb_enc_from_index(encidx);
3817 for (bend = (p = buf) + dirlen; p < bend; Inc(p, bend, enc)) {
3830 VALUE dirname = rb_getpwdirnam_for_login(user);
3831 if (dirname ==
Qnil) {
3832 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3834 const char *dir = RSTRING_PTR(dirname);
3836 extern char *getlogin(
void);
3837 const char *pwPtr = 0;
3839 # define endpwent() ((void)0)
3840 const char *dir, *username = RSTRING_PTR(user);
3851 if ((login = getlogin()) && strcasecmp(username, login) == 0)
3852 dir = pwPtr = getenv(
"HOME");
3854 rb_raise(rb_eArgError,
"user %"PRIsVALUE
" doesn't exist", user);
3857 copy_home_path(result, dir);
3863rb_default_home_dir(
VALUE result)
3865 const char *dir = getenv(
"HOME");
3867#if defined HAVE_PWD_H
3881 VALUE login_name = rb_getlogin();
3883# if !defined(HAVE_GETPWUID_R) && !defined(HAVE_GETPWUID)
3888 if (
NIL_P(login_name)) {
3889 rb_raise(rb_eArgError,
"couldn't find login name -- expanding '~'");
3893 VALUE pw_dir = rb_getpwdirnam_for_login(login_name);
3894 if (
NIL_P(pw_dir)) {
3895 pw_dir = rb_getpwdiruid();
3896 if (
NIL_P(pw_dir)) {
3897 rb_raise(rb_eArgError,
"couldn't find home for uid '%ld'", (
long)getuid());
3902 copy_home_path(result, RSTRING_PTR(pw_dir));
3903 rb_str_resize(pw_dir, 0);
3908 rb_raise(rb_eArgError,
"couldn't find HOME environment -- expanding '~'");
3910 return copy_home_path(result, dir);
3916#if NORMALIZE_UTF8PATH
3917 VALUE path = rb_str_normalize_ospath(ptr,
len);
3918 rb_enc_associate(path, fsenc);
3921 return rb_enc_str_new(ptr,
len, fsenc);
3928 char *buf, *cwdp = dir;
3932 if (NORMALIZE_UTF8PATH || *enc != fsenc) {
3933 dirname = ospath_new(dir, dirlen, fsenc);
3934 if (!rb_enc_compatible(fname, dirname)) {
3938 rb_enc_check(fname, dirname);
3939 rb_bug(
"unreachable");
3941 rb_encoding *direnc = fs_enc_check(fname, dirname);
3942 if (direnc != fsenc) {
3946 else if (NORMALIZE_UTF8PATH) {
3951 do {buflen *= 2;}
while (dirlen > buflen);
3952 rb_str_resize(result, buflen);
3953 buf = RSTRING_PTR(result);
3954 memcpy(buf, cwdp, dirlen);
3956 if (!
NIL_P(dirname)) rb_str_resize(dirname, 0);
3957 rb_enc_associate(result, *enc);
3958 return buf + dirlen;
3962rb_file_expand_path_internal(
VALUE fname,
VALUE dname,
int abs_mode,
int long_name,
VALUE result)
3964 const char *s, *b, *fend;
3965 char *buf, *p, *pend, *root;
3966 size_t buflen, bdiff;
3967 rb_encoding *enc, *fsenc = rb_filesystem_encoding();
3970 fend = s + RSTRING_LEN(fname);
3971 enc = rb_enc_get(fname);
3974 if (s[0] ==
'~' && abs_mode == 0) {
3976 if (isdirsep(s[1]) || s[1] ==
'\0') {
3981 rb_default_home_dir(result);
3984 s = nextdirsep(b = s, fend, enc);
3987 BUFCHECK(bdiff + userlen >= buflen);
3988 memcpy(p, b, userlen);
3991 rb_enc_associate(result, enc);
3992 rb_home_dir_of(result, result);
3996 if (!rb_is_absolute_path(RSTRING_PTR(result))) {
3998 rb_enc_raise(enc, rb_eArgError,
"non-absolute home of %.*s%.0"PRIsVALUE,
3999 (
int)userlen, b, fname);
4002 rb_raise(rb_eArgError,
"non-absolute home");
4008#ifdef DOSISH_DRIVE_LETTER
4010 else if (has_drive_letter(s)) {
4011 if (isdirsep(s[2])) {
4014 BUFCHECK(bdiff + 2 >= buflen);
4018 rb_enc_copy(result, fname);
4023 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
4024 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4032 char *e = append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
4037 rb_enc_associate(result, enc = fs_enc_check(result, fname));
4040 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4045 else if (!rb_is_absolute_path(s)) {
4046 if (!
NIL_P(dname)) {
4047 rb_file_expand_path_internal(dname,
Qnil, abs_mode, long_name, result);
4048 rb_enc_associate(result, fs_enc_check(result, fname));
4053 char *e = append_fspath(result, fname,
ruby_getcwd(), &enc, fsenc);
4057#if defined DOSISH || defined __CYGWIN__
4061 p = skipprefix(buf, p, enc);
4065 p = chompdirsep(skiproot(buf, p, enc), p, enc);
4070 do s++;
while (isdirsep(*s));
4073 BUFCHECK(bdiff >= buflen);
4074 memset(buf,
'/',
len);
4076 rb_enc_associate(result, fs_enc_check(result, fname));
4078 if (p > buf && p[-1] ==
'/')
4082 BUFCHECK(bdiff + 1 >= buflen);
4087 BUFCHECK(bdiff + 1 >= buflen);
4089 root = skipprefix(buf, p+1, enc);
4101 if (*(s+1) ==
'\0' || isdirsep(*(s+1))) {
4105 if (!(n = strrdirsep(root, p, enc))) {
4115 do ++s;
while (istrailinggarbage(*s));
4120#if defined DOSISH || defined __CYGWIN__
4135 while (s < fend && istrailinggarbage(*s)) s++;
4145#if defined DOSISH || defined __CYGWIN__
4149 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4157 int n = ignored_char_p(s, fend, enc);
4160 WITH_ROOTDIFF(BUFCOPY(b, s-b));
4176 static const char prime[] =
":$DATA";
4177 enum {prime_len =
sizeof(prime) -1};
4181 if (s > b + prime_len && strncasecmp(s - prime_len, prime, prime_len) == 0) {
4184 if (isADS(*(s - (prime_len+1)))) {
4187 else if (memchr(b,
':', s - prime_len - b)) {
4196 if (p == skiproot(buf, p + !!*p, enc) - 1) p++;
4200 if ((s = strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
4205 WIN32_FIND_DATAW wfd;
4208#ifdef HAVE_CYGWIN_CONV_PATH
4209 char *w32buf = NULL;
4210 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
4212 char w32buf[MAXPATHLEN];
4216 int lnk_added = 0, is_symlink = 0;
4220 if (lstat_without_gvl(buf, &st) == 0 && S_ISLNK(st.st_mode)) {
4226 path = *buf ? buf :
"/";
4227#ifdef HAVE_CYGWIN_CONV_PATH
4228 bufsize = cygwin_conv_path(flags, path, NULL, 0);
4231 if (lnk_added) bufsize += 4;
4233 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
4238 bufsize = MAXPATHLEN;
4239 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
4243 if (is_symlink && b == w32buf) {
4245 strlcat(w32buf, p, bufsize);
4247 strlcat(w32buf,
".lnk", bufsize);
4258 if (encidx != ENCINDEX_UTF_8 && !is_ascii_string(result)) {
4259 tmp = rb_str_encode_ospath(result);
4261 len = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, NULL, 0);
4263 MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(tmp), -1, wstr,
len);
4265 h = FindFirstFileW(wstr, &wfd);
4267 if (h != INVALID_HANDLE_VALUE) {
4270 len = lstrlenW(wfd.cFileName);
4272 if (lnk_added &&
len > 4 &&
4273 wcscasecmp(wfd.cFileName +
len - 4, L
".lnk") == 0) {
4274 wfd.cFileName[
len -= 4] = L
'\0';
4281 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, NULL, 0, NULL, NULL);
4282 if (tmp == result) {
4283 BUFCHECK(bdiff +
len >= buflen);
4284 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p,
len + 1, NULL, NULL);
4288 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, RSTRING_PTR(tmp),
len + 1, NULL, NULL);
4289 rb_str_cat_conv_enc_opts(result, bdiff, RSTRING_PTR(tmp),
len,
4290 rb_utf8_encoding(), 0,
Qnil);
4292 rb_str_resize(tmp, 0);
4305 rb_enc_check(fname, result);
4311#define EXPAND_PATH_BUFFER() rb_usascii_str_new(0, 1)
4314str_shrink(
VALUE str)
4316 rb_str_resize(str, RSTRING_LEN(str));
4320#define expand_path(fname, dname, abs_mode, long_name, result) \
4321 str_shrink(rb_file_expand_path_internal(fname, dname, abs_mode, long_name, result))
4323#define check_expand_path_args(fname, dname) \
4324 (((fname) = rb_get_path(fname)), \
4325 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
4328file_expand_path_1(
VALUE fname)
4330 return rb_file_expand_path_internal(fname,
Qnil, 0, 0, EXPAND_PATH_BUFFER());
4336 check_expand_path_args(fname, dname);
4337 return expand_path(fname, dname, 0, 1, EXPAND_PATH_BUFFER());
4341rb_file_expand_path_fast(
VALUE fname,
VALUE dname)
4343 return expand_path(fname, dname, 0, 0, EXPAND_PATH_BUFFER());
4347rb_file_s_expand_path(
int argc,
const VALUE *argv)
4350 return rb_file_expand_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4384 return rb_file_s_expand_path(c, v);
4390 check_expand_path_args(fname, dname);
4391 return expand_path(fname, dname, 1, 1, EXPAND_PATH_BUFFER());
4395rb_file_s_absolute_path(
int argc,
const VALUE *argv)
4398 return rb_file_absolute_path(argv[0], argc > 1 ? argv[1] :
Qnil);
4417 return rb_file_s_absolute_path(c, v);
4433 VALUE path = rb_get_path(fname);
4435 if (!rb_is_absolute_path(RSTRING_PTR(path)))
return Qfalse;
4439enum rb_realpath_mode {
4443 RB_REALPATH_MODE_MAX
4447realpath_rec(
long *prefixlenp,
VALUE *resolvedp,
const char *unresolved,
VALUE fallback,
4448 VALUE loopcheck,
enum rb_realpath_mode mode,
int last)
4450 const char *pend = unresolved + strlen(unresolved);
4454 while (unresolved < pend) {
4455 const char *testname = unresolved;
4456 const char *unresolved_firstsep = rb_enc_path_next(unresolved, pend, enc);
4457 long testnamelen = unresolved_firstsep - unresolved;
4458 const char *unresolved_nextname = unresolved_firstsep;
4459 while (unresolved_nextname < pend && isdirsep(*unresolved_nextname))
4460 unresolved_nextname++;
4461 unresolved = unresolved_nextname;
4462 if (testnamelen == 1 && testname[0] ==
'.') {
4464 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
4465 if (*prefixlenp < RSTRING_LEN(*resolvedp)) {
4466 const char *resolved_str = RSTRING_PTR(*resolvedp);
4467 const char *resolved_names = resolved_str + *prefixlenp;
4468 const char *lastsep = strrdirsep(resolved_names, resolved_str + RSTRING_LEN(*resolvedp), enc);
4469 long len = lastsep ? lastsep - resolved_names : 0;
4470 rb_str_resize(*resolvedp, *prefixlenp +
len);
4476 if (*prefixlenp < RSTRING_LEN(testpath))
4478#if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
4479 if (*prefixlenp > 1 && *prefixlenp == RSTRING_LEN(testpath)) {
4480 const char *prefix = RSTRING_PTR(testpath);
4481 const char *last =
rb_enc_left_char_head(prefix, prefix + *prefixlenp - 1, prefix + *prefixlenp, enc);
4486 checkval = rb_hash_aref(loopcheck, testpath);
4487 if (!
NIL_P(checkval)) {
4488 if (checkval ==
ID2SYM(resolving)) {
4489 if (mode == RB_REALPATH_CHECK) {
4493 rb_syserr_fail_path(ELOOP, testpath);
4502 ret = lstat_without_gvl(RSTRING_PTR(testpath), &sbuf);
4505 if (e == ENOENT && !
NIL_P(fallback)) {
4506 if (stat_without_gvl(RSTRING_PTR(fallback), &sbuf) == 0) {
4511 if (mode == RB_REALPATH_CHECK)
return -1;
4513 if (mode == RB_REALPATH_STRICT || !last || *unresolved_firstsep)
4514 rb_syserr_fail_path(e, testpath);
4515 *resolvedp = testpath;
4519 rb_syserr_fail_path(e, testpath);
4523 if (S_ISLNK(sbuf.st_mode)) {
4526 const char *link_prefix, *link_names;
4527 long link_prefixlen;
4528 rb_hash_aset(loopcheck, testpath,
ID2SYM(resolving));
4529 link = rb_readlink(testpath, enc);
4530 link_prefix = RSTRING_PTR(link);
4531 link_names = skipprefixroot(link_prefix, link_prefix + RSTRING_LEN(link), rb_enc_get(link));
4532 link_prefixlen = link_names - link_prefix;
4533 if (link_prefixlen > 0) {
4537 tmpenc = fs_enc_check(*resolvedp, link);
4540 *prefixlenp = link_prefixlen;
4542 if (realpath_rec(prefixlenp, resolvedp, link_names, testpath,
4543 loopcheck, mode, !*unresolved_firstsep))
4552 rb_hash_aset(loopcheck, s, s);
4553 *resolvedp = testpath;
4562rb_check_realpath_emulate(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4566 VALUE unresolved_path;
4571 char *path_names = NULL, *basedir_names = NULL, *curdir_names = NULL;
4572 char *ptr, *prefixptr = NULL, *pend;
4577 if (!
NIL_P(basedir)) {
4582 enc = rb_enc_get(unresolved_path);
4583 unresolved_path = TO_OSPATH(unresolved_path);
4585 path_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(unresolved_path));
4586 if (ptr != path_names) {
4587 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
4591 if (!
NIL_P(basedir)) {
4593 basedir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(basedir));
4594 if (ptr != basedir_names) {
4600 curdir = rb_dir_getwd_ospath();
4602 curdir_names = skipprefixroot(ptr, ptr +
len, rb_enc_get(curdir));
4607 pend = prefixptr + prefixlen;
4608 ptr = chompdirsep(prefixptr, pend, enc);
4610 prefixlen = ++ptr - prefixptr;
4613#ifdef FILE_ALT_SEPARATOR
4614 while (prefixptr < ptr) {
4615 if (*prefixptr == FILE_ALT_SEPARATOR) {
4618 Inc(prefixptr, pend, enc);
4622 switch (rb_enc_to_index(enc)) {
4623 case ENCINDEX_ASCII_8BIT:
4624 case ENCINDEX_US_ASCII:
4625 rb_enc_associate_index(resolved, rb_filesystem_encindex());
4628 loopcheck = rb_hash_new();
4630 if (realpath_rec(&prefixlen, &resolved, curdir_names,
Qnil, loopcheck, mode, 0))
4633 if (basedir_names) {
4634 if (realpath_rec(&prefixlen, &resolved, basedir_names,
Qnil, loopcheck, mode, 0))
4637 if (realpath_rec(&prefixlen, &resolved, path_names,
Qnil, loopcheck, mode, 1))
4640 if (origenc && origenc != rb_enc_get(resolved)) {
4642 rb_enc_associate(resolved, origenc);
4656#ifndef HAVE_REALPATH
4658rb_check_realpath_emulate_try(
VALUE arg)
4661 return rb_check_realpath_emulate(args[0], args[1], (
rb_encoding *)args[2], RB_REALPATH_CHECK);
4665rb_check_realpath_emulate_rescue(
VALUE arg,
VALUE exc)
4669#elif !defined(NEEDS_REALPATH_BUFFER) && defined(__APPLE__) && \
4670 (!defined(MAC_OS_X_VERSION_10_6) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6))
4672# include <sys/syslimits.h>
4673# define NEEDS_REALPATH_BUFFER 1
4677rb_check_realpath_internal(
VALUE basedir,
VALUE path,
rb_encoding *origenc,
enum rb_realpath_mode mode)
4680 VALUE unresolved_path;
4681 char *resolved_ptr = NULL;
4683# if defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER
4684 char resolved_buffer[PATH_MAX];
4686 char *
const resolved_buffer = NULL;
4689 if (mode == RB_REALPATH_DIR) {
4690 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4694 if (*RSTRING_PTR(unresolved_path) !=
'/' && !
NIL_P(basedir)) {
4695 unresolved_path = rb_file_join(
rb_assoc_new(basedir, unresolved_path));
4697 if (origenc) unresolved_path = TO_OSPATH(unresolved_path);
4699 if ((resolved_ptr = realpath(RSTRING_PTR(unresolved_path), resolved_buffer)) == NULL) {
4708 if (
errno == ENOTSUP ||
4710 (
errno == ENOENT && rb_file_exist_p(0, unresolved_path))) {
4711 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4714 if (mode == RB_REALPATH_CHECK) {
4717 rb_sys_fail_path(unresolved_path);
4719 resolved = ospath_new(resolved_ptr, strlen(resolved_ptr), rb_filesystem_encoding());
4720# if !(defined(NEEDS_REALPATH_BUFFER) && NEEDS_REALPATH_BUFFER)
4724# if !defined(__linux__) && !defined(__APPLE__)
4728 if (stat_without_gvl(RSTRING_PTR(resolved), &st) < 0) {
4729 if (mode == RB_REALPATH_CHECK) {
4732 rb_sys_fail_path(unresolved_path);
4736 if (origenc && origenc != rb_enc_get(resolved)) {
4740 rb_enc_associate(resolved, origenc);
4743 if (is_broken_string(resolved)) {
4744 rb_enc_associate(resolved, rb_filesystem_encoding());
4745 if (is_broken_string(resolved)) {
4746 rb_enc_associate(resolved, rb_ascii8bit_encoding());
4753 if (mode == RB_REALPATH_CHECK) {
4757 arg[2] = (
VALUE)origenc;
4760 rb_check_realpath_emulate_rescue,
Qnil);
4763 return rb_check_realpath_emulate(basedir, path, origenc, mode);
4769rb_realpath_internal(
VALUE basedir,
VALUE path,
int strict)
4771 const enum rb_realpath_mode mode =
4772 strict ? RB_REALPATH_STRICT : RB_REALPATH_DIR;
4773 return rb_check_realpath_internal(basedir, path, rb_enc_get(path), mode);
4779 return rb_check_realpath_internal(basedir, path, enc, RB_REALPATH_CHECK);
4796rb_file_s_realpath(
int argc,
VALUE *argv,
VALUE klass)
4799 VALUE path = argv[0];
4801 return rb_realpath_internal(basedir, path, 1);
4817rb_file_s_realdirpath(
int argc,
VALUE *argv,
VALUE klass)
4820 VALUE path = argv[0];
4822 return rb_realpath_internal(basedir, path, 0);
4826rmext(
const char *p,
long l0,
long l1,
const char *e,
long l2,
rb_encoding *enc)
4830 const char *s, *last;
4832 if (!e || !l2)
return 0;
4834 c = rb_enc_codepoint_len(e, e + l2, &len1, enc);
4835 if (rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
4836 if (c ==
'.')
return l0;
4841 if (rb_enc_codepoint_len(s, e, &len1, enc) == c) last = s;
4846 if (l1 < l2)
return l1;
4849 if (!at_char_boundary(p, s, p+l1, enc))
return 0;
4850#if CASEFOLD_FILESYSTEM
4851#define fncomp strncasecmp
4853#define fncomp strncmp
4855 if (fncomp(s, e, l2) == 0) {
4862ruby_enc_find_basename(
const char *name,
long *baselen,
long *alllen,
rb_encoding *enc)
4864 const char *p, *q, *e, *end;
4865#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4870 end = name + (alllen ? (size_t)*alllen : strlen(name));
4871 name = skipprefix(name, end, enc);
4872#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4875 while (isdirsep(*name))
4880#if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
4884#ifdef DOSISH_DRIVE_LETTER
4885 else if (*p ==
':') {
4898 if (!(p = strrdirsep(name, end, enc))) {
4902 while (isdirsep(*p)) p++;
4905 n = ntfs_tail(p, end, enc) - p;
4907 n = chompdirsep(p, end, enc) - p;
4909 for (q = p; q - p < n && *q ==
'.'; q++);
4910 for (e = 0; q - p < n; Inc(q, end, enc)) {
4911 if (*q ==
'.') e = q;
4944 VALUE fname, fext, basename;
4945 const char *name, *p;
4953 enc = check_path_encoding(fext);
4957 if (
NIL_P(fext) || !(enc = rb_enc_compatible(fname, fext))) {
4958 enc = rb_enc_get(fname);
4961 if ((n = RSTRING_LEN(fname)) == 0 || !*(name = RSTRING_PTR(fname)))
4964 p = ruby_enc_find_basename(name, &f, &n, enc);
4972 if (!(f = rmext(p, f, n, fp, RSTRING_LEN(fext), enc))) {
4981 rb_enc_copy(basename, fname);
4985static VALUE rb_file_dirname_n(
VALUE fname,
int n);
5007rb_file_s_dirname(
int argc,
VALUE *argv,
VALUE klass)
5013 return rb_file_dirname_n(argv[0], n);
5019 return rb_file_dirname_n(fname, 1);
5023rb_file_dirname_n(
VALUE fname,
int n)
5025 const char *name, *root, *p, *end;
5031 if (n < 0) rb_raise(rb_eArgError,
"negative level: %d", n);
5034 end = name + RSTRING_LEN(fname);
5035 enc = rb_enc_get(fname);
5036 root = skiproot(name, end, enc);
5038 if (root > name + 1 && isdirsep(*name))
5039 root = skipprefix(name = root - 2, end, enc);
5041 if (root > name + 1)
5044 if (n > (end - root + 1) / 2) {
5054 if (!(p = strrdirsep(root, end, enc))) p = root;
5057 seps =
ALLOCV_N(
const char *, sepsv, n);
5058 for (i = 0; i < n; ++i) seps[i] = root;
5060 for (p = root; p < end; ) {
5062 const char *tmp = p++;
5063 while (p < end && isdirsep(*p)) p++;
5064 if (p >= end)
break;
5079 rb_enc_copy(dirname, fname);
5082#ifdef DOSISH_DRIVE_LETTER
5083 if (has_drive_letter(name) && isdirsep(*(name + 2))) {
5084 const char *top = skiproot(name + 2, end, enc);
5091#ifdef DOSISH_DRIVE_LETTER
5092 if (has_drive_letter(name) && root == name + 2 && p - name == 2)
5095 rb_enc_copy(dirname, fname);
5114 const char *p, *e, *end = name + (
len ? *
len : (long)strlen(name));
5116 p = strrdirsep(name, end, enc);
5120 do name = ++p;
while (isdirsep(*p));
5123 while (*p && *p ==
'.') p++;
5125 if (*p ==
'.' || istrailinggarbage(*p)) {
5127 const char *last = p++, *dot = last;
5128 while (istrailinggarbage(*p)) {
5129 if (*p ==
'.') dot = p;
5132 if (!*p || isADS(*p)) {
5136 if (*last ==
'.' || dot > last) e = dot;
5143 else if (isADS(*p)) {
5147 else if (isdirsep(*p))
5154 if (!e || e == name)
5193 const char *name, *e;
5199 len = RSTRING_LEN(fname);
5200 e = ruby_enc_find_extname(name, &
len, rb_enc_get(fname));
5237 return rb_get_path(fname);
5259file_inspect_join(
VALUE ary,
VALUE arg,
int recur)
5261 if (recur || ary == arg) rb_raise(rb_eArgError,
"recursive array");
5262 return rb_file_join(arg);
5266rb_file_join(
VALUE ary)
5270 const char *name, *tail;
5280 check_path_encoding(tmp);
5281 len += RSTRING_LEN(tmp);
5289 RBASIC_CLEAR_CLASS(result);
5292 switch (OBJ_BUILTIN_TYPE(tmp)) {
5294 if (!checked) check_path_encoding(tmp);
5299 rb_raise(rb_eArgError,
"recursive array");
5311 rb_enc_copy(result, tmp);
5314 tail = chompdirsep(name, name +
len, rb_enc_get(result));
5315 if (RSTRING_PTR(tmp) && isdirsep(RSTRING_PTR(tmp)[0])) {
5322 enc = fs_enc_check(result, tmp);
5324 rb_enc_associate(result, enc);
5345 return rb_file_join(args);
5348#if defined(HAVE_TRUNCATE)
5349struct truncate_arg {
5355nogvl_truncate(
void *ptr)
5357 struct truncate_arg *ta = ptr;
5358 return (
void *)(
VALUE)truncate(ta->path, ta->pos);
5379 struct truncate_arg ta;
5384 path = rb_str_encode_ospath(path);
5387 r = IO_WITHOUT_GVL_INT(nogvl_truncate, &ta);
5389 rb_sys_fail_path(path);
5393#define rb_file_s_truncate rb_f_notimplement
5396#if defined(HAVE_FTRUNCATE)
5397struct ftruncate_arg {
5403nogvl_ftruncate(
void *ptr)
5405 struct ftruncate_arg *fa = ptr;
5407 return (
VALUE)ftruncate(fa->fd, fa->pos);
5428 struct ftruncate_arg fa;
5435 rb_io_flush_raw(obj, 0);
5437 if ((
int)rb_io_blocking_region(fptr, nogvl_ftruncate, &fa) < 0) {
5438 rb_sys_fail_path(fptr->
pathv);
5443#define rb_file_truncate rb_f_notimplement
5460#include <winerror.h>
5464rb_thread_flock(
void *data)
5467 int old_errno =
errno;
5469 int *op = data, ret = flock(op[0], op[1]);
5472 if (GetLastError() == ERROR_NOT_LOCKED) {
5530 op[1] = op1 =
NUM2INT(operation);
5535 rb_io_flush_raw(obj, 0);
5537 while ((
int)rb_io_blocking_region(fptr, rb_thread_flock, op) < 0) {
5542#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
5545 if (op1 & LOCK_NB)
return Qfalse;
5548 time.tv_usec = 100 * 1000;
5554#if defined(ERESTART)
5560 rb_syserr_fail_path(e, fptr->
pathv);
5567test_check(
int n,
int argc,
VALUE *argv)
5573 for (i=1; i<n; i++) {
5580#define CHECK(n) test_check((n), argc, argv)
5672 if (strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
5676 return rb_file_blockdev_p(0, argv[1]);
5679 return rb_file_chardev_p(0, argv[1]);
5682 return rb_file_directory_p(0, argv[1]);
5685 return rb_file_exist_p(0, argv[1]);
5688 return rb_file_file_p(0, argv[1]);
5691 return rb_file_sgid_p(0, argv[1]);
5694 return rb_file_grpowned_p(0, argv[1]);
5697 return rb_file_sticky_p(0, argv[1]);
5700 return rb_file_symlink_p(0, argv[1]);
5703 return rb_file_owned_p(0, argv[1]);
5706 return rb_file_rowned_p(0, argv[1]);
5709 return rb_file_pipe_p(0, argv[1]);
5712 return rb_file_readable_p(0, argv[1]);
5715 return rb_file_readable_real_p(0, argv[1]);
5718 return rb_file_size_p(0, argv[1]);
5721 return rb_file_socket_p(0, argv[1]);
5724 return rb_file_suid_p(0, argv[1]);
5727 return rb_file_writable_p(0, argv[1]);
5730 return rb_file_writable_real_p(0, argv[1]);
5733 return rb_file_executable_p(0, argv[1]);
5736 return rb_file_executable_real_p(0, argv[1]);
5739 return rb_file_zero_p(0, argv[1]);
5743 if (strchr(
"MAC", cmd)) {
5745 VALUE fname = argv[1];
5748 if (
rb_stat(fname, &st) == -1) {
5751 rb_syserr_fail_path(e, fname);
5756 return stat_atime(&st);
5758 return stat_mtime(&st);
5760 return stat_ctime(&st);
5766 return rb_file_identical_p(0, argv[1], argv[2]);
5769 if (strchr(
"=<>", cmd)) {
5770 struct stat st1, st2;
5777 t1 = stat_mtimespec(&st1);
5778 t2 = stat_mtimespec(&st2);
5782 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec == t2.tv_nsec)
return Qtrue;
5786 if (t1.tv_sec > t2.tv_sec)
return Qtrue;
5787 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec > t2.tv_nsec)
return Qtrue;
5791 if (t1.tv_sec < t2.tv_sec)
return Qtrue;
5792 if (t1.tv_sec == t2.tv_sec && t1.tv_nsec < t2.tv_nsec)
return Qtrue;
5799 rb_raise(rb_eArgError,
"unknown command '%s%c'", cmd ==
'\'' || cmd ==
'\\' ?
"\\" :
"", cmd);
5802 rb_raise(rb_eArgError,
"unknown command \"\\x%02X\"", cmd);
5821rb_stat_s_alloc(
VALUE klass)
5842 fname = rb_str_encode_ospath(fname);
5844 rb_sys_fail_path(fname);
5851 rb_st->initialized =
true;
5868 *copy_rb_st = *orig_rb_st;
5887rb_stat_ftype(
VALUE obj)
5889 return rb_file_ftype(get_stat(obj)->ST_(mode));
5906 if (S_ISDIR(get_stat(obj)->ST_(mode)))
return Qtrue;
5922 if (S_ISFIFO(get_stat(obj)->ST_(mode)))
return Qtrue;
5948 if (S_ISLNK(get_stat(obj)->ST_(mode)))
return Qtrue;
5969 if (S_ISSOCK(get_stat(obj)->ST_(mode)))
return Qtrue;
5992 if (S_ISBLK(get_stat(obj)->ST_(mode)))
return Qtrue;
6013 if (S_ISCHR(get_stat(obj)->ST_(mode)))
return Qtrue;
6031rb_stat_owned(
VALUE obj)
6033 if (get_stat(obj)->ST_(uid) == geteuid())
return Qtrue;
6038rb_stat_rowned(
VALUE obj)
6040 if (get_stat(obj)->ST_(uid) == getuid())
return Qtrue;
6057rb_stat_grpowned(
VALUE obj)
6060 if (rb_group_member(get_stat(obj)->ST_(gid)))
return Qtrue;
6079 rb_io_stat_data *st = get_stat(obj);
6082 if (geteuid() == 0)
return Qtrue;
6085 if (rb_stat_owned(obj))
6086 return RBOOL(st->ST_(mode) & S_IRUSR);
6089 if (rb_stat_grpowned(obj))
6090 return RBOOL(st->ST_(mode) & S_IRGRP);
6093 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6112 rb_io_stat_data *st = get_stat(obj);
6115 if (getuid() == 0)
return Qtrue;
6118 if (rb_stat_rowned(obj))
6119 return RBOOL(st->ST_(mode) & S_IRUSR);
6122 if (rb_group_member(get_stat(obj)->ST_(gid)))
6123 return RBOOL(st->ST_(mode) & S_IRGRP);
6126 if (!(st->ST_(mode) & S_IROTH))
return Qfalse;
6145rb_stat_wr(
VALUE obj)
6148 rb_io_stat_data *st = get_stat(obj);
6149 if ((st->ST_(mode) & (S_IROTH)) == S_IROTH) {
6150 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6170 rb_io_stat_data *st = get_stat(obj);
6173 if (geteuid() == 0)
return Qtrue;
6176 if (rb_stat_owned(obj))
6177 return RBOOL(st->ST_(mode) & S_IWUSR);
6180 if (rb_stat_grpowned(obj))
6181 return RBOOL(st->ST_(mode) & S_IWGRP);
6184 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6203 rb_io_stat_data *st = get_stat(obj);
6206 if (getuid() == 0)
return Qtrue;
6209 if (rb_stat_rowned(obj))
6210 return RBOOL(st->ST_(mode) & S_IWUSR);
6213 if (rb_group_member(get_stat(obj)->ST_(gid)))
6214 return RBOOL(st->ST_(mode) & S_IWGRP);
6217 if (!(st->ST_(mode) & S_IWOTH))
return Qfalse;
6236rb_stat_ww(
VALUE obj)
6239 rb_io_stat_data *st = get_stat(obj);
6240 if ((st->ST_(mode) & (S_IWOTH)) == S_IWOTH) {
6241 return UINT2NUM(st->ST_(mode) & (S_IRUGO|S_IWUGO|S_IXUGO));
6263 rb_io_stat_data *st = get_stat(obj);
6266 if (geteuid() == 0) {
6267 return RBOOL(st->ST_(mode) & S_IXUGO);
6271 if (rb_stat_owned(obj))
6272 return RBOOL(st->ST_(mode) & S_IXUSR);
6275 if (rb_stat_grpowned(obj))
6276 return RBOOL(st->ST_(mode) & S_IXGRP);
6279 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6295 rb_io_stat_data *st = get_stat(obj);
6298 if (getuid() == 0) {
6299 return RBOOL(st->ST_(mode) & S_IXUGO);
6303 if (rb_stat_rowned(obj))
6304 return RBOOL(st->ST_(mode) & S_IXUSR);
6307 if (rb_group_member(get_stat(obj)->ST_(gid)))
6308 return RBOOL(st->ST_(mode) & S_IXGRP);
6311 if (!(st->ST_(mode) & S_IXOTH))
return Qfalse;
6330 if (S_ISREG(get_stat(obj)->ST_(mode)))
return Qtrue;
6348 if (get_stat(obj)->ST_(size) == 0)
return Qtrue;
6367 rb_off_t size = get_stat(obj)->ST_(size);
6369 if (size == 0)
return Qnil;
6385rb_stat_suid(
VALUE obj)
6388 if (get_stat(obj)->ST_(mode) & S_ISUID)
return Qtrue;
6406rb_stat_sgid(
VALUE obj)
6409 if (get_stat(obj)->ST_(mode) & S_ISGID)
return Qtrue;
6427rb_stat_sticky(
VALUE obj)
6430 if (get_stat(obj)->ST_(mode) & S_ISVTX)
return Qtrue;
6435#if !defined HAVE_MKFIFO && defined HAVE_MKNOD && defined S_IFIFO
6436#define mkfifo(path, mode) mknod(path, (mode)&~S_IFMT|S_IFIFO, 0)
6447nogvl_mkfifo(
void *ptr)
6449 struct mkfifo_arg *ma = ptr;
6451 return (
void *)(
VALUE)mkfifo(ma->path, ma->mode);
6468 struct mkfifo_arg ma;
6477 path = rb_str_encode_ospath(path);
6478 ma.path = RSTRING_PTR(path);
6479 if (IO_WITHOUT_GVL(nogvl_mkfifo, &ma)) {
6480 rb_sys_fail_path(path);
6485#define rb_file_s_mkfifo rb_f_notimplement
6488static VALUE rb_mFConst;
6491rb_file_const(
const char *name,
VALUE value)
6493 rb_define_const(rb_mFConst, name, value);
6497rb_is_absolute_path(
const char *path)
6499#ifdef DOSISH_DRIVE_LETTER
6500 if (has_drive_letter(path) && isdirsep(path[2]))
return 1;
6503 if (isdirsep(path[0]) && isdirsep(path[1]))
return 1;
6506 if (path[0] ==
'/')
return 1;
6512ruby_is_fd_loadable(
int fd)
6519 if (fstat(fd, &st) < 0)
6522 if (S_ISREG(st.st_mode))
6525 if (S_ISFIFO(st.st_mode) || S_ISCHR(st.st_mode))
6528 if (S_ISDIR(st.st_mode))
6539rb_file_load_ok(
const char *path)
6546 int mode = (O_RDONLY |
6547#if defined O_NONBLOCK
6549#elif defined O_NDELAY
6555 if (!rb_gc_for_fd(
errno))
return 0;
6557 if (fd < 0)
return 0;
6560 ret = ruby_is_fd_loadable(fd);
6567is_explicit_relative(
const char *path)
6569 if (*path++ !=
'.')
return 0;
6570 if (*path ==
'.') path++;
6571 return isdirsep(*path);
6577 int encidx = rb_enc_get_index(orig);
6578 if (encidx == ENCINDEX_ASCII_8BIT || encidx == ENCINDEX_US_ASCII)
6579 encidx = rb_filesystem_encindex();
6580 rb_enc_associate_index(path, encidx);
6588rb_find_file_ext(
VALUE *filep,
const char *
const *ext)
6591 VALUE fname = *filep, load_path, tmp;
6595 if (!ext[0])
return 0;
6598 fname = file_expand_path_1(fname);
6599 f = RSTRING_PTR(fname);
6604 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6605 if (!expanded) fname = file_expand_path_1(fname);
6606 fnlen = RSTRING_LEN(fname);
6607 for (i=0; ext[i]; i++) {
6609 if (rb_file_load_ok(RSTRING_PTR(fname))) {
6610 *filep = copy_path_class(fname, *filep);
6618 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6619 if (!load_path)
return 0;
6622 RBASIC_CLEAR_CLASS(fname);
6623 fnlen = RSTRING_LEN(fname);
6625 rb_enc_associate_index(tmp, rb_usascii_encindex());
6626 for (j=0; ext[j]; j++) {
6628 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6632 if (RSTRING_LEN(str) == 0)
continue;
6633 rb_file_expand_path_internal(fname, str, 0, 0, tmp);
6634 if (rb_file_load_ok(RSTRING_PTR(tmp))) {
6635 *filep = copy_path_class(tmp, *filep);
6641 rb_str_resize(tmp, 0);
6649 VALUE tmp, load_path;
6654 tmp = file_expand_path_1(path);
6655 path = copy_path_class(tmp, path);
6656 f = RSTRING_PTR(path);
6660 if (expanded || rb_is_absolute_path(f) || is_explicit_relative(f)) {
6661 if (!rb_file_load_ok(f))
return 0;
6663 path = copy_path_class(file_expand_path_1(path), path);
6667 RB_GC_GUARD(load_path) = rb_get_expanded_load_path();
6672 rb_enc_associate_index(tmp, rb_usascii_encindex());
6673 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
6676 if (RSTRING_LEN(str) > 0) {
6677 rb_file_expand_path_internal(path, str, 0, 0, tmp);
6678 f = RSTRING_PTR(tmp);
6679 if (rb_file_load_ok(f))
goto found;
6682 rb_str_resize(tmp, 0);
6690 return copy_path_class(tmp, path);
6693#define define_filetest_function(name, func, argc) do { \
6694 rb_define_module_function(rb_mFileTest, name, func, argc); \
6695 rb_define_singleton_method(rb_cFile, name, func, argc); \
6698const char ruby_null_device[] =
6701#elif defined AMIGA || defined __amigaos__
7505#if defined(__APPLE__) && defined(HAVE_WORKING_FORK)
7506 rb_CFString_class_initialize_before_fork();
7514 define_filetest_function(
"directory?", rb_file_directory_p, 1);
7515 define_filetest_function(
"exist?", rb_file_exist_p, 1);
7516 define_filetest_function(
"readable?", rb_file_readable_p, 1);
7517 define_filetest_function(
"readable_real?", rb_file_readable_real_p, 1);
7518 define_filetest_function(
"world_readable?", rb_file_world_readable_p, 1);
7519 define_filetest_function(
"writable?", rb_file_writable_p, 1);
7520 define_filetest_function(
"writable_real?", rb_file_writable_real_p, 1);
7521 define_filetest_function(
"world_writable?", rb_file_world_writable_p, 1);
7522 define_filetest_function(
"executable?", rb_file_executable_p, 1);
7523 define_filetest_function(
"executable_real?", rb_file_executable_real_p, 1);
7524 define_filetest_function(
"file?", rb_file_file_p, 1);
7525 define_filetest_function(
"zero?", rb_file_zero_p, 1);
7526 define_filetest_function(
"empty?", rb_file_zero_p, 1);
7527 define_filetest_function(
"size?", rb_file_size_p, 1);
7528 define_filetest_function(
"size", rb_file_s_size, 1);
7529 define_filetest_function(
"owned?", rb_file_owned_p, 1);
7530 define_filetest_function(
"grpowned?", rb_file_grpowned_p, 1);
7532 define_filetest_function(
"pipe?", rb_file_pipe_p, 1);
7533 define_filetest_function(
"symlink?", rb_file_symlink_p, 1);
7534 define_filetest_function(
"socket?", rb_file_socket_p, 1);
7536 define_filetest_function(
"blockdev?", rb_file_blockdev_p, 1);
7537 define_filetest_function(
"chardev?", rb_file_chardev_p, 1);
7539 define_filetest_function(
"setuid?", rb_file_suid_p, 1);
7540 define_filetest_function(
"setgid?", rb_file_sgid_p, 1);
7541 define_filetest_function(
"sticky?", rb_file_sticky_p, 1);
7543 define_filetest_function(
"identical?", rb_file_identical_p, 2);
7581 separator = rb_fstring_lit(
"/");
7583 rb_define_const(
rb_cFile,
"Separator", separator);
7585 rb_define_const(
rb_cFile,
"SEPARATOR", separator);
7937 rb_define_const(rb_mFConst,
"RDONLY",
INT2FIX(O_RDONLY));
7939 rb_define_const(rb_mFConst,
"WRONLY",
INT2FIX(O_WRONLY));
7941 rb_define_const(rb_mFConst,
"RDWR",
INT2FIX(O_RDWR));
7943 rb_define_const(rb_mFConst,
"APPEND",
INT2FIX(O_APPEND));
7945 rb_define_const(rb_mFConst,
"CREAT",
INT2FIX(O_CREAT));
7947 rb_define_const(rb_mFConst,
"EXCL",
INT2FIX(O_EXCL));
7948#if defined(O_NDELAY) || defined(O_NONBLOCK)
7950# define O_NONBLOCK O_NDELAY
7953 rb_define_const(rb_mFConst,
"NONBLOCK",
INT2FIX(O_NONBLOCK));
7956 rb_define_const(rb_mFConst,
"TRUNC",
INT2FIX(O_TRUNC));
7959 rb_define_const(rb_mFConst,
"NOCTTY",
INT2FIX(O_NOCTTY));
7965 rb_define_const(rb_mFConst,
"BINARY",
INT2FIX(O_BINARY));
7966#ifndef O_SHARE_DELETE
7967# define O_SHARE_DELETE 0
7970 rb_define_const(rb_mFConst,
"SHARE_DELETE",
INT2FIX(O_SHARE_DELETE));
7973 rb_define_const(rb_mFConst,
"SYNC",
INT2FIX(O_SYNC));
7977 rb_define_const(rb_mFConst,
"DSYNC",
INT2FIX(O_DSYNC));
7981 rb_define_const(rb_mFConst,
"RSYNC",
INT2FIX(O_RSYNC));
7985 rb_define_const(rb_mFConst,
"NOFOLLOW",
INT2FIX(O_NOFOLLOW));
7989 rb_define_const(rb_mFConst,
"NOATIME",
INT2FIX(O_NOATIME));
7993 rb_define_const(rb_mFConst,
"DIRECT",
INT2FIX(O_DIRECT));
7997 rb_define_const(rb_mFConst,
"TMPFILE",
INT2FIX(O_TMPFILE));
8001 rb_define_const(rb_mFConst,
"LOCK_SH",
INT2FIX(LOCK_SH));
8003 rb_define_const(rb_mFConst,
"LOCK_EX",
INT2FIX(LOCK_EX));
8005 rb_define_const(rb_mFConst,
"LOCK_UN",
INT2FIX(LOCK_UN));
8007 rb_define_const(rb_mFConst,
"LOCK_NB",
INT2FIX(LOCK_NB));
8010 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 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 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 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.