aboutsummaryrefslogtreecommitdiffstats
path: root/dir.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-03 05:04:49 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-02-03 05:04:49 +0000
commit45df1c24d269f93a2bc1e7a6fe0ffcecc1193051 (patch)
treed761bfe41c93637d194b2a4fc2da9ed8080152e8 /dir.c
parent3e2942423e38ab70738dd9aafdbbb06e834d3cc4 (diff)
downloadruby-45df1c24d269f93a2bc1e7a6fe0ffcecc1193051.tar.gz
dir.c: glob legacy short name
* dir.c (glob_helper): obtain real name with FindFirstFile API instead of matchin all entries, on Windows. [ruby-core:67954] [Bug #10819] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49478 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'dir.c')
-rw-r--r--dir.c75
1 files changed, 68 insertions, 7 deletions
diff --git a/dir.c b/dir.c
index c7ba1b0c04..e983ea2552 100644
--- a/dir.c
+++ b/dir.c
@@ -81,6 +81,10 @@ char *strchr(char*,char);
# define USE_NAME_ON_FS 1
# define RUP32(size) ((size)+3/4)
# define SIZEUP32(type) RUP32(sizeof(type))
+#elif defined _WIN32
+# define USE_NAME_ON_FS 1
+#elif defined DOSISH
+# define USE_NAME_ON_FS 2 /* by fnmatch */
#else
# define USE_NAME_ON_FS 0
#endif
@@ -1177,10 +1181,30 @@ has_magic(const char *p, const char *pend, int flags, rb_encoding *enc)
return PLAIN;
continue;
+#ifdef _WIN32
+ case '~':
+ /* possibly legacy 8.3 short name */
+ if (p < pend && (c = *p, ISDIGIT(c))) {
+ while (++p < pend && (c = *p, ISDIGIT(c)));
+ if (p == pend || c == '.') hasalpha = 1;
+ }
+ continue;
+#endif
+
default:
if (ISALPHA(c)) {
hasalpha = 1;
}
+#ifdef _WIN32
+ else if (!rb_isascii(c)) {
+ unsigned int code = rb_enc_mbc_to_codepoint(p, pend, enc);
+ if (ONIGENC_IS_CODE_ALPHA(enc, c)) {
+ /* Full width alphabets */
+ hasalpha = 1;
+ }
+ }
+#endif
+ break;
}
p = Next(p-1, pend, enc);
@@ -1373,7 +1397,7 @@ is_case_sensitive(DIR *dirp)
}
static char *
-replace_real_basename(char *path, long base, int norm_p)
+replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p)
{
u_int32_t attrbuf[SIZEUP32(attrreference_t) + RUP32(MAXPATHLEN * 3) + 1];
struct attrlist al = {ATTR_BIT_MAP_COUNT, 0, ATTR_CMN_NAME};
@@ -1408,6 +1432,43 @@ replace_real_basename(char *path, long base, int norm_p)
IF_NORMALIZE_UTF8PATH(if (!NIL_P(utf8str)) rb_str_resize(utf8str, 0));
return path;
}
+#elif defined _WIN32
+VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc);
+
+static char *
+replace_real_basename(char *path, long base, rb_encoding *enc, int norm_p)
+{
+ char *plainname = path;
+ volatile VALUE tmp = 0;
+ WIN32_FIND_DATAW fd;
+ WCHAR *wplain;
+ HANDLE h;
+ long wlen;
+ if (enc &&
+ enc != rb_usascii_encoding() &&
+ enc != rb_ascii8bit_encoding() &&
+ enc != rb_utf8_encoding()) {
+ tmp = rb_enc_str_new_cstr(plainname, enc);
+ tmp = rb_str_encode_ospath(tmp);
+ plainname = RSTRING_PTR(tmp);
+ }
+ wplain = rb_w32_mbstr_to_wstr(CP_UTF8, plainname, -1, &wlen);
+ if (tmp) rb_str_resize(tmp, 0);
+ if (!wplain) return path;
+ h = FindFirstFileW(wplain, &fd);
+ free(wplain);
+ if (h == INVALID_HANDLE_VALUE) return path;
+ FindClose(h);
+ tmp = rb_w32_conv_from_wchar(fd.cFileName, enc);
+ wlen = RSTRING_LEN(tmp);
+ path = GLOB_REALLOC(path, base + wlen + 1);
+ memcpy(path + base, RSTRING_PTR(tmp), wlen);
+ path[base + wlen] = 0;
+ rb_str_resize(tmp, 0);
+ return path;
+}
+#elif USE_NAME_ON_FS == 1
+# error not implemented
#endif
enum answer {UNKNOWN = -1, NO, YES};
@@ -1473,7 +1534,7 @@ glob_helper(
plain = 1;
break;
case ALPHA:
-#ifdef HAVE_GETATTRLIST
+#if defined HAVE_GETATTRLIST || defined _WIN32
plain = 1;
#else
magical = 1;
@@ -1533,11 +1594,11 @@ glob_helper(
if (magical || recursive) {
struct dirent *dp;
DIR *dirp;
-# ifdef DOSISH
+# if USE_NAME_ON_FS == 2
char *plainname = 0;
# endif
IF_NORMALIZE_UTF8PATH(int norm_p);
-# ifdef DOSISH
+# if USE_NAME_ON_FS == 2
if (cur + 1 == end && (*cur)->type <= ALPHA) {
plainname = join_path(path, pathlen, dirsep, (*cur)->str, strlen((*cur)->str));
if (!plainname) return -1;
@@ -1633,7 +1694,7 @@ glob_helper(
}
switch (p->type) {
case ALPHA:
-# ifdef DOSISH
+# if USE_NAME_ON_FS == 2
if (plainname) {
*new_end++ = p->next;
break;
@@ -1703,10 +1764,10 @@ glob_helper(
status = -1;
break;
}
-#ifdef HAVE_GETATTRLIST
+#if defined HAVE_GETATTRLIST || defined _WIN32
if ((*cur)->type == ALPHA) {
long base = pathlen + (dirsep != 0);
- buf = replace_real_basename(buf, base, IF_NORMALIZE_UTF8PATH(1)+0);
+ buf = replace_real_basename(buf, base, enc, IF_NORMALIZE_UTF8PATH(1)+0);
}
#endif
status = glob_helper(buf, 1, UNKNOWN, UNKNOWN, new_beg,