19 #include <sys/cygwin.h>
33 #ifdef HAVE_SYS_FILE_H
34 # include <sys/file.h>
39 #ifdef HAVE_SYS_PARAM_H
40 # include <sys/param.h>
43 # define MAXPATHLEN 1024
52 #elif defined HAVE_SYS_UTIME_H
53 #include <sys/utime.h>
60 #include <sys/types.h>
63 #ifdef HAVE_SYS_MKDEV_H
64 #include <sys/mkdev.h>
67 #if defined(HAVE_FCNTL_H)
71 #if defined(HAVE_SYS_TIME_H)
75 #if !defined HAVE_LSTAT && !defined lstat
81 #define STAT(p, s) rb_w32_ustati64((p), (s))
83 #define lstat(p, s) rb_w32_ustati64((p), (s))
85 #define access(p, m) rb_w32_uaccess((p), (m))
87 #define chmod(p, m) rb_w32_uchmod((p), (m))
89 #define chown(p, o, g) rb_w32_uchown((p), (o), (g))
91 #define utime(p, t) rb_w32_uutime((p), (t))
93 #define link(f, t) rb_w32_ulink((f), (t))
95 #define unlink(p) rb_w32_uunlink(p)
97 #define rename(f, t) rb_w32_urename((f), (t))
99 #define STAT(p, s) stat((p), (s))
102 #define rb_sys_fail_path(path) rb_sys_fail_str(path)
104 #if defined(__BEOS__) || defined(__HAIKU__)
106 be_chown(
const char *
path, uid_t owner, gid_t group)
108 if (owner == (uid_t)-1 || group == (gid_t)-1) {
110 if (
STAT(path, &st) < 0)
return -1;
111 if (owner == (uid_t)-1) owner = st.st_uid;
112 if (group == (gid_t)-1) group = st.st_gid;
114 return chown(path, owner, group);
116 #define chown be_chown
118 be_fchown(
int fd, uid_t owner, gid_t group)
120 if (owner == (uid_t)-1 || group == (gid_t)-1) {
122 if (
fstat(fd, &st) < 0)
return -1;
123 if (owner == (uid_t)-1) owner = st.st_uid;
124 if (group == (gid_t)-1) group = st.st_gid;
126 return fchown(fd, owner, group);
128 #define fchown be_fchown
135 #define insecure_obj_p(obj, level) ((level) >= 4 || ((level) > 0 && OBJ_TAINTED(obj)))
233 (*func)(s, path,
arg);
265 return p ?
sizeof(
struct stat) : 0;
276 struct stat *nst = 0;
332 #define ST2UINT(val) ((val) & ~(~1UL << (sizeof(val) * CHAR_BIT - 1)))
335 # define NUM2DEVT(v) NUM2UINT(v)
338 # define DEVT2NUM(v) UINT2NUM(v)
340 #ifndef PRI_DEVT_PREFIX
341 # define PRI_DEVT_PREFIX ""
415 #if SIZEOF_STRUCT_STAT_ST_INO > SIZEOF_LONG
416 return ULL2NUM(
get_stat(
self)->st_ino);
527 #if defined(HAVE_ST_RDEV) && defined(major)
548 #if defined(HAVE_ST_RDEV) && defined(minor)
584 #ifdef HAVE_ST_BLKSIZE
605 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
606 # if SIZEOF_STRUCT_STAT_ST_BLOCKS > SIZEOF_LONG
607 return ULL2NUM(
get_stat(
self)->st_blocks);
621 #if defined(HAVE_STRUCT_STAT_ST_ATIM)
622 ts.
tv_nsec = st->st_atim.tv_nsec;
623 #elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC)
624 ts.
tv_nsec = st->st_atimespec.tv_nsec;
625 #elif defined(HAVE_STRUCT_STAT_ST_ATIMENSEC)
645 #if defined(HAVE_STRUCT_STAT_ST_MTIM)
646 ts.
tv_nsec = st->st_mtim.tv_nsec;
647 #elif defined(HAVE_STRUCT_STAT_ST_MTIMESPEC)
648 ts.
tv_nsec = st->st_mtimespec.tv_nsec;
649 #elif defined(HAVE_STRUCT_STAT_ST_MTIMENSEC)
669 #if defined(HAVE_STRUCT_STAT_ST_CTIM)
670 ts.
tv_nsec = st->st_ctim.tv_nsec;
671 #elif defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
672 ts.
tv_nsec = st->st_ctimespec.tv_nsec;
673 #elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
760 static const struct {
789 for (i = 0; i <
sizeof(member)/
sizeof(member[0]); i++) {
797 v = (*member[
i].func)(
self);
801 else if (i == 0 || i == 6) {
834 w32_io_info(
VALUE *file, BY_HANDLE_FILE_INFORMATION *st)
845 if (f == (HANDLE)-1)
return INVALID_HANDLE_VALUE;
857 MultiByteToWideChar(CP_UTF8, 0,
RSTRING_PTR(tmp), -1, ptr, len);
858 f = CreateFileW(ptr, 0,
859 FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING,
863 if (f == INVALID_HANDLE_VALUE)
return f;
866 if (GetFileType(f) == FILE_TYPE_DISK) {
867 ZeroMemory(st,
sizeof(*st));
868 if (GetFileInformationByHandle(f, st))
return ret;
870 if (ret) CloseHandle(ret);
871 return INVALID_HANDLE_VALUE;
921 if (
fstat(fptr->
fd, &st) == -1) {
1002 # ifdef HAVE_GETGROUPS
1005 # define NGROUPS NGROUPS_MAX
1014 gary =
xmalloc(NGROUPS *
sizeof(GETGROUPS_T));
1015 anum = getgroups(NGROUPS, gary);
1016 while (--anum >= 0) {
1017 if (gary[anum] == gid) {
1030 # define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH)
1033 #if defined(S_IXGRP) && !defined(_WIN32) && !defined(__CYGWIN__)
1034 #define USE_GETEUID 1
1037 #ifndef HAVE_EACCESS
1045 if (
STAT(path, &st) < 0)
1063 if (st.st_uid == euid)
1068 if ((
int)(st.st_mode & mode) == mode)
return 0;
1072 return access(path, mode);
1080 return access(path, mode);
1123 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
1145 # define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
1151 if (S_ISFIFO(st.st_mode))
return Qtrue;
1169 # define S_ISLNK(m) _S_ISLNK(m)
1172 # define S_ISLNK(m) (((m) & S_IFMT) == _S_IFLNK)
1175 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
1206 # define S_ISSOCK(m) _S_ISSOCK(m)
1209 # define S_ISSOCK(m) (((m) & S_IFMT) == _S_IFSOCK)
1212 # define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK)
1222 if (S_ISSOCK(st.st_mode))
return Qtrue;
1240 # define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
1242 # define S_ISBLK(m) (0)
1266 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
1331 # define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH)
1335 # define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH)
1467 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
1502 if (st.st_size == 0)
return Qtrue;
1520 if (st.st_size == 0)
return Qnil;
1574 #if defined(S_ISUID) || defined(S_ISGID) || defined(S_ISVTX)
1576 check3rdbyte(
VALUE fname,
int mode)
1584 if (st.st_mode & mode)
return Qtrue;
1600 return check3rdbyte(fname, S_ISUID);
1617 return check3rdbyte(fname, S_ISGID);
1634 return check3rdbyte(fname, S_ISVTX);
1661 struct stat st1, st2;
1665 if (st1.st_dev != st2.st_dev)
return Qfalse;
1666 if (st1.st_ino != st2.st_ino)
return Qfalse;
1669 BY_HANDLE_FILE_INFORMATION st1, st2;
1670 HANDLE f1 = 0, f2 = 0;
1675 f1 = w32_io_info(&fname1, &st1);
1676 if (f1 == INVALID_HANDLE_VALUE)
return Qfalse;
1677 f2 = w32_io_info(&fname2, &st2);
1678 if (f1) CloseHandle(f1);
1679 if (f2 == INVALID_HANDLE_VALUE)
return Qfalse;
1680 if (f2) CloseHandle(f2);
1682 if (st1.dwVolumeSerialNumber == st2.dwVolumeSerialNumber &&
1683 st1.nFileIndexHigh == st2.nFileIndexHigh &&
1684 st1.nFileIndexLow == st2.nFileIndexLow)
1686 if (!f1 || !f2)
return Qfalse;
1718 if (
rb_stat(fname, &st) < 0) {
1733 else if (
S_ISDIR(st->st_mode)) {
1736 else if (
S_ISCHR(st->st_mode)) {
1737 t =
"characterSpecial";
1740 else if (
S_ISBLK(st->st_mode)) {
1745 else if (S_ISFIFO(st->st_mode)) {
1750 else if (
S_ISLNK(st->st_mode)) {
1755 else if (S_ISSOCK(st->st_mode)) {
1811 if (
rb_stat(fname, &st) < 0) {
1836 if (
fstat(fptr->
fd, &st) == -1) {
1857 if (
rb_stat(fname, &st) < 0) {
1881 if (
fstat(fptr->
fd, &st) == -1) {
1906 if (
rb_stat(fname, &st) < 0) {
1933 if (
fstat(fptr->
fd, &st) == -1) {
1959 if (
fstat(fptr->
fd, &st) == -1) {
1968 if (chmod(path, *(
int *)mode) < 0)
2028 if (fchmod(fptr->
fd, mode) == -1)
2040 #if defined(HAVE_LCHMOD)
2042 lchmod_internal(
const char *path,
VALUE pathv,
void *mode)
2044 if (lchmod(path, (
int)(
VALUE)mode) < 0)
2069 n =
apply2files(lchmod_internal, rest, (
void *)(
long)mode);
2073 #define rb_file_s_lchmod rb_f_notimplement
2164 if (fchown(fptr->
fd, o, g) == -1)
2171 #if defined(HAVE_LCHOWN)
2173 lchown_internal(
const char *path,
VALUE pathv,
void *
arg)
2176 if (lchown(path, args->
owner, args->
group) < 0)
2217 #define rb_file_s_lchown rb_f_notimplement
2225 #if defined DOSISH || defined __CYGWIN__
2231 if (tsp &&
errno == EINVAL) {
2234 if (!
NIL_P(atime)) {
2237 if (!
NIL_P(mtime) && mtime != atime && !
rb_equal(atime, mtime)) {
2240 if (
NIL_P(a)) e[0] = m;
2260 #define utime_failed(path, tsp, atime, mtime) rb_sys_fail_path(path)
2263 #if defined(HAVE_UTIMES)
2272 #ifdef HAVE_UTIMENSAT
2273 static int try_utimensat = 1;
2275 if (try_utimensat) {
2276 if (utimensat(AT_FDCWD, path, tsp, 0) < 0) {
2277 if (
errno == ENOSYS) {
2290 tvbuf[0].
tv_usec = (int)(tsp[0].tv_nsec / 1000);
2292 tvbuf[1].
tv_usec = (int)(tsp[1].tv_nsec / 1000);
2295 if (utimes(path, tvp) < 0)
2301 #if !defined HAVE_UTIME_H && !defined HAVE_SYS_UTIME_H
2319 if (utime(path, utp) < 0)
2362 const int max_pathlen = MAX_PATH;
2403 #define rb_file_s_link rb_f_notimplement
2434 #define rb_file_s_symlink rb_f_notimplement
2437 #ifdef HAVE_READLINK
2454 return rb_readlink(path);
2458 rb_readlink(
VALUE path)
2469 while ((rv = readlink(
RSTRING_PTR(path), buf, size)) == size
2471 || (rv < 0 &&
errno == ERANGE)
2487 #define rb_file_s_readlink rb_f_notimplement
2493 if (unlink(path) < 0)
2530 const char *src, *dst;
2540 #if defined __CYGWIN__
2543 if (rename(src, dst) < 0) {
2547 #if defined (__EMX__)
2550 if (chmod(dst, 0666) == 0 &&
2552 rename(src, dst) == 0)
2587 else if (argc == 1) {
2588 omask = umask(
NUM2INT(argv[0]));
2599 #if defined __CYGWIN__ || defined DOSISH
2601 #define DOSISH_DRIVE_LETTER
2602 #define FILE_ALT_SEPARATOR '\\'
2604 #ifdef FILE_ALT_SEPARATOR
2605 #define isdirsep(x) ((x) == '/' || (x) == FILE_ALT_SEPARATOR)
2606 static const char file_alt_separator[] = {FILE_ALT_SEPARATOR,
'\0'};
2608 #define isdirsep(x) ((x) == '/')
2612 #if defined _WIN32 || defined __CYGWIN__
2620 #define istrailinggarbage(x) ((x) == '.' || (x) == ' ')
2622 #define istrailinggarbage(x) 0
2625 #define Next(p, e, enc) ((p) + rb_enc_mbclen((p), (e), (enc)))
2626 #define Inc(p, e, enc) ((p) = Next((p), (e), (enc)))
2628 #if defined(DOSISH_UNC)
2629 #define has_unc(buf) (isdirsep((buf)[0]) && isdirsep((buf)[1]))
2631 #define has_unc(buf) 0
2634 #ifdef DOSISH_DRIVE_LETTER
2636 has_drive_letter(
const char *buf)
2638 if (
ISALPHA(buf[0]) && buf[1] ==
':') {
2647 getcwdofdrv(
int drv)
2650 char *drvcwd, *oldcwd;
2661 if (chdir(drive) == 0) {
2674 not_same_drive(
VALUE path,
int drive)
2678 if (has_drive_letter(p)) {
2687 static inline char *
2690 #ifdef DOSISH_DRIVE_LETTER
2691 if (path + 2 <= end && has_drive_letter(path)) path += 2;
2693 while (path < end &&
isdirsep(*path)) path++;
2694 return (
char *)path;
2697 #define nextdirsep rb_enc_path_next
2707 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
2708 #define skipprefix rb_enc_path_skip_prefix
2710 #define skipprefix(path, end, enc) (path)
2715 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
2719 while (path < end &&
isdirsep(*path)) path++;
2722 return (
char *)path;
2725 #ifdef DOSISH_DRIVE_LETTER
2726 if (has_drive_letter(path))
2727 return (
char *)(path + 2);
2730 return (
char *)path;
2733 static inline char *
2736 #if defined(DOSISH_UNC) || defined(DOSISH_DRIVE_LETTER)
2745 #define strrdirsep rb_enc_path_last_separator
2750 while (path < end) {
2752 const char *tmp = path++;
2753 while (path < end &&
isdirsep(*path)) path++;
2754 if (path >= end)
break;
2758 Inc(path, end, enc);
2767 while (path < end) {
2769 const char *
last = path++;
2770 while (path < end &&
isdirsep(*path)) path++;
2771 if (path >= end)
return (
char *)
last;
2774 Inc(path, end, enc);
2777 return (
char *)path;
2783 if (path < end &&
isdirsep(*path)) path++;
2790 rb_warn(
"rb_path_next() is deprecated");
2797 rb_warn(
"rb_path_skip_prefix() is deprecated");
2804 rb_warn(
"rb_path_last_separator() is deprecated");
2810 rb_warn(
"rb_path_end() is deprecated");
2817 ntfs_tail(
const char *path,
const char *end,
rb_encoding *enc)
2819 while (path < end && *path ==
'.') path++;
2820 while (path < end && *path !=
':') {
2822 const char *
last = path++;
2824 if (path >= end || *path ==
':')
return (
char *)
last;
2827 const char *last = path++;
2828 while (path < end &&
isdirsep(*path)) path++;
2829 if (path >= end)
return (
char *)
last;
2830 if (*path ==
':') path++;
2833 Inc(path, end, enc);
2836 return (
char *)path;
2840 #define BUFCHECK(cond) do {\
2843 do {buflen *= 2;} while (cond);\
2844 rb_str_resize(result, buflen);\
2845 buf = RSTRING_PTR(result);\
2847 pend = buf + buflen;\
2851 #define BUFINIT() (\
2852 p = buf = RSTRING_PTR(result),\
2853 buflen = RSTRING_LEN(result),\
2861 #if defined DOSISH || defined __CYGWIN__
2867 if (!user || !*user) {
2868 if (!(dir =
getenv(
"HOME"))) {
2877 struct passwd *pwPtr = getpwnam(user);
2882 dirlen =
strlen(pwPtr->pw_dir);
2884 memcpy(buf =
RSTRING_PTR(result), pwPtr->pw_dir, dirlen + 1);
2892 #if defined DOSISH || defined __CYGWIN__
2893 for (bend = (p = buf) + dirlen; p < bend;
Inc(p, bend, enc)) {
2906 char *
buf, *cwdp = dir;
2911 do {buflen *= 2;}
while (dirlen > buflen);
2914 memcpy(buf, cwdp, dirlen);
2918 return buf + dirlen;
2924 const char *s, *b, *fend;
2925 char *
buf, *
p, *pend, *root;
2926 size_t buflen, bdiff;
2936 if (s[0] ==
'~' && abs_mode == 0) {
2939 if (
isdirsep(s[1]) || s[1] ==
'\0') {
2948 BUFCHECK(bdiff + userlen >= buflen);
2949 memcpy(p, b, userlen);
2968 #ifdef DOSISH_DRIVE_LETTER
2970 else if (has_drive_letter(s)) {
2983 if (!
NIL_P(dname) && !not_same_drive(dname, s[0])) {
2992 char *e =
append_fspath(result, fname, getcwdofdrv(*s), &enc, fsenc);
3007 if (!
NIL_P(dname)) {
3019 #if defined DOSISH || defined __CYGWIN__
3036 memset(buf,
'/', len);
3040 if (p > buf && p[-1] ==
'/')
3063 if (*(s+1) ==
'\0' ||
isdirsep(*(s+1))) {
3082 #if defined DOSISH || defined __CYGWIN__
3107 #if defined DOSISH || defined __CYGWIN__
3111 long rootdiff = root -
buf;
3113 BUFCHECK(bdiff + (s-b+1) >= buflen);
3114 root = buf + rootdiff;
3115 memcpy(++p, b, s-b);
3129 static const char prime[] =
":$DATA";
3130 enum {prime_len =
sizeof(prime) -1};
3132 if (s > b + prime_len &&
strncasecmp(s - prime_len, prime, prime_len) == 0) {
3135 if (*(s - (prime_len+1)) ==
':') {
3138 else if (memchr(b,
':', s - prime_len - b)) {
3145 memcpy(++p, b, s-b);
3149 if (p ==
skiproot(buf, p + !!*p, enc) - 1) p++;
3153 if ((s =
strrdirsep(b = buf, p, enc)) != 0 && !strpbrk(s,
"*?")) {
3158 WIN32_FIND_DATAW wfd;
3161 #ifdef HAVE_CYGWIN_CONV_PATH
3162 char *w32buf =
NULL;
3163 const int flags = CCP_POSIX_TO_WIN_A | CCP_RELATIVE;
3169 int lnk_added = 0, is_symlink = 0;
3175 if (len > 4 &&
STRCASECMP(p + len - 4,
".lnk") != 0) {
3179 path = *buf ? buf :
"/";
3180 #ifdef HAVE_CYGWIN_CONV_PATH
3181 bufsize = cygwin_conv_path(flags, path,
NULL, 0);
3184 if (lnk_added) bufsize += 4;
3186 if (cygwin_conv_path(flags, path, w32buf, bufsize) == 0) {
3192 if (cygwin_conv_to_win32_path(path, w32buf) == 0) {
3196 if (is_symlink && b == w32buf) {
3200 strlcat(w32buf,
".lnk", bufsize);
3216 MultiByteToWideChar(CP_UTF8, 0,
RSTRING_PTR(tmp), -1, wstr, len);
3218 h = FindFirstFileW(wstr, &wfd);
3220 if (h != INVALID_HANDLE_VALUE) {
3223 len = lstrlenW(wfd.cFileName);
3225 if (lnk_added && len > 4 &&
3226 wcscasecmp(wfd.cFileName + len - 4, L
".lnk") == 0) {
3227 wfd.cFileName[len -= 4] = L
'\0';
3234 len = WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen,
NULL, 0,
NULL,
NULL);
3236 WideCharToMultiByte(CP_UTF8, 0, wfd.cFileName, wlen, p, len + 1,
NULL,
NULL);
3237 if (tmp != result) {
3263 #define EXPAND_PATH_BUFFER() rb_enc_str_new(0, MAXPATHLEN + 2, rb_filesystem_encoding())
3265 #define check_expand_path_args(fname, dname) \
3266 (((fname) = rb_get_path(fname)), \
3267 (void)(NIL_P(dname) ? (dname) : ((dname) = rb_get_path(dname))))
3355 const char *pend = unresolved +
strlen(unresolved);
3359 while (unresolved < pend) {
3360 const char *testname = unresolved;
3362 long testnamelen = unresolved_firstsep - unresolved;
3363 const char *unresolved_nextname = unresolved_firstsep;
3364 while (unresolved_nextname < pend &&
isdirsep(*unresolved_nextname))
3365 unresolved_nextname++;
3366 unresolved = unresolved_nextname;
3367 if (testnamelen == 1 && testname[0] ==
'.') {
3369 else if (testnamelen == 2 && testname[0] ==
'.' && testname[1] ==
'.') {
3371 const char *resolved_str =
RSTRING_PTR(*resolvedp);
3372 const char *resolved_names = resolved_str + *prefixlenp;
3374 long len = lastsep ? lastsep - resolved_names : 0;
3385 if (!
NIL_P(checkval)) {
3386 if (checkval ==
ID2SYM(resolving)) {
3400 if (
errno == ENOENT) {
3401 if (strict || !last || *unresolved_firstsep)
3403 *resolvedp = testpath;
3410 #ifdef HAVE_READLINK
3414 const char *link_prefix, *link_names;
3415 long link_prefixlen;
3417 link = rb_readlink(testpath);
3420 link_prefixlen = link_names - link_prefix;
3421 if (link_prefixlen > 0) {
3428 *prefixlenp = link_prefixlen;
3430 realpath_rec(prefixlenp, resolvedp, link_names, loopcheck, strict, *unresolved_firstsep ==
'\0');
3439 *resolvedp = testpath;
3451 volatile VALUE unresolved_path;
3456 char *path_names =
NULL, *basedir_names =
NULL, *curdir_names =
NULL;
3457 char *ptr, *prefixptr =
NULL, *pend;
3465 if (!
NIL_P(basedir)) {
3472 if (ptr != path_names) {
3473 resolved =
rb_str_subseq(unresolved_path, 0, path_names - ptr);
3477 if (!
NIL_P(basedir)) {
3480 if (ptr != basedir_names) {
3493 pend = prefixptr + prefixlen;
3497 prefixlen = ++ptr - prefixptr;
3500 #ifdef FILE_ALT_SEPARATOR
3501 while (prefixptr < ptr) {
3502 if (*prefixptr == FILE_ALT_SEPARATOR) {
3505 Inc(prefixptr, pend, enc);
3511 realpath_rec(&prefixlen, &resolved, curdir_names, loopcheck, 1, 0);
3513 realpath_rec(&prefixlen, &resolved, basedir_names, loopcheck, 1, 0);
3514 realpath_rec(&prefixlen, &resolved, path_names, loopcheck, strict, 1);
3536 VALUE path, basedir;
3556 VALUE path, basedir;
3566 const char *s, *
last;
3568 if (!e || !l2)
return 0;
3571 if (
rb_enc_ascget(e + len1, e + l2, &len2, enc) ==
'*' && len1 + len2 == l2) {
3572 if (c ==
'.')
return l0;
3582 if (l1 < l2)
return l1;
3586 #if CASEFOLD_FILESYSTEM
3587 #define fncomp strncasecmp
3589 #define fncomp strncmp
3591 if (
fncomp(s, e, l2) == 0) {
3600 const char *
p, *q, *e, *end;
3601 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
3606 end = name + *alllen;
3608 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
3616 #if defined DOSISH_DRIVE_LETTER || defined DOSISH_UNC
3620 #ifdef DOSISH_DRIVE_LETTER
3621 else if (*p ==
':') {
3641 n = ntfs_tail(p, end, enc) -
p;
3645 for (q = p; q - p < n && *q ==
'.'; q++);
3646 for (e = 0; q - p < n;
Inc(q, end, enc)) {
3647 if (*q ==
'.') e = q;
3663 rb_warn(
"ruby_find_basename() is deprecated");
3684 VALUE fname, fext, basename;
3685 const char *
name, *
p;
3689 if (
rb_scan_args(argc, argv,
"11", &fname, &fext) == 2) {
3751 const char *
name, *root, *
p, *end;
3761 if (root > name + 1 &&
isdirsep(*name))
3762 root =
skipprefix(name = root - 2, end, enc);
3764 if (root > name + 1)
3773 #ifdef DOSISH_DRIVE_LETTER
3774 if (has_drive_letter(name) &&
isdirsep(*(name + 2))) {
3782 #ifdef DOSISH_DRIVE_LETTER
3783 if (has_drive_letter(name) && root == name + 2 && p - name == 2)
3806 const char *
p, *e, *end = name + (len ? *len : (
long)
strlen(name));
3815 while (*p && *p ==
'.') p++;
3819 const char *last = p++, *dot =
last;
3821 if (*p ==
'.') dot =
p;
3824 if (!*p || *p ==
':') {
3828 if (*last ==
'.' || dot > last) e = dot;
3835 else if (*p ==
':') {
3846 if (!e || e == name)
3859 rb_warn(
"ruby_find_extname() is deprecated");
3880 const char *
name, *e;
3947 const char *
name, *tail;
3969 switch (
TYPE(tmp)) {
3992 else if (!
NIL_P(sep)) {
4024 #if defined(HAVE_TRUNCATE) || defined(HAVE_CHSIZE)
4049 #ifdef HAVE_TRUNCATE
4060 if (chsize(tmpfd, pos) < 0) {
4070 #define rb_file_s_truncate rb_f_notimplement
4073 #if defined(HAVE_FTRUNCATE) || defined(HAVE_CHSIZE)
4101 #ifdef HAVE_FTRUNCATE
4105 if (chsize(fptr->
fd, pos) < 0)
4111 #define rb_file_truncate rb_f_notimplement
4128 #include <winerror.h>
4129 extern unsigned long __attribute__((stdcall)) GetLastError(
void);
4136 int old_errno =
errno;
4138 int *op = data, ret =
flock(op[0], op[1]);
4141 if (GetLastError() == ERROR_NOT_LOCKED) {
4200 op[1] = op1 =
NUM2INT(operation);
4211 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
4220 #if defined(ERESTART)
4241 for (i=1; i<n; i++) {
4242 switch (
TYPE(argv[i])) {
4253 #define CHECK(n) test_check((n), argc, argv)
4321 if (cmd == 0)
goto unknown;
4322 if (
strchr(
"bcdefgGkloOprRsSuwWxXz", cmd)) {
4394 if (
strchr(
"MAC", cmd)) {
4396 VALUE fname = argv[1];
4399 if (
rb_stat(fname, &st) == -1) {
4419 if (
strchr(
"=<>", cmd)) {
4420 struct stat st1, st2;
4428 if (st1.st_mtime == st2.st_mtime)
return Qtrue;
4432 if (st1.st_mtime > st2.st_mtime)
return Qtrue;
4436 if (st1.st_mtime < st2.st_mtime)
return Qtrue;
4484 struct stat st, *nst;
4509 if (copy == orig)
return orig;
5026 if (size == 0)
return Qnil;
5103 #ifdef DOSISH_DRIVE_LETTER
5104 if (has_drive_letter(path) &&
isdirsep(path[2]))
return 1;
5110 if (path[0] ==
'/')
return 1;
5115 #ifndef ENABLE_PATH_CHECK
5116 # if defined DOSISH || defined __CYGWIN__
5117 # define ENABLE_PATH_CHECK 0
5119 # define ENABLE_PATH_CHECK 1
5123 #if ENABLE_PATH_CHECK
5149 # define S_IWOTH 002
5153 && !(p && execpath && (st.st_mode & S_ISVTX))
5156 rb_warn(
"Insecure world writable dir %s in %sPATH, mode 0%"
5157 PRI_MODET_PREFIX
"o",
5158 p0, (execpath ?
"" :
"LOAD_"), st.st_mode);
5165 if (!s || s == p0)
return 1;
5173 #if ENABLE_PATH_CHECK
5174 #define fpath_check(path) path_check_0((path), FALSE)
5176 #define fpath_check(path) 1
5182 #if ENABLE_PATH_CHECK
5183 const char *p0, *
p, *pend;
5186 if (!path)
return 1;
5188 pend = path +
strlen(path);
5198 if (p0 > pend)
break;
5211 int fd = open(path, O_RDONLY);
5212 if (fd == -1)
return 0;
5230 if (*path++ !=
'.')
return 0;
5231 if (*path ==
'.') path++;
5253 VALUE fname = *filep, load_path, tmp;
5257 if (!ext[0])
return 0;
5275 for (i=0; ext[
i]; i++) {
5286 if (safe_level >= 4) {
5291 if (!load_path)
return 0;
5294 RBASIC(fname)->klass = 0;
5298 for (j=0; ext[j]; j++) {
5300 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
5327 VALUE tmp, load_path;
5351 if (safe_level >= 4) {
5361 for (i = 0; i <
RARRAY_LEN(load_path); i++) {
5394 #elif defined AMIGA || defined __amigaos__
char * rb_path_skip_prefix(const char *path)
static int access_internal(const char *path, int mode)
#define RSTRING_LEN(string)
static VALUE rb_stat_ino(VALUE self)
void rb_w32_init_file(void)
VALUE rb_get_path(VALUE obj)
rb_encoding * rb_enc_check(VALUE str1, VALUE str2)
static VALUE UINT2NUM(unsigned int v)
static VALUE rb_file_socket_p(VALUE obj, VALUE fname)
static VALUE rb_file_s_mtime(VALUE klass, VALUE fname)
static VALUE stat_mtime(struct stat *st)
void rb_enc_copy(VALUE obj1, VALUE obj2)
static VALUE rb_file_sgid_p(VALUE obj, VALUE fname)
static struct timespec stat_atimespec(struct stat *st)
size_t strlen(const char *)
static VALUE rb_get_path_check(VALUE obj, int level)
static VALUE rb_stat_z(VALUE obj)
void rb_update_max_fd(int fd)
static VALUE rb_stat_init(VALUE obj, VALUE fname)
VALUE rb_time_nano_new(time_t, long)
static VALUE rb_file_executable_p(VALUE obj, VALUE fname)
static VALUE rb_stat_rdev(VALUE self)
static VALUE rb_stat_cmp(VALUE self, VALUE other)
int rb_is_absolute_path(const char *path)
static VALUE rb_stat_init_copy(VALUE copy, VALUE orig)
static VALUE rb_file_grpowned_p(VALUE obj, VALUE fname)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
#define skipprefix(path, end, enc)
#define access(path, mode)
static VALUE rb_file_size_p(VALUE obj, VALUE fname)
static VALUE rb_file_s_size(VALUE klass, VALUE fname)
void rb_file_const(const char *name, VALUE value)
VALUE rb_str_cat(VALUE, const char *, long)
static VALUE rb_f_test(int argc, VALUE *argv)
static VALUE rb_stat_mode(VALUE self)
int rb_find_file_ext(VALUE *filep, const char *const *ext)
static VALUE rb_file_world_writable_p(VALUE obj, VALUE fname)
static VALUE rb_file_readable_p(VALUE obj, VALUE fname)
static VALUE rb_stat_f(VALUE obj)
#define TypedData_Wrap_Struct(klass, data_type, sval)
void rb_io_check_initialized(rb_io_t *)
#define TypedData_Get_Struct(obj, type, data_type, sval)
const struct timespec * tsp
#define ENC_CODERANGE_CLEAR(obj)
VALUE rb_enc_from_encoding(rb_encoding *encoding)
SOCKET rb_w32_get_osfhandle(int)
int eaccess(const char *path, int mode)
rb_encoding * rb_default_internal_encoding(void)
VALUE rb_str_buf_new2(const char *)
static VALUE rb_file_flock(VALUE obj, VALUE operation)
const char * ruby_enc_find_basename(const char *name, long *baselen, long *alllen, rb_encoding *enc)
int rb_usascii_encindex(void)
VALUE rb_str_plus(VALUE, VALUE)
VALUE rb_file_s_expand_path(int argc, VALUE *argv)
#define FilePathStringValue(v)
static VALUE INT2NUM(int v)
static VALUE rb_stat_blocks(VALUE self)
static VALUE copy_path_class(VALUE path, VALUE orig)
#define RSTRING_PTR(string)
int rb_enc_str_coderange(VALUE)
unsigned int rb_enc_codepoint_len(const char *p, const char *e, int *len_p, rb_encoding *enc)
VALUE rb_define_class_under(VALUE outer, const char *name, VALUE super)
Defines a class under the namespace of outer.
void rb_raise(VALUE exc, const char *fmt,...)
static long apply2files(void(*func)(const char *, VALUE, void *), VALUE vargs, void *arg)
static struct timespec stat_mtimespec(struct stat *st)
#define RSTRING_GETMEM(str, ptrvar, lenvar)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
static VALUE rb_stat_w(VALUE obj)
VALUE rb_io_taint_check(VALUE)
VALUE rb_exec_recursive(VALUE(*)(VALUE, VALUE, int), VALUE, VALUE)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_obj_is_kind_of(VALUE, VALUE)
static VALUE rb_file_zero_p(VALUE obj, VALUE fname)
static VALUE rb_stat_d(VALUE obj)
static VALUE file_inspect_join(VALUE ary, VALUE argp, int recur)
#define RARRAY_LEN(ARRAY)
VALUE rb_thread_io_blocking_region(rb_blocking_function_t *func, void *data1, int fd)
static VALUE rb_file_size(VALUE obj)
void rb_include_module(VALUE klass, VALUE module)
static VALUE file_path_convert(VALUE name)
static VALUE rb_file_lstat(VALUE obj)
static VALUE rb_stat_dev_minor(VALUE self)
static VALUE rb_file_suid_p(VALUE obj, VALUE fname)
static VALUE rb_file_s_basename(int argc, VALUE *argv)
int rb_str_cmp(VALUE, VALUE)
static VALUE rb_stat_S(VALUE obj)
static VALUE file_expand_path_1(VALUE fname)
void rb_define_global_function(const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a global function.
static VALUE rb_file_s_utime(int argc, VALUE *argv)
static VALUE rb_stat_s(VALUE obj)
static VALUE rb_stat_dev(VALUE self)
rb_encoding * rb_utf8_encoding(void)
static void realpath_rec(long *prefixlenp, VALUE *resolvedp, const char *unresolved, VALUE loopcheck, int strict, int last)
static void sys_fail2(VALUE s1, VALUE s2)
VALUE rb_str_dup_frozen(VALUE)
static VALUE rb_file_ctime(VALUE obj)
static VALUE rb_stat_uid(VALUE self)
VALUE rb_str_tmp_new(long)
VALUE rb_str_buf_append(VALUE, VALUE)
#define GetOpenFile(obj, fp)
static VALUE rb_stat_r(VALUE obj)
static VALUE rb_stat_grpowned(VALUE obj)
#define ENC_CODERANGE_7BIT
const char * rb_obj_classname(VALUE)
struct timespec rb_time_timespec(VALUE time)
static VALUE rb_stat_x(VALUE obj)
VALUE rb_str_buf_cat(VALUE, const char *, long)
static char * chompdirsep(const char *path, const char *end, rb_encoding *enc)
#define rb_file_s_readlink
void rb_exc_raise(VALUE mesg)
static VALUE rb_file_blockdev_p(VALUE obj, VALUE fname)
char * rb_enc_path_skip_prefix(const char *path, const char *end, rb_encoding *enc)
static VALUE rb_file_world_readable_p(VALUE obj, VALUE fname)
static VALUE rb_stat_s_alloc(VALUE klass)
#define RB_TYPE_P(obj, type)
static VALUE rb_file_ftype(const struct stat *st)
static VALUE rb_thread_flock(void *data)
static int path_check_0(VALUE path, int execpath)
static VALUE stat_ctime(struct stat *st)
static VALUE rb_stat_size(VALUE self)
static VALUE rb_file_s_split(VALUE klass, VALUE path)
static VALUE rb_file_pipe_p(VALUE obj, VALUE fname)
VALUE rb_find_file(VALUE path)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
char * rb_path_last_separator(const char *path)
static VALUE rb_file_s_chmod(int argc, VALUE *argv)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
static VALUE rb_file_s_lstat(VALUE klass, VALUE fname)
VALUE rb_str_new_shared(VALUE)
VALUE rb_obj_taint(VALUE)
VALUE rb_str_encode_ospath(VALUE path)
static VALUE rb_file_readable_real_p(VALUE obj, VALUE fname)
#define rb_file_s_symlink
RUBY_EXTERN VALUE rb_cObject
static VALUE rb_file_s_stat(VALUE klass, VALUE fname)
static VALUE rb_file_mtime(VALUE obj)
static VALUE rb_file_s_chown(int argc, VALUE *argv)
static VALUE rb_file_s_realpath(int argc, VALUE *argv, VALUE klass)
static VALUE rb_stat_owned(VALUE obj)
VALUE rb_str_cat2(VALUE, const char *)
VALUE rb_str_buf_cat2(VALUE, const char *)
static int is_explicit_relative(const char *path)
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
static VALUE rb_file_exist_p(VALUE obj, VALUE fname)
int chown(const char *, int, int)
void rb_define_const(VALUE, const char *, VALUE)
VALUE rb_home_dir(const char *user, VALUE result)
static char * skiproot(const char *path, const char *end, rb_encoding *enc)
char * rb_enc_path_next(const char *s, const char *e, rb_encoding *enc)
VALUE rb_str_conv_enc(VALUE str, rb_encoding *from, rb_encoding *to)
#define insecure_obj_p(obj, level)
static VALUE rb_file_sticky_p(VALUE obj, VALUE fname)
static VALUE rb_file_s_join(VALUE klass, VALUE args)
static VALUE rb_file_atime(VALUE obj)
#define ALLOCV_N(type, v, n)
static size_t stat_memsize(const void *p)
static VALUE rb_stat_b(VALUE obj)
#define ALLOCA_N(type, n)
#define fpath_check(path)
VALUE rb_enc_associate_index(VALUE obj, int idx)
static void chmod_internal(const char *path, VALUE pathv, void *mode)
VALUE rb_find_file_safe(VALUE path, int safe_level)
static VALUE rb_stat_suid(VALUE obj)
int link(const char *, const char *)
static VALUE rb_file_writable_real_p(VALUE obj, VALUE fname)
static VALUE rb_stat_sticky(VALUE obj)
static VALUE rb_stat_ftype(VALUE obj)
static char * append_fspath(VALUE result, VALUE fname, char *dir, rb_encoding **enc, rb_encoding *fsenc)
VALUE rb_file_absolute_path(VALUE fname, VALUE dname)
VALUE rb_str_resize(VALUE, long)
size_t rb_str_capacity(VALUE)
int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
VALUE rb_str_subseq(VALUE, long, long)
static VALUE rb_stat_blksize(VALUE self)
static VALUE rb_file_identical_p(VALUE obj, VALUE fname1, VALUE fname2)
void rb_define_module_function(VALUE module, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a module function for module.
VALUE rb_get_load_path(void)
SSL_METHOD *(* func)(void)
VALUE rb_check_funcall(VALUE, ID, int, VALUE *)
VALUE rb_file_s_absolute_path(int argc, VALUE *argv)
VALUE rb_sprintf(const char *format,...)
#define RB_GC_GUARD(object)
VALUE rb_eSystemCallError
static VALUE rb_file_s_path(VALUE klass, VALUE fname)
static VALUE rb_stat_dev_major(VALUE self)
VALUE rb_file_dirname(VALUE fname)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
static char * skipprefixroot(const char *path, const char *end, rb_encoding *enc)
static VALUE rb_stat_ctime(VALUE self)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
rb_encoding * rb_usascii_encoding(void)
VALUE rb_file_directory_p(VALUE obj, VALUE fname)
static int rb_group_member(GETGROUPS_T gid)
#define EXPAND_PATH_BUFFER()
#define check_expand_path_args(fname, dname)
static VALUE rb_stat_sgid(VALUE obj)
char * strchr(char *, char)
static VALUE rb_stat_inspect(VALUE self)
register unsigned int len
#define RARRAY_PTR(ARRAY)
#define rb_enc_asciicompat(enc)
static VALUE rb_file_writable_p(VALUE obj, VALUE fname)
VALUE rb_str_ellipsize(VALUE, long)
Shortens str and adds three dots, an ellipsis, if it is longer than len characters.
VALUE rb_str_new_cstr(const char *)
static void utime_internal(const char *path, VALUE pathv, void *arg)
static struct timespec stat_ctimespec(struct stat *st)
static void chown_internal(const char *path, VALUE pathv, void *arg)
static void test_check(int n, int argc, VALUE *argv)
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
char * rb_path_next(const char *path)
static VALUE rb_stat_wr(VALUE obj)
static VALUE rb_stat_mtime(VALUE self)
static VALUE stat_new(struct stat *st)
VALUE rb_obj_is_instance_of(VALUE, VALUE)
VALUE rb_usascii_str_new2(const char *)
#define ENCODING_GET(obj)
VALUE rb_equal(VALUE, VALUE)
rb_encoding * rb_enc_get(VALUE obj)
#define utime_failed(path, tsp, atime, mtime)
void rb_insecure_operation(void)
static const char null_device[]
static VALUE stat_new_0(VALUE klass, struct stat *st)
static VALUE rb_file_s_ftype(VALUE klass, VALUE fname)
int rb_path_check(const char *path)
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
NORETURN(static void sys_fail2(VALUE, VALUE))
static VALUE ULONG2NUM(unsigned long v)
VALUE rb_file_expand_path(VALUE fname, VALUE dname)
static const rb_data_type_t stat_data_type
static VALUE rb_file_chown(VALUE obj, VALUE owner, VALUE group)
static VALUE rb_stat_rowned(VALUE obj)
char * rb_path_end(const char *path)
static VALUE rb_stat_c(VALUE obj)
static VALUE rb_file_s_extname(VALUE klass, VALUE fname)
VALUE rb_hash_aref(VALUE hash, VALUE key)
static VALUE rb_file_chmod(VALUE obj, VALUE vmode)
VALUE rb_str_catf(VALUE str, const char *format,...)
const char * ruby_find_extname(const char *name, long *len)
static VALUE rb_file_chardev_p(VALUE obj, VALUE fname)
static VALUE stat_atime(struct stat *st)
#define rb_str_set_len(str, length)
static VALUE rb_file_s_dirname(VALUE klass, VALUE fname)
static VALUE rb_file_owned_p(VALUE obj, VALUE fname)
static void unlink_internal(const char *path, VALUE pathv, void *arg)
rb_encoding * rb_filesystem_encoding(void)
static VALUE rb_stat_atime(VALUE self)
static VALUE rb_stat_gid(VALUE self)
static VALUE rb_file_rowned_p(VALUE obj, VALUE fname)
static size_t rmext(const char *p, long l0, long l1, const char *e, long l2, rb_encoding *enc)
#define rb_enc_left_char_head(s, p, e, enc)
VALUE rb_str_inspect(VALUE)
static VALUE rb_file_s_rename(VALUE klass, VALUE from, VALUE to)
static VALUE rb_stat_p(VALUE obj)
const char * ruby_find_basename(const char *name, long *baselen, long *alllen)
static VALUE rb_stat_rdev_major(VALUE self)
char * rb_enc_path_end(const char *path, const char *end, rb_encoding *enc)
static struct stat * get_stat(VALUE self)
static int rb_stat(VALUE file, struct stat *st)
static VALUE rb_stat_X(VALUE obj)
VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict)
#define istrailinggarbage(x)
char * rb_enc_path_last_separator(const char *path, const char *end, rb_encoding *enc)
static char NUM2CHR(VALUE x)
RUBY_EXTERN VALUE rb_eIOError
static VALUE rb_file_executable_real_p(VALUE obj, VALUE fname)
int rb_find_file_ext_safe(VALUE *filep, const char *const *ext, int safe_level)
int rb_file_load_ok(const char *path)
static VALUE rb_stat_ww(VALUE obj)
RUBY_EXTERN size_t strlcat(char *, const char *, size_t)
static VALUE rb_file_s_ctime(VALUE klass, VALUE fname)
static VALUE rb_file_path(VALUE obj)
static VALUE rb_io_stat(VALUE obj)
#define StringValuePtr(v)
#define STRCASECMP(s1, s2)
rb_encoding * rb_ascii8bit_encoding(void)
#define rb_check_frozen(obj)
#define CONST_ID(var, str)
#define RUBY_TYPED_DEFAULT_FREE
static VALUE rb_stat_nlink(VALUE self)
VALUE rb_obj_freeze(VALUE)
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
VALUE rb_define_module(const char *name)
int rb_enc_str_asciionly_p(VALUE)
const char * ruby_enc_find_extname(const char *name, long *len, rb_encoding *enc)
VALUE rb_str_buf_new(long)
static VALUE rb_file_s_umask(int argc, VALUE *argv)
VALUE rb_get_path_no_checksafe(VALUE obj)
#define rb_file_s_truncate
void rb_thread_polling(void)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
static VALUE rb_stat_rdev_minor(VALUE self)
static VALUE rb_stat_l(VALUE obj)
static VALUE rb_file_s_realdirpath(int argc, VALUE *argv, VALUE klass)
static VALUE rb_file_s_unlink(VALUE klass, VALUE args)
VALUE rb_str_new2(const char *)
static VALUE rb_file_join(VALUE ary, VALUE sep)
int rb_memcicmp(const void *, const void *, long)
void rb_warn(const char *fmt,...)
void rb_io_check_closed(rb_io_t *)
static VALUE rb_stat_W(VALUE obj)
static VALUE rb_file_s_atime(VALUE klass, VALUE fname)
VALUE rb_file_expand_path_internal(VALUE fname, VALUE dname, int abs_mode, int long_name, VALUE result)
static VALUE rb_stat_R(VALUE obj)
VALUE rb_file_expand_path_fast(VALUE fname, VALUE dname)
static void define_filetest_function(const char *name, VALUE(*func)(ANYARGS), int argc)
rb_encoding * rb_enc_from_index(int index)
VALUE rb_filesystem_str_new(const char *, long)
VALUE rb_str_new(const char *, long)
VALUE rb_obj_class(VALUE)
#define rb_sys_fail_path(path)
static VALUE rb_file_file_p(VALUE obj, VALUE fname)
static VALUE rb_file_symlink_p(VALUE obj, VALUE fname)