Commit 7131b620
7131b620300fMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +1 added · ~3 modified
Description
Implement Dir singleton methods: mkdir/rmdir/chdir/glob/home
Dir.mkdir, Dir.rmdir (and its delete/unlink aliases), Dir.chdir (no block), Dir.glob (and Dir.[]), and a portable Dir.home were unresolved and fell through to the integer 0 fallback.
Add runtime helpers in sp_runtime.h:
- mkdir/rmdir/chdir wrap the platform call (Windows _-prefixed variants via <direct.h>; POSIX otherwise), returning 0 on success;
- home reads HOME, falling back to USERPROFILE on Windows;
- glob lists entries matching the last path component via opendir/readdir
(<dirent.h>, available on both POSIX and MinGW) with a small
*/?matcher, hidden-file rules, and sorted results (Ruby 3.0+ glob order).
Kept Windows-portable throughout with #ifdef; no POSIX-only glob.h or fnmatch.h. codegen dispatches and analyze types the results (int / str_array / string).
Fixes #1283
Co-Authored-By: Claude Opus 4.8 [email protected]
Details
- Commit
- 7131b620300f
- Parent
- b1a0f05fa17b
lib/sp_runtime.hmodifiedView file
@@ -26,9 +26,11 @@
2626
#include <setjmp.h>2727
#include <errno.h>2828
#include <sys/stat.h>29+
#include <dirent.h>2930
#ifdef _WIN323031
#include <windows.h>3132
#include <process.h>33+
#include <direct.h>3234
/* POSIX compat shims for MinGW */3335
#define mmap(a,l,p,f,fd,off) VirtualAlloc(NULL,(l),MEM_RESERVE|MEM_COMMIT,PAGE_READWRITE)3436
#define munmap(a,l) (VirtualFree((a),0,MEM_RELEASE)?0:-1)@@ -3587,6 +3589,99 @@
35873589
memcpy(buf, tmp, n + 1);35883590
return buf;35893591
}3592+
/* Dir singleton methods. mkdir/rmdir/chdir use the platform call (the3593+
Windows _-prefixed variants take a single path argument); each returns3594+
0 on success, matching CRuby's `Dir.mkdir` etc. */3595+
static mrb_int sp_dir_mkdir(const char *path) {3596+
#ifdef _WIN323597+
return (mrb_int)_mkdir(path);3598+
#else3599+
return (mrb_int)mkdir(path, 0777);3600+
#endif3601+
}3602+
static mrb_int sp_dir_rmdir(const char *path) {3603+
#ifdef _WIN323604+
return (mrb_int)_rmdir(path);3605+
#else3606+
return (mrb_int)rmdir(path);3607+
#endif3608+
}3609+
static mrb_int sp_dir_chdir(const char *path) {3610+
#ifdef _WIN323611+
return (mrb_int)_chdir(path);3612+
#else3613+
return (mrb_int)chdir(path);3614+
#endif3615+
}3616+
static const char *sp_dir_home(void) {3617+
const char *h = getenv("HOME");3618+
#ifdef _WIN323619+
if (!h || !*h) h = getenv("USERPROFILE");3620+
#endif3621+
if (!h) return sp_str_empty;3622+
return sp_str_dup_external(h);3623+
}3624+
/* Wildcard match for a single path component: `*` (any run, no `/`),3625+
`?` (one char). Recursive over `*`; adequate for the common3626+
single-directory glob patterns. */3627+
static int sp_fnmatch1(const char *pat, const char *str) {3628+
while (*pat) {3629+
if (*pat == '*') {3630+
pat++;3631+
if (!*pat) return 1;3632+
while (*str) { if (sp_fnmatch1(pat, str)) return 1; str++; }3633+
return sp_fnmatch1(pat, str);3634+
} else if (*pat == '?') {3635+
if (!*str) return 0;3636+
pat++; str++;3637+
} else {3638+
if (*pat != *str) return 0;3639+
pat++; str++;3640+
}3641+
}3642+
return *str == 0;3643+
}3644+
/* Dir.glob(pattern): list directory entries matching the last component3645+
of `pattern` (an optional leading `dir/` selects the directory). Hidden3646+
entries match only when the pattern itself begins with `.`. Results are3647+
sorted, matching Ruby 3.0+ default glob ordering. */3648+
static sp_StrArray *sp_dir_glob(const char *pattern) {3649+
sp_StrArray *a = sp_StrArray_new();3650+
if (!pattern) return a;3651+
const char *slash = strrchr(pattern, '/');3652+
char dirbuf[1024];3653+
const char *dirpath;3654+
const char *base_pat;3655+
if (slash) {3656+
size_t dl = (size_t)(slash - pattern);3657+
if (dl >= sizeof(dirbuf)) return a;3658+
memcpy(dirbuf, pattern, dl);3659+
dirbuf[dl] = 0;3660+
dirpath = (dl == 0) ? "/" : dirbuf;3661+
base_pat = slash + 1;3662+
} else {3663+
dirpath = ".";3664+
base_pat = pattern;3665+
}3666+
DIR *d = opendir(dirpath);3667+
if (!d) return a;3668+
struct dirent *e;3669+
while ((e = readdir(d)) != NULL) {3670+
const char *name = e->d_name;3671+
if (name[0] == '.' && base_pat[0] != '.') continue;3672+
if (sp_fnmatch1(base_pat, name)) {3673+
char full[2048];3674+
if (slash) snprintf(full, sizeof(full), "%s/%s", dirbuf, name);3675+
else snprintf(full, sizeof(full), "%s", name);3676+
char *copy = sp_str_alloc(strlen(full));3677+
strcpy(copy, full);3678+
sp_StrArray_push(a, copy);3679+
}3680+
}3681+
closedir(d);3682+
sp_StrArray_sort_bang(a);3683+
return a;3684+
}35903685
35913686
/* File.expand_path(path[, base]) -- CRuby-compatible pure-string35923687
expansion (does NOT require the path to exist). A leading `~` / `~/`spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…
test/dir_methods.rbaddedView file
Loading diff…