summaryrefslogtreecommitdiff
path: root/win32
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-02 01:59:05 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-02-02 01:59:05 +0000
commit41e770944ee9ee3be4eaad1ec282cd6b88b5933f (patch)
tree8f8272d458fccfc5d4f4ddcf0a004805a874e3e1 /win32
parentef9206bbea3b8cb1af66966e74fe8527edab2418 (diff)
* win32/win32.c (open_dir_handle, rb_w32_opendir, move_to_next_entry,
rb_w32_readdir, check_valid_dir): change backend API from A to W. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@21937 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'win32')
-rw-r--r--win32/win32.c64
1 files changed, 37 insertions, 27 deletions
diff --git a/win32/win32.c b/win32/win32.c
index b248721915..25368c1edb 100644
--- a/win32/win32.c
+++ b/win32/win32.c
@@ -33,9 +33,6 @@
#endif
#include "ruby/win32.h"
#include "win32/dir.h"
-#ifndef index
-#define index(x, y) strchr((x), (y))
-#endif
#define isdirsep(x) ((x) == '/' || (x) == '\\')
#undef stat
@@ -1434,31 +1431,40 @@ rb_w32_cmdvector(const char *cmd, char ***vec)
#define DIRENT_PER_CHAR (CHAR_BIT / 2)
static HANDLE
-open_dir_handle(const char *filename, WIN32_FIND_DATA *fd)
+open_dir_handle(const char *filename, WIN32_FIND_DATAW *fd)
{
HANDLE fh;
- static const char wildcard[] = "/*";
- long len = strlen(filename);
- char *scanname = malloc(len + sizeof(wildcard));
+ static const WCHAR wildcard[] = L"\\*";
+ UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
+ WCHAR *scanname;
+ WCHAR *p;
+ int len;
//
// Create the search pattern
//
- if (!scanname) {
+ len = MultiByteToWideChar(cp, 0, filename, -1, NULL, 0);
+ if (len <= 0) {
+ errno = map_errno(GetLastError());
+ return INVALID_HANDLE_VALUE;
+ }
+ scanname = ALLOCA_N(WCHAR, len + sizeof(wildcard) / sizeof(WCHAR));
+ len = MultiByteToWideChar(cp, 0, filename, -1, scanname, len + 2);
+ if (len <= 0) {
+ errno = map_errno(GetLastError());
return INVALID_HANDLE_VALUE;
}
- memcpy(scanname, filename, len + 1);
- if (index("/\\:", *CharPrev(scanname, scanname + len)) == NULL)
- memcpy(scanname + len, wildcard, sizeof(wildcard));
+ p = CharPrevW(scanname, scanname + len);
+ if (*p == L'/' || *p == L'\\' || *p == L':')
+ lstrcatW(scanname, wildcard + 1);
else
- memcpy(scanname + len, wildcard + 1, sizeof(wildcard) - 1);
+ lstrcatW(scanname, wildcard);
//
// do the FindFirstFile call
//
- fh = FindFirstFile(scanname, fd);
- free(scanname);
+ fh = FindFirstFileW(scanname, fd);
if (fh == INVALID_HANDLE_VALUE) {
errno = map_errno(GetLastError());
}
@@ -1471,9 +1477,10 @@ rb_w32_opendir(const char *filename)
DIR *p;
long len;
long idx;
- char *tmp;
+ WCHAR *tmpW;
+ char *tmp;
struct stati64 sbuf;
- WIN32_FIND_DATA fd;
+ WIN32_FIND_DATAW fd;
HANDLE fh;
//
@@ -1509,14 +1516,14 @@ rb_w32_opendir(const char *filename)
// of the previous string found.
//
do {
- len = strlen(fd.cFileName) + 1;
+ len = lstrlenW(fd.cFileName) + 1;
//
// bump the string table size by enough for the
// new name and it's null terminator
//
- tmp = realloc(p->start, idx + len);
- if (!tmp) {
+ tmpW = realloc(p->start, (idx + len) * sizeof(WCHAR));
+ if (!tmpW) {
error:
rb_w32_closedir(p);
FindClose(fh);
@@ -1524,8 +1531,8 @@ rb_w32_opendir(const char *filename)
return NULL;
}
- p->start = tmp;
- strlcpy(&p->start[idx], fd.cFileName, len);
+ p->start = tmpW;
+ memcpy(&p->start[idx], fd.cFileName, len * sizeof(WCHAR));
if (p->nfiles % DIRENT_PER_CHAR == 0) {
tmp = realloc(p->bits, p->nfiles / DIRENT_PER_CHAR + 1);
@@ -1541,7 +1548,7 @@ rb_w32_opendir(const char *filename)
p->nfiles++;
idx += len;
- } while (FindNextFile(fh, &fd));
+ } while (FindNextFileW(fh, &fd));
FindClose(fh);
p->size = idx;
p->curr = p->start;
@@ -1557,7 +1564,7 @@ move_to_next_entry(DIR *dirp)
{
if (dirp->curr) {
dirp->loc++;
- dirp->curr += strlen(dirp->curr) + 1;
+ dirp->curr += lstrlenW(dirp->curr) + 1;
if (dirp->curr >= (dirp->start + dirp->size)) {
dirp->curr = NULL;
}
@@ -1572,17 +1579,20 @@ move_to_next_entry(DIR *dirp)
struct direct *
rb_w32_readdir(DIR *dirp)
{
- static int dummy = 0;
+ static int dummy = 0;
+ UINT cp = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
if (dirp->curr) {
//
// first set up the structure to return
//
- dirp->dirstr.d_namlen = strlen(dirp->curr);
+ dirp->dirstr.d_namlen =
+ WideCharToMultiByte(cp, 0, dirp->curr, -1, NULL, 0, NULL, NULL) - 1;
if (!(dirp->dirstr.d_name = malloc(dirp->dirstr.d_namlen + 1)))
return NULL;
- strlcpy(dirp->dirstr.d_name, dirp->curr, dirp->dirstr.d_namlen + 1);
+ WideCharToMultiByte(cp, 0, dirp->curr, -1, dirp->dirstr.d_name,
+ dirp->dirstr.d_namlen + 1, NULL, NULL);
//
// Fake inode
@@ -3531,7 +3541,7 @@ fileattr_to_unixmode(DWORD attr, const char *path)
static int
check_valid_dir(const char *path)
{
- WIN32_FIND_DATA fd;
+ WIN32_FIND_DATAW fd;
HANDLE fh = open_dir_handle(path, &fd);
if (fh == INVALID_HANDLE_VALUE)
return -1;